I'm using mongoDB (mongoHQ) in my Flask app (mongoengine).
I have a Document
that looks like this:
{items: [{id: 1}, {id: 2}, {id: 3}]}
Is there a way to to reach, for example, the dict
with id: 1
in a single query?
Currently I'm looping through the items
list with a next()
statement and I was hoping for a wiser solution. Thanks
Best Answer
I'm not familiar with MongoEngine, but the $ projection operator can filter an array to show only the matched element. In the Mongo shell:
More info: http://docs.mongodb.org/manual/reference/projection/positional/
It seems in MongoEngine you do:
More: http://mongoengine-odm.readthedocs.org/en/latest/guide/querying.html#raw-queries