mongoDB中的DBRef

來源:互聯網
上載者:User

前段時間串講的時候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

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.