Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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}) 
like image 574
Uuid Avatar asked Dec 07 '12 02:12

Uuid


1 Answers

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] 
like image 172
Sushant Gupta Avatar answered Sep 17 '22 12:09

Sushant Gupta