DbrefIs a more formal specification for creating references between documents. dbrefs (generally) include a collection name as well as an object ID. Most developers only use dbrefs if the collection can change from one
Document to the next. If your referenced collection will always be the same, the manual references outlined abve are more efficient.
^_^[root@:/usr/local/mongodb/bin]#./mongoMongoDB shell version: 1.8.2connecting to: test> var a = {name:"C++"} > db test> db.language.save(a)> db.language.find(){ "_id" : ObjectId("4da32c897d2de864e0448e06"), "name" : "C++" }> var b = {name:"javascript"}> db.language.save(b)> db.language.find(){ "_id" : ObjectId("4da32c897d2de864e0448e06"), "name" : "C++" }{ "_id" : ObjectId("4da32cb17d2de864e0448e07"), "name" : "javascript" }> lan = {name:"obj1",computer:[new DBRef('language',a._id)]}{ "name" : "obj1", "computer" : [ { "$ref" : "language", "$id" : ObjectId("4da32c897d2de864e0448e06") } ]}> lan.computer[0]{ "$ref" : "language", "$id" : ObjectId("4da32c897d2de864e0448e06") }> lan.computer[0].fetch(){ "_id" : ObjectId("4da32c897d2de864e0448e06"), "name" : "C++" }> db.language.insert(lan) > db.language.find(){ "_id" : ObjectId("4da32c897d2de864e0448e06"), "name" : "C++" }{ "_id" : ObjectId("4da32cb17d2de864e0448e07"), "name" : "javascript" }{ "_id" : ObjectId("4da33b487d2de864e0448e08"), "name" : "obj1", "computer" : [ { "$ref" : "language", "$id" : ObjectId("4da32c897d2de864e0448e06") } ] }> db.language.findOne({name:"obj1"}).computer[0].fetch() { "_id" : ObjectId("4da32c897d2de864e0448e06"), "name" : "C++" }> lan2 = {name:"obj2",computer:[new DBRef('language',b._id)]} { "name" : "obj2", "computer" : [ { "$ref" : "language", "$id" : ObjectId("4da32cb17d2de864e0448e07") } ]}> db.language.insert(lan2)> db.language.find(){ "_id" : ObjectId("4da32c897d2de864e0448e06"), "name" : "C++" }{ "_id" : ObjectId("4da32cb17d2de864e0448e07"), "name" : "javascript" }{ "_id" : ObjectId("4da33b487d2de864e0448e08"), "name" : "obj1", "computer" : [ { "$ref" : "language", "$id" : ObjectId("4da32c897d2de864e0448e06") } ] }
Related data is stored together, and targeted queries can eliminate join, which is more efficient and convenient than distributed storage.