Because MongoDB is non-relational (join), references between documents ("Foreign keys") are usually solved by the client through additional queries to the server (linking ).
These connections are always resolved on the client. It is very easy to do this directly/manually and is recommended.
There is also a dbref mechanism that many drivers support. It abstracts the concept of linking to a certain extent.The recommended method is direct/manual link..
Embedded objects are an alternative to linking and are often very suitable and outstanding.
Simple direct/Manual linking
Generally, writing a link solution manually can work very well and easily. We store the value in _ id to other documents in the database and query it later. For example:
> // grab a random blog post:
> p = db.postings.findOne();
{
"_id" : ObjectId("4b866f08234ae01d21d89604"),
"author" : "jim",
"title" : "Brewing Methods"
}
> // get more info on author of post p. this is the "linking" step.
> a = db.users.findOne( { _id : p.author } )
{ "_id" : "jim", "email" : "jim@gmail.com" }
> // inverse: given an author, find all blog posts for the author
> db.postings.find( {author : a._id } )
DBRef
Dbref is a more formal specification for document reference creation. Dbrefs (usually) contains a set name in addition to the Object ID. Most developers use dbref only when the set in the document can be changed to another one. If the referenced set is always the same, the manual reference described above will be more efficient.
Dbref is a reference to another document in the same database. A database reference is a standard embedded (JSON/bson) object: a convention we define, not a special type. By using standard methods, the driver and data framework can add assistant methods to operate and reference according to standard methods.
In some drivers, dbref has an advantage that allows optional atomic client unreference. In many scenarios, you can simply store _ id as a reference, and then dereference it according to the previous section "simple manual reference.
The syntax for dbref reference values is:
{ $ref : <collname>, $id : <idvalue>[, $db : <dbname>] }
<Collname> is the referenced collection name (without the database name), and <idvalue> is the value of the field _ ID of the referenced object. $ Db is optional (currently not supported by most drivers) and documents that can be referenced are in other databases (specified by <dbname> ).
DBRef in different languages/Drivers
C #
Use the dbref class. The constructor parameters are set names and _ IDs. Then you can use the followreference method in the database class to obtain the reference document.
C ++
The c ++ driver does not provide a way to automatically traverse DBRefs. Of course you can do it manually.
Java
Java uses the DBRef class to support DB reference.
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" }
>
Perl
The perl driver does not provide a way to automatically traverse DBRefs, but there is a CPAN package: MongoDBx: AutoDeref. Of course you can also manually traverse them.
PHP
Apart from the creation and reference methods at the database level (MongoDB: createDBRef and MongoDB: getDBRef) and collection level (Collection collection: createDBRef and collection: getDBRef, PHP also provides the MongoDBRef class to support DB reference.
Python
Use the bson. dbref. DBRef class in python to create a DB reference. You can also use the dereference method in the Database instance to make the solution reference easier.
Ruby
Ruby also uses the DBRef class and the dereference method in the DB instance to support DB reference. 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)