MongoDB之DBref(關聯插入,查詢,刪除) 執行個體深入

來源:互聯網
上載者:User

標籤:style   blog   http   color   io   os   ar   strong   for   

MongoDB之DBref(關聯插入,查詢,刪除) 執行個體深入

 

,A,B,C三個Collection互相關聯。 其中的數字為document的value值。

關於DBref的入門可以看http://blog.csdn.net/crazyjixiang/article/details/6616678這篇文章。

我們先建立A collection。

 

Cpp代碼
  1. > var a={value:"1"} 
  2. > var b={value:"2"} 
  3. > var c={value:"9"} 
  4. > var d={value:"10"} 
  5. > db.A.save(a) 
  6. > db.A.save(b)       
  7. > db.A.save(c)  
  8. > db.A.save(d) 
  9. > db.A.find()                                                                                                
  10. { "_id" : ObjectId("4e3f33ab6266b5845052c02b"), "value" : "1" } 
  11. { "_id" : ObjectId("4e3f33de6266b5845052c02c"), "value" : "2" } 
  12. { "_id" : ObjectId("4e3f33e06266b5845052c02d"), "value" : "9" } 
  13. { "_id" : ObjectId("4e3f33e26266b5845052c02e"), "value" : "10" } 
> var a={value:"1"}> var b={value:"2"}> var c={value:"9"}> var d={value:"10"}> db.A.save(a)> db.A.save(b) > db.A.save(c) > db.A.save(d)> db.A.find() { "_id" : ObjectId("4e3f33ab6266b5845052c02b"), "value" : "1" }{ "_id" : ObjectId("4e3f33de6266b5845052c02c"), "value" : "2" }{ "_id" : ObjectId("4e3f33e06266b5845052c02d"), "value" : "9" }{ "_id" : ObjectId("4e3f33e26266b5845052c02e"), "value" : "10" } B collection以A collection的 _id為ObjectId("4e3f33de6266b5845052c02c")作為Apid

 

所以:

 

Cpp代碼
  1. > var Ba={Apid:[new DBRef(‘A‘,ObjectId("4e3f33de6266b5845052c02c"))],value:3}                       
  2. > db.B.save(Ba) 
  3. > var Ba={Apid:[new DBRef(‘A‘,ObjectId("4e3f33de6266b5845052c02c"))],value:4} 
  4. > db.B.insert(Ba)                                                             
  5. > var Ba={Apid:[new DBRef(‘A‘,ObjectId("4e3f33de6266b5845052c02c"))],value:7} 
  6. > db.B.insert(Ba)                                                             
  7. > var Ba={Apid:[new DBRef(‘A‘,ObjectId("4e3f33de6266b5845052c02c"))],value:8} 
  8. > db.B.insert(Ba)                                                             
  9. > db.B.find() 
  10. { "_id" : ObjectId("4e3f3dd96266b5845052c035"), "Apid" : [ { "$ref" : "A", "$id" : ObjectId("4e3f33de6266b5845052c02c") } ], "value" : 3 } 
  11. { "_id" : ObjectId("4e3f3de16266b5845052c036"), "Apid" : [ { "$ref" : "A", "$id" : ObjectId("4e3f33de6266b5845052c02c") } ], "value" : 4 } 
  12. { "_id" : ObjectId("4e3f3dec6266b5845052c037"), "Apid" : [ { "$ref" : "A", "$id" : ObjectId("4e3f33de6266b5845052c02c") } ], "value" : 7 } 
  13. { "_id" : ObjectId("4e3f3df06266b5845052c038"), "Apid" : [ { "$ref" : "A", "$id" : ObjectId("4e3f33de6266b5845052c02c") } ], "value" : 8 } 
> var Ba={Apid:[new DBRef(‘A‘,ObjectId("4e3f33de6266b5845052c02c"))],value:3} > db.B.save(Ba)> var Ba={Apid:[new DBRef(‘A‘,ObjectId("4e3f33de6266b5845052c02c"))],value:4}> db.B.insert(Ba) > var Ba={Apid:[new DBRef(‘A‘,ObjectId("4e3f33de6266b5845052c02c"))],value:7}> db.B.insert(Ba) > var Ba={Apid:[new DBRef(‘A‘,ObjectId("4e3f33de6266b5845052c02c"))],value:8}> db.B.insert(Ba) > db.B.find(){ "_id" : ObjectId("4e3f3dd96266b5845052c035"), "Apid" : [ { "$ref" : "A", "$id" : ObjectId("4e3f33de6266b5845052c02c") } ], "value" : 3 }{ "_id" : ObjectId("4e3f3de16266b5845052c036"), "Apid" : [ { "$ref" : "A", "$id" : ObjectId("4e3f33de6266b5845052c02c") } ], "value" : 4 }{ "_id" : ObjectId("4e3f3dec6266b5845052c037"), "Apid" : [ { "$ref" : "A", "$id" : ObjectId("4e3f33de6266b5845052c02c") } ], "value" : 7 }{ "_id" : ObjectId("4e3f3df06266b5845052c038"), "Apid" : [ { "$ref" : "A", "$id" : ObjectId("4e3f33de6266b5845052c02c") } ], "value" : 8 }

 

C collection以B collection的 _id為ObjectId("4e3f3de16266b5845052c036") 作為Apid

 

Cpp代碼
  1. > var Ca={Bpid:[new DBRef(‘B‘,ObjectId("4e3f3de16266b5845052c036"))],value:5}                       
  2. > db.C.save(Ca)                                                               
  3. > var Ca={Bpid:[new DBRef(‘B‘,ObjectId("4e3f3de16266b5845052c036"))],value:6} 
  4. > db.C.save(Ca)                                                               
  5. > db.C.find() 
  6. { "_id" : ObjectId("4e3f42f36266b5845052c03d"), "Bpid" : [ { "$ref" : "B", "$id" : ObjectId("4e3f3de16266b5845052c036") } ], "value" : 5 } 
  7. { "_id" : ObjectId("4e3f42f96266b5845052c03e"), "Bpid" : [ { "$ref" : "B", "$id" : ObjectId("4e3f3de16266b5845052c036") } ], "value" : 6 } 
> var Ca={Bpid:[new DBRef(‘B‘,ObjectId("4e3f3de16266b5845052c036"))],value:5} > db.C.save(Ca) > var Ca={Bpid:[new DBRef(‘B‘,ObjectId("4e3f3de16266b5845052c036"))],value:6}> db.C.save(Ca) > db.C.find(){ "_id" : ObjectId("4e3f42f36266b5845052c03d"), "Bpid" : [ { "$ref" : "B", "$id" : ObjectId("4e3f3de16266b5845052c036") } ], "value" : 5 }{ "_id" : ObjectId("4e3f42f96266b5845052c03e"), "Bpid" : [ { "$ref" : "B", "$id" : ObjectId("4e3f3de16266b5845052c036") } ], "value" : 6 }

 

 

目前為止3個collection 的關係已經建成。

查詢

 

 

Cpp代碼
  1. <span style="font-size: 16px;">> var a = db.B.findOne({"value":4})                                                      
  2. > a.Apid.forEach(function(ref){printjson(db[ref.$ref].findOne({"_id":ref.$id}));}) 
  3. { "_id" : ObjectId("4e3f33de6266b5845052c02c"), "value" : "2" }</span> 
<span style="font-size: 16px;">> var a = db.B.findOne({"value":4}) > a.Apid.forEach(function(ref){printjson(db[ref.$ref].findOne({"_id":ref.$id}));}){ "_id" : ObjectId("4e3f33de6266b5845052c02c"), "value" : "2" }</span>

 

Cpp代碼
  1. > db.A.findOne({"_id":db.B.findOne().Apid[0].$id}) 
  2. { "_id" : ObjectId("4e3f33de6266b5845052c02c"), "value" : "2" } 
> db.A.findOne({"_id":db.B.findOne().Apid[0].$id}){ "_id" : ObjectId("4e3f33de6266b5845052c02c"), "value" : "2" }

 

其實好好想想引用不是必須的。
MongoDB 權威指南說了這麼一句:

In short,the best time to use DBRefs are when you‘re storing heterogeneous references to documents in different collections.like when you want to take advantage of some additional DBRef-specific functionality in a driver or tool.

MongoDB之DBref(關聯插入,查詢,刪除) 執行個體深入

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.