MongoEngine/mongoengine

Covered query not working in mongoengine

suyashdeshpande opened this issue · 1 comments

How to do a covered query in mongoengine? I tried and it is not parsing the qyery as expected due to which covered query isn't working.
https://www.mongodb.com/community/forums/t/chapter-4-covered-queries/92350
Any example for this?

If I take the example from the post that you reference

db.example.find( { name : { $in : [ “Bart”, “Homer” ] } }, {_id : 0, dob : 1, name : 1} )

I didn't actually verify that only the index was used but assuming you have a MongoEngine Document class like

class Example(Document):
    name = StringField()
    dob = DateTimeField()

    meta = {...your index definition ...}

I would assume that the 2 following queries would be "covered queries":

Example.objects(name__in=["Bob", "Foo"]).only('dob', 'name').exclude('id').as_pymongo() # returns you a dict
Example.objects(name__in=["Bob", "Foo"]).only('dob', 'name').exclude('id')    # returns you an Example instance (with only dob and name set)