How to search for an oid document in mongoengine

I need to get documents from db using oid, for example:

Docs.objects(_id='4f4381f4e779897a2c000009')

But how to do this, if _id requires an ObjectId object, and even I try to set the ObjectId from pymongo, it does not work.

Docs.objects(_id=pymongo.objectid.ObjectId('4f4381f4e779897a2c000009'))

return empty list

+5
source share
3 answers

How to simply use the source string:

Docs.objects.get(id='4f4381f4e779897a2c000009')

This is probably the easiest way ... right?

+15
source

This should work:

Docs.objects(pk='4f4381f4e779897a2c000009')
+20
source

Came to this question because I had a lot of problems with this. It seems that PyMongo has changed this , and the objectid is no longer inside pymongo and now instead:

import bson
Doc.objects.get(id=bson.objectid.ObjectId('4f4381f4e779897a2c000009'))

In addition, Mongoengine uses the name 'id' for the ObjectID field.

+7
source

All Articles