MongoDB 學習筆記之 DBRef

來源:互聯網
上載者:User

標籤:find   com   限制   fetch   兩種   under   ase   test   mongo   

DBRef:

MongoDB建模有兩種方式,一種是內嵌(Embed),另一種是串連(Link)。內嵌比較好理解,就是欄位內容是個數組,數組內再包含文檔,而我們今天介紹的是另一種,稱為連結DBRef。由於MongoDB對單個文檔(document)有大小16M限制,設計時也要將這個限制納入考慮。

DBRef接收主要3個參數:

  • collection: 指定未經處理資料所在的集合
  • id: 指定引用文檔的id
  • database: 指定所引用資料庫的名稱

樣本:

建立people集合:

db.people.insert({name: "Sky", age: "20", "dep": "CSL"})
db.people.insert({name: "Bill", age: "22", "dep": "CSL"})

建立DBRef:

db.deps.insert({name: "CSL", num: 15, people: [ DBRef("people", ObjectId("59b51b36a36f6ee8c75b0f68"), "test"), DBRef("people", ObjectId("59b51b44a36f6ee8c75b0f69"), "test")]})

 

那麼如何引用呢?

db.deps.findOne({"name": "CSL"}).people[0].fetch()

db.deps.findOne({"name": "CSL"}).people[1].fetch()

 

MongoDB 學習筆記之 DBRef

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.