前段时间串讲的时候mongoDB中的DBRef这个东西没有讲太明白,由于时间原因就匆匆带过了。在这里附上一个小实验,补充一下。
As MongoDB is non-relational (no joins), references (”foreign keys”) between documents 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.
个人理解DBRef就是在两个collection之间定义的一个关联,比如把collectionB的”_id”列的值存在collectionA的一个列中,然后通过collectionA这个列中所存的值在collectionB中找到相应的记录。
mongoDB号称是Document-oriented database。在mongoDB中,collection就相当于table,document相当于row,至于上面提到的object,可以理解 为一个行对象,当存储进一个collection中就变成一条document。而所谓的”_id”列其实就是系统自动生成的一个唯一列,用于标识一个 collection里的每条document。系统默认会对”_id”列进行索引,如果可以的话,你可以指定RDBMS表中的主键列来填充”_id”, 这样既保证了唯一性,也能有效的利用起这个索引。
下面是一个关于课程表和学生表的例子,学生表有”_id”,name,classes三个列,课程表有”_id”,name两个列。通过在学生表的classes列中存储课程表中相应记录的”_id”值,来达到两张表关联的目的。
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 documents in colletion courses,just like the select in RDBMS
{ “_id” : ObjectId(”4c7ce443e7140000000072c1″), “name” : “English” }
> db.courses.insert({name:”Biology”}) #insert another document into collection courses
> db.courses.find()
{ “_id” : ObjectId(”4c7ce443e7140000000072c1″), “name” : “English” }
{ “_id” : ObjectId(”4c7ce498e7140000000072c2″), “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("4c7ce443e7140000000072c1")
}
]
}
> stu1.classes[0]
{ “$ref” : “courses”, “$id” : ObjectId(”4c7ce443e7140000000072c1″) }
> stu1.classes[0].fetch() #use the object stu1 to fetch the value referenced in collection courses
{ “_id” : ObjectId(”4c7ce443e7140000000072c1″), “name” : “English” }
> db.students.insert(stu1) #save the object stu1 into collection students
> db.students.find()
{ “_id” : ObjectId(”4c7ce6c0e7140000000072c4″), “name” : “Joe1″, “classes” : [ {
"$ref" : "courses", "$id" : ObjectId("4c7ce443e7140000000072c1") } ] }
> db.students.findOne({name:”Joe1″}).classes[0]
{ “$ref” : “courses”, “$id” : ObjectId(”4c7ce443e7140000000072c1″) }
> db.students.findOne({name:”Joe1″}).classes[0].fetch() #use the object stu1 to fetch the value referenced in collection courses
{ “_id” : ObjectId(”4c7ce443e7140000000072c1″), “name” : “English” }
> stu2 = {name:”Joe2″,classes:[new DBRef('courses',ObjectId("4c7ce498e7140000000072c2"))]} #define another student object stu2,also with the column “_id”,just in another style
{
“name” : “Joe2″,
“classes” : [
{
"$ref" : "courses",
"$id" : ObjectId("4c7ce498e7140000000072c2")
}
]
}
> db.students.insert(stu2)
> db.students.find()
{ “_id” : ObjectId(”4c7ce6c0e7140000000072c4″), “name” : “Joe1″, “classes” : [ {
"$ref" : "courses", "$id" : ObjectId("4c7ce443e7140000000072c1") } ] }
{ “_id” : ObjectId(”4c7ce754e7140000000072c5″), “name” : “Joe2″, “classes” : [ {
"$ref" : "courses", "$id" : ObjectId("4c7ce498e7140000000072c2") } ] }
> db.students.findOne({name:”Joe2″}).classes[0]
{ “$ref” : “courses”, “$id” : ObjectId(”4c7ce498e7140000000072c2″) }
> db.students.findOne({name:”Joe2″}).classes[0].fetch() #also works
{ “_id” : ObjectId(”4c7ce498e7140000000072c2″), “name” : “Biology” }
下面链接是官网上关于DBRef的讲解,有兴趣可以看一下
http://www.mongodb.org/display/DOCS/Database+References