Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best way to query a DictField in MongoEngine

Tags:

mongoengine

I've been hunting through the mongoengine documentation and around stack overflow and there doesn't seem to be a very clear answer to this so I'm asking: how do you best query a DictField? example code:

class Note(Document):
    someData = DictField()

note = Note()
note.someData['someID'] = {"name": "Steve", "age":25}
note.save()

The closest I could find in the docs would be:

Note.objects(someData__name="Steve") 

but that hasn't been working Again, feel like this should be a simple answer. Thanks for your help

like image 332
Matt Davis Avatar asked Aug 15 '13 22:08

Matt Davis


1 Answers

You have wrong request, because you miss someID.

See you structure in db:

>>> db.note.findOne()
>>> {
    "_id": ObjectId("'0'*24")
    "someData": {
        "someID": {
            {"name": "Steve", "age":25}
        }
    }
}

So right request will be Note.objects(someData__someID__name="Steve").

like image 73
tbicr Avatar answered Oct 26 '22 18:10

tbicr