How to search document by oid in mongoengine

后端 未结 3 1728
伪装坚强ぢ
伪装坚强ぢ 2021-02-08 14:30

I need get documents from db by oid, like:

Docs.objects(_id=\'4f4381f4e779897a2c000009\')

But how to do it, if _id requires ObjectId object and

相关标签:
3条回答
  • 2021-02-08 15:01

    Came to this question because I had a lot of trouble with this myself. It seems like PyMongo changed this and objectid is no longer inside pymongo and is now instead:

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

    Also, Mongoengine uses the name 'id' for the ObjectID field.

    0 讨论(0)
  • 2021-02-08 15:05

    How about just using the raw string:

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

    That is probably the easiest way ... right ?

    0 讨论(0)
  • 2021-02-08 15:24

    This should work:

    Docs.objects(pk='4f4381f4e779897a2c000009')
    
    0 讨论(0)
提交回复
热议问题