MongoDB dbref (Associative insert, query, delete) instance in depth
, A,b,c three collection are interrelated. The number is the value of document.
The introduction to Dbref can be seen http://blog.csdn.net/crazyjixiang/article/details/6616678 this article.
Let's set up a collection first.
CPP Code
- > var a={value:"1"}
- > var b={value:"2"}
- > var c={value:"9"}
- > var d={value:"Ten"}
- > 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": "Ten"}
<textarea class="cpp" style="display: none;" name="code">> var a={value: "1"}> var b={value: "2"}> var c={value: "9"}> var d={value: "Ten"}> 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": objec TId ("4e3f33e06266b5845052c02d"), "value": "9"} {"_id": ObjectId ("4e3f33e26266b5845052c02e"), "value": "Ten"}</textarea>B collection with the _id of a collectionObjectId ("4e3f33de6266b5845052c02c") as Apid
So:
CPP Code
- > var ba={apid:[new Dbref (' A ', ObjectId ("4e3f33de6266b5845052c02c"))],value:3} /c1>
- > 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 ("4 E3f33de6266b5845052c02c ")}], " value ": 3}
- { "_id": ObjectId ("4e3f3de16266b5845052c036"), "Apid": [{ "$ref": "A", "$id": ObjectId ("4 E3f33de6266b5845052c02c ")}], " value ": 4}
- { "_id": ObjectId ("4e3f3dec6266b5845052c037"), "Apid": [{ "$ref": "A", "$id": ObjectId ( "4e3f33de6266b5845052c02c")}], "value": 7}
- { "4e3f3df06266b5845052c038"), "Apid": [{ "A", "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 ("4e3f33d e6266b5845052c02c "))],value:7}> db. B.insert (Ba) > var ba={apid:[new dbref (' A ', ObjectId ("4e3f33d e6266b5845052c02c "))],value:8}> db. B.insert (Ba) > db. B.find () {"_id": ObjectId ("4e3f3dd96266b5845052c035"), "Apid": [{"$ref": "A", "$id": ObjectId ("4e3f33de6266b5845052c 02c ")}]," Value ": 3} {" _id ": ObjectId (" 4e3f3de16266b5845052c036 ")," Apid ": [{" $ref ":" A "," $id ": ObjectId (" 4e3f3 3de6266b5845052c02c ")}]," 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 with the _id of B collection as ObjectId ("4e3f3de16266b5845052c036") as Apid
CPP Code
- > var ca={bpid:[new Dbref (' B ', ObjectId ("4e3f3de16266b5845052c036"))],value:5} /c2>
- > 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 ("4 e3f3de16266b5845052c036 ")}], " value ": 5}
- { "_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 ("4e3f3de 16266b5845052c036 "))],value:6}> db. C.save (Ca) > db. C.find () {"_id": ObjectId ("4e3f42f36266b5845052c03d"), "bpid": [{"$ref": "B", "$id": ObjectId ("4e3f3de16266b5845052c 036 ")}]," Value ": 5} {" _id ": ObjectId (" 4e3f42f96266b5845052c03e ")," bpid ": [{" $ref ":" B "," $id ": ObjectId (" 4e3f3 de16266b5845052c036 ")}]," Value ": 6}
so far, 3 collection relationships have been built.
Inquire
CPP Code
- <span style= "FONT-SIZE:16PX;" >> var a = db. B.findone ({
- > A.apid.foreach (function (ref) {Printjson (db [Ref. $ref].findone ({
- {
<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 Code
- > DB. A.findone ({"_id":d B. B.findone (). Apid[0]. $id})
- { "_id": ObjectId ("4e3f33de6266b5845052c02c"), "value": "2"}
> Db. A.findone ({"_id":d B. B.findone (). (Apid[0]. $id}) {"_id": ObjectId ("4e3f33de6266b5845052c02c"), "value": "2"}
Actually, it's not necessary to think about references.
The MongoDB authoritative guide says this sentence:
In Short,the best time to use DBRefs is when you ' re storing heterogeneous references to documents in different collection S.like when you want to take advantage of some additional dbref-specific functionality in a driver or tool.
MongoDB dbref (Associative insert, query, delete) instance in depth