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

前端 未结 6 538
北海茫月
北海茫月 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 13:59

    And this is how you can do it in python (pymongo):

    from pymongo import MongoClient
    import bson
    
    client = MongoClient('mongodb://localhost:27017/')
    db = client.test_db
    
    result=db.users.insert_one({'name': 'jim',
        'documents': [
            {'_id': bson.objectid.ObjectId(), 'title': "My document"},
            {'_id': bson.objectid.ObjectId(), 'title': "My second document!"},
        ]})
    
    print list(db.users.find({}))
    
    0 讨论(0)
  • 2021-01-30 14:06

    Yes, using mongo's ObjectId is the way to go. The only thing is: you have to generate them yourself, in the application code. They are meant to be globally unique, different workers won't generate two identical ObjectIds, so there's no race condition in that sense.

    All official drivers should provide a way to generate ObjectId. Here's how it is in Ruby:

    oid = BSON::ObjectId.new
    
    0 讨论(0)
  • And here's how to do it in Clojure, assuming the use of congomongo:

    (import org.bson.types.ObjectId)
    (str (ObjectId.)) ; => "12345xxxxx"
    
    0 讨论(0)
  • 2021-01-30 14:09

    In Meteor, on the server, use:

    new Meteor.Collection.ObjectID(hexString);
    

    See: http://docs.meteor.com/#collection_object_id

    0 讨论(0)
  • 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()
    
    0 讨论(0)
  • 2021-01-30 14:15

    All drivers have functionality for generating ObjectIds.

    In the shell you just do new ObjectId():

    > db.test.insert({x:new ObjectId()});
    > db.test.find();
    { "_id" : ObjectId("4f88592a06c05e4de90d0bc1"), "x" : ObjectId("4f88592a06c05e4de90d0bc0") }
    

    In Java it's new ObjectId() as well. See the API docs for your driver to see the specific syntax.

    0 讨论(0)
提交回复
热议问题