DBRef
我注意到在 mongodb 中可以使用字符串或对象 id 来构造 a 。例如
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") } }
谁能告诉我有什么区别以及首选哪种方式?