Uuid
Uuid

Reputation: 2556

find() and findOne() in mongoengine

How can I do a quick find() or findOne() with mongoengine, I already have this but it does not seems to be the right way:

Cars.objects()._collection.find_one({'model':2013})

Upvotes: 33

Views: 28665

Answers (1)

Sushant Gupta
Sushant Gupta

Reputation: 9468

For find() you can do:

Cars.objects(model=2013)

And for find_one() you can do:

Cars.objects.get(model=2013)

To retrieve a result that should be unique in the collection, use get(). This will raise DoesNotExist if no document matches the query, and MultipleObjectsReturned if more than one document matched the query.

Else if multiple records exists, simply limit, like:

Cars.objects(model=2013)[0]

Upvotes: 65

Related Questions