MongoDB Middle Level (關聯多表查詢)

來源:互聯網
上載者:User

DBRef is 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 above 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") } ] }

相關資料存放在一起,針對性的查詢可以消除join,效能比分散儲存要高且方便



相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.