MongoDB dbref (Associative insert, query, delete) instance in depth

Source: Internet
Author: User
Tags findone

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
  1. > var a={value:"1"}
  2. > var b={value:"2"}
  3. > var c={value:"9"}
  4. > var d={value:"Ten"}
  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": "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
  1. > var ba={apid:[new Dbref (' A ', ObjectId ("4e3f33de6266b5845052c02c"))],value:3} /c1>
  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 ("4 E3f33de6266b5845052c02c ")}], " value ": 3}
  11. { "_id": ObjectId ("4e3f3de16266b5845052c036"), "Apid": [{ "$ref": "A", "$id": ObjectId ("4 E3f33de6266b5845052c02c ")}], " value ": 4}
  12. { "_id": ObjectId ("4e3f3dec6266b5845052c037"), "Apid": [{ "$ref": "A", "$id": ObjectId ( "4e3f33de6266b5845052c02c")}], "value": 7}
  13. { "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
  1. > var ca={bpid:[new Dbref (' B ', ObjectId ("4e3f3de16266b5845052c036"))],value:5} /c2>
  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 ("4 e3f3de16266b5845052c036 ")}], " 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 ("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
    1. <span style= "FONT-SIZE:16PX;" >> var a = db. B.findone ({
    2. > A.apid.foreach (function (ref) {Printjson (db [Ref. $ref].findone ({
    3. {
<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
    1. > DB. A.findone ({"_id":d B. B.findone (). Apid[0]. $id})
    2. { "_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

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.