Some time ago, the dbref in MongoDB was not very clear, and it was in a hurry because of time. Add a small experiment here.
As MongoDB is non-relational (no joins), references ("Foreign keys") between parameters are generally resolved client-side by additional queries to the server.
A dbref is a reference from one document (object) to another within a database.
Personally, dbref is an association defined between two collections. For example, the value of the "_ id" column of collectionb is stored in a column of collectiona, then, find the corresponding record in collectionb by using the value stored in the collectiona column.
MongoDB is known as the document-oriented database. In MongoDB, collection is equivalent to table and document is equivalent to row. As for the object mentioned above, it can be understood as a row object. When it is stored in a collection, it becomes a document. The so-called "_ id" column is actually a unique column automatically generated by the system, used to identify each document in a collection. By default, the system will index the "_ id" column. If you can, you can specify the primary key column in the RDBMS table to fill in "_ id", which ensures uniqueness, this index can also be effectively used.
The following is an example of the curriculum and student table. The student table has three columns: "_ id", name, and classes. The curriculum has two columns: "_ id" and "name. The "_ id" value of the corresponding course records is stored in the classes column of the student table to associate the two tables.
MongoDB shell version: 1.6.0
Connecting to: Test
> C = {Name: 'English '} # define a course object
{"Name": "English "}
> DB. Courses. Save (c) # Save this object into a collection named courses
> DB. Courses. Find () # Use Find () to retrive the statements in colletion courses, just like the select in RDBMS
{"_ ID": objectid ("4c7ce443e71410872c1"), "name": "English "}
> DB. Courses. insert ({Name: "biology"}) # insert another document into collection courses
> DB. Courses. Find ()
{"_ ID": objectid ("4c7ce443e71410872c1"), "name": "English "}
{"_ ID": objectid ("4c7ce498e7141_72c2"), "name": "biology "}
> Stu1 = {name: "joe1", classes: [New dbref ('courses ', C. _ id)]} # define a student object that references the collection courses, with the column "_ id"
{
"Name": "joe1 ″,
"Classes ":[
{
"$ Ref": "courses ",
"$ Id": objectid ("4c7ce443e7141_72c1 ")
}
]
}
> Stu1.classes [0]
{"$ Ref": "courses", "$ ID": objectid ("4c7ce443e7141_72c1 ″)}
> Stu1.classes [0]. Fetch () # Use the object stu1 to fetch the value referenced in collection courses
{"_ ID": objectid ("4c7ce443e71410872c1"), "name": "English "}
> DB. Students. insert (stu1) # Save the object stu1 into collection students
> DB. Students. Find ()
{"_ ID": objectid ("4c7ce6c0e71410872c4"), "name": "joe1", "Classes ":[{
"$ Ref": "courses", "$ id": objectid ("4c7ce443e7141_72c1")}]}
> DB. Students. findone ({Name: "joe1"}). classes [0]
{"$ Ref": "courses", "$ ID": objectid ("4c7ce443e7141_72c1 ″)}
> DB. Students. findone ({Name: "joe1"}). classes [0]. Fetch () # Use the object stu1 to fetch the value referenced in collection courses
{"_ ID": objectid ("4c7ce443e71410872c1"), "name": "English "}
> Stu2 = {Name: "joe2", classes: [New dbref ('courses ', objectid ("4c7ce498e71420.000072c2")]} # define another student object stu2, also with the column "_ id", just in another style
{
"Name": "joe2 ″,
"Classes ":[
{
"$ Ref": "courses ",
"$ Id": objectid ("4c7ce498e7141_72c2 ")
}
]
}
> DB. Students. insert (stu2)
> DB. Students. Find ()
{"_ ID": objectid ("4c7ce6c0e71410872c4"), "name": "joe1", "Classes ":[{
"$ Ref": "courses", "$ id": objectid ("4c7ce443e7141_72c1")}]}
{"_ ID": objectid ("4c7ce754e71410872c5"), "name": "joe2", "Classes ":[{
"$ Ref": "courses", "$ id": objectid ("4c7ce498e71420.000072c2")}]}
> DB. Students. findone ({Name: "joe2"}). classes [0]
{"$ Ref": "courses", "$ ID": objectid ("4c7ce498e71420.000072c2 ″)}
> DB. Students. findone ({Name: "joe2"}). classes [0]. Fetch () # also works
{"_ ID": objectid ("4c7ce498e7141_72c2"), "name": "biology "}
The following link explains dbref on the official website. If you are interested, please take a look.
Http://www.mongodb.org/display/DOCS/Database+References