|
As MongoDB is non-relational (no joins), references ("foreign keys") between documents are generally resolved client-side by additional queries to the server. Two conventions are common for references in MongoDB: first simple manual references, and second, the DBRef standard, which many drivers support explicitly. Note: Often embedding of objects eliminates the need for references, but sometimes references are still appropriate. Simple Manual ReferencesGenerally, manually coded references work just fine. We simply store the value that is present in _id in some other document in the database. For example: > p = db.postings.findOne();
{
"_id" : ObjectId("4b866f08234ae01d21d89604"),
"author" : "jim",
"title" : "Brewing Methods"
}
> // get more info on author
> db.users.findOne( { _id : p.author } )
{ "_id" : "jim", "email" : "jim@gmail.com" }
DBRefDBRef is a more formal specification for creating references between documents. DBRefs (generally) include a collection name as well as an oject 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 above are more efficient.
DBRef's have the advantage of allowing optional automatic client-side dereferencing with some drivers, although more features may be added later. In many cases, you can just get away with storing the _id as a reference then dereferencing manually as detailed in the "Simple Manual References" section above. Syntax for a DBRef reference value is { $ref : <collname>, $id : <idvalue>[, $db : <dbname>] }
where <collname> is the collection name referenced (without the database name), and <idvalue> is the value of the _id field for the object referenced. $db is optional (currently unsupported by many of the drivers) and allows for references to documents in other databases (specified by <dbname>).
The old BSON DBRef datatype is deprecated. DBRef in Different Languages / DriversC#Use the DBRef class. It takes the collection name and _id as parameters to the constructor. Then you can use the FollowReference method on the Database class to get the referenced document. C++The C++ driver does not yet provide a facility for automatically traversing DBRefs. However one can do it manually of course. JavaJava supports DB references using the DBRef class. Javascript (mongo shell)Example: > x = { name : 'Biology' }
{ "name" : "Biology" }
> db.courses.save(x)
> x
{ "name" : "Biology", "_id" : ObjectId("4b0552b0f0da7d1eb6f126a1") }
> stu = { name : 'Joe', classes : [ new DBRef('courses', x._id) ] }
// or we could write:
// stu = { name : 'Joe', classes : [ {$ref:'courses',$id:x._id} ] }
> db.students.save(stu)
> stu
{
"name" : "Joe",
"classes" : [
{
"$ref" : "courses",
"$id" : ObjectId("4b0552b0f0da7d1eb6f126a1")
}
],
"_id" : ObjectId("4b0552e4f0da7d1eb6f126a2")
}
> stu.classes[0]
{ "$ref" : "courses", "$id" : ObjectId("4b0552b0f0da7d1eb6f126a1") }
> stu.classes[0].fetch()
{ "_id" : ObjectId("4b0552b0f0da7d1eb6f126a1"), "name" : "Biology" }
>
PHPPHP supports DB references with the MongoDBRef class, as well as creation and deferencing methods at the database (MongoDB::createDBRef and MongoDB::getDBRef) and collection (MongoCollection::createDBRef and MongoCollection::getDBRef) levels. PythonTo create a DB reference in python use the pymongo.dbref.DBRef class. You can also use the dereference method on Database instances to make dereferencing easier. Python also supports auto-ref and auto-deref - check out the auto_reference example. RubyRuby also supports DB references using the DBRef class and a dereference method on DB instances. For example: @db = Connection.new.db("blog") @user = @db["users"].save({:name => "Smith"}) @post = @db["posts"].save({:title => "Hello World", :user_id => @user.id}) @ref = DBRef.new("users", @post.user_id) assert_equal @user, @db.dereference(@ref) See Also |

PLEASE POST QUESTIONS IN THE USER GROUPS FORUM. Post non-question comments and helpful hints here.
blog comments powered by Disqus