MongoDB-dbref (associated insertion, query, and deletion) instance depth

Source: Internet
Author: User
Tags findone

, A, B, and C collections are correlated. The number is the value of document.

For more information about dbref, see the http://blog.csdn.net/crazyjixiang/article/details/6616678 article.

Create a collection first.

> 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 uses the _ ID of a collection as objectid ("4e3f33de6266b5841052c02c") as the apid

Therefore:

> 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 uses the _ id of B collection as objectid ("4e3f3de16266b5841052c036") as the apid

> 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 }


So far, the relationship between the three collections has been established.

Query


> 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" }


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

In fact, it is not necessary to think about references.

The MongoDB authoritative guide says:

In short, the best time to use dbrefs are when you're storing heterogeneous references to arguments in different collections. Like when you want to take advantage of some additional dbref-specific
Functionality in a driver or tool.


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.