How do I get projection $ to work in pymongo?
Clash Royale CLAN TAG#URR8PPP
How do I get projection $ to work in pymongo?
I have a query which runs in the mongo
repl.
mongo
I have a collection of documents that look somewhat like this...
[
"_id" : ObjectId("5b6b6959828619572d48a9da"),
"files" : [
"original_filename" : "test_file1.csv",
"s3_key" : "intake/uploads/3edc78aa-2275-45ad-afcb-23bf4a765591/test_file1.csv"
,
"original_filename" : "test_file2.csv",
"s3_key" : "intake/uploads/3edc78aa-2275-45ad-afcb-23bf4a765591/test_file2.csv"
],
"created_at" : ISODate("2018-08-08T22:06:17.366Z")
]
In the mongo
repl I can do:
mongo
db.uploads.find("_id": ObjectId("5b6b6959828619572d48a9da"), "files.original_filename": "test_file1.csv", "files.$": 1)
And I receive the data exactly as I want it:
"_id" : ObjectId("5b6b6959828619572d48a9da"),
"files" : [
"original_filename" : "test_file2.csv",
"s3_key" : "intake/uploads/3edc78aa-2275-45ad-afcb-23bf4a765591/test_file2.csv"
]
But in pymongo
I do:
pymongo
query =
'_id': upload_id,
'files.original_filename': 'test_file1.csv'
projection = 'files.$': 1
result = list(self.uploads.find(query, projection))
And result is wrong:
['files': , '_id': ObjectId('5b6b6959828619572d48a9da')]
WHY?!?
$elemMatch
db.uploads.find( "files.original_filename": "test_file1.csv" , "files": $elemMatch: original_filename: "test_file1.csv" )
I did try something similar. Thanks for responding! That does seem to work, but I'm still a little perplexed why there is a difference between the repl and python code.
– nackjicholson
Aug 10 at 16:31
Don't know why this works... But generally I use
$elemMatch
projection operator instead of $
positional operator and that's why I have suggested you– Anthony Winzlet
Aug 10 at 16:38
$elemMatch
$
1 Answer
1
query =
'_id': upload_id
projection =
'files':
'$elemMatch':
'original_filename': original_filename
result = list(self.uploads.find(query, projection))
I used this projection instead and I seem to get the data I want. I would still like to know why, and if there are other better ways of doing this.
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Try with
$elemMatch
...db.uploads.find( "files.original_filename": "test_file1.csv" , "files": $elemMatch: original_filename: "test_file1.csv" )
– Anthony Winzlet
Aug 10 at 16:27