我注意到,可以使用字符串或對象ID來構造mongodb中的DBRef
。例如mongodb - 使用字符串或ObjectId構造DBRef
db.persons.insert({name: 'alice'})
db.persons.find()
// { "_id" : ObjectId("5165419064fada69cef33ea2"), "name" : "alice" }
db.persons.insert({name: 'bob', sister: new DBRef('persons', '5165419064fada69cef33ea2')}) // use a string
db.persons.find()
// { "_id" : ObjectId("5165419064fada69cef33ea2"), "name" : "alice" }
// { "_id" : ObjectId("516541c064fada69cef33ea3"), "name" : "bob", "sister" : { "$ref" : "persons", "$id" : "5165419064fada69cef33ea2" } }
db.persons.insert({name: 'cavin', sister: new DBRef('persons', new ObjectId('5165419064fada69cef33ea2'))}) // use an ObjectId
db.persons.find()
// { "_id" : ObjectId("5165419064fada69cef33ea2"), "name" : "alice" }
// { "_id" : ObjectId("516541c064fada69cef33ea3"), "name" : "bob", "sister" : { "$ref" : "persons", "$id" : "5165419064fada69cef33ea2" } }
// { "_id" : ObjectId("516541e464fada69cef33ea4"), "name" : "cavin", "sister" : { "$ref" : "persons", "$id" : ObjectId("5165419064fada69cef33ea2") } }
有誰告訴我有什麼區別和方法是首選?