How can I create unique IDs for embedded documents in MongoDB?

前端 未结 6 553
北海茫月
北海茫月 2021-01-30 13:37

So I need to reference particular subdocuments uniquely from items in my collection. For instance:

User = {
    \'name\': \'jim\',
    \'documents: [
        {\         


        
6条回答
  •  执念已碎
    2021-01-30 14:14

    With mongoengine create a ObjectId in an embedded document like this:

    from bson.objectid import ObjectId
    
    class Address(EmbeddedDocument):
        _id = ObjectIdField( required=True, default=lambda: ObjectId() )
        street = StringField()
    

提交回复
热议问题