MongoDB Intermediate----> Associated multi-table query

Source: Internet
Author: User
Tags mongodb

Http://www.linuxidc.com/Linux/2011-08/41043.htm


Dbrefis a more formal specification for creating references between documents.  DBRefs (generally) include a collection name as well as a object ID.  Most developers DBRefs if the collection can change from one document to the next. If your referenced collection'll always be the same, the manual references outlined above is more efficient. ^_^ [root@:/usr/Local/mongodb/bin]#./mongo mongodb Shell version:1.8.2 connecting to: Test > var a = {name: "C + +"} > DB Test ; Db.language.save (a) > Db.language.find () {"_id": ObjectId ("4da32c897d2de864e0448e06"), "name": "C + +"} > VA R B = {name: "JavaScript"} > Db.language.save (b) > Db.language.find () {"_id": ObjectId ("4da32c897d2de864e0448e06"), "Nam E ":" C + + "} {" _id ": ObjectId (" 4da32cb17d2de864e0448e07 ")," name ":" JavaScript "} > LAN = {name: "Obj1", Computer:[new dbref (' language ', a._id)]}   {             "name"  :  "obj1",            " Computer " : [                    {                             "$ref"  :  "language",                              "$id"  : objectid ("4da32c897d2de864e0448e06")                    }           ]  }   > lan.computer[0]    {  "$ref"  :  "Language",  "$id"  : objectid ("4da32c897d2de864e0448e06")  }   >  Lan.computer[0].Fetch() {"_id": ObjectId ("4da32c897d2de864e0448e06"), "name": "C + +"} > Db.language.Insert(LAN)                                                                                                 > db.language.find ()    {  "_id"  : objectid ("4da32c897d2de864e0448e06"),   "Name"  :  "C + +"  }   {  "_id"  : objectid ("4da32cb17d2de864e0448e07") ,  "Name"  :  "JavaScript"  }   {  "_id"  : objectid (" 4da33b487d2de864e0448e08 "), " name " : " obj1 ", " Computer " : [ { " $ref "  :  "Language",  "$id" &nbsP;: objectid ("4da32c897d2de864e0448e06")  } ] }   > db.language.findone ({ name: "Obj1"}). Computer[0].Fetch() {"_id": ObjectId ("4da32c897d2de864e0448e06"), "name": "c+ + "} > lan2 = {name: "Obj2", Computer:[new dbref (' language ', b._id)]}        {            "name"  :  "Obj2",             "Computer"  : [                    {                             "$ref"  :   "Language",                             "$id"  : objectid (" 4da32cb17d2de864e0448e07 ")                     }           ]  }    > db.language.Insert(lan2) > Db.language.find () {"_id": ObjectId ("4da32c897d2de864e0448e06"), "name": "C + +"} {"_id": ObjectId (" 4da32cb17d2de864e0448e07 ")," name ":" JavaScript "} {" _id ": ObjectId (" 4da33b487d2de864e0448e08 ")," name ":" Obj1 "," Co Mputer ": [{" $ref ":" Language "," $id ": ObjectId (" 4da32c897d2de864e0448e06 ")}]}

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.