MongoDB middle level (join multi-Table query)

Source: Internet
Author: User

DbrefIs a more formal specification for creating references between documents. dbrefs (generally) include a collection name as well as an object ID. Most developers only use dbrefs if the collection can change from one
Document to the next. If your referenced collection will always be the same, the manual references outlined abve are more efficient.

^_^[root@:/usr/local/mongodb/bin]#./mongoMongoDB shell version: 1.8.2connecting to: test> var a = {name:"C++"}                                                                                           > db test> db.language.save(a)> db.language.find(){ "_id" : ObjectId("4da32c897d2de864e0448e06"), "name" : "C++" }> var b = {name:"javascript"}> db.language.save(b)> db.language.find(){ "_id" : ObjectId("4da32c897d2de864e0448e06"), "name" : "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" : 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", "computer" : [ { "$ref" : "language", "$id" : ObjectId("4da32c897d2de864e0448e06") } ] }

Related data is stored together, and targeted queries can eliminate join, which is more efficient and convenient than distributed storage.



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.