mongodb關聯查詢

來源:互聯網
上載者:User

標籤:images   ddr   insert   use   toolbar   mon   bsp   文檔   分享   

使用DBRefs

DBRefs中有三個欄位 -

  • $ref - 此欄位指定引用文檔的集合
  • $id - 此欄位指定引用文檔的_id欄位
  • $db - 這是一個可選欄位,並包含引用文檔所在的資料庫的名稱

假設一個具有DBRef欄位address的樣本使用者文檔,如程式碼片段所示 -

{ "_id":ObjectId("348362491fjaskdlf2314"), "address": { "$ref": "address_home", "$id": ObjectId("sfaafdf4137832149fssa"), "$db": "yiibai"}, "contact": "13800138000", "dob": "1991-12-12", "name": "Maxsu" }
使用DBef

建立collection   role角色表

db.role.insert([{role:"admin"},{role:"root"},{role:"guest"}])

 

建立使用者user表

db.user.insert([
{roleId:[new DBRef(‘role‘,ObjectId("59b7910c943eabe738230d34")),new DBRef(‘role‘,ObjectId("59b7910c943eabe738230d35"))]},
{roleId:[new DBRef(‘role‘,ObjectId("59b7914c943eabe738230d36"))]}
])

 

‘role‘是剛才建立的role表名,objectId是當前的這條user記錄要關聯哪些role表中的記錄(role中的id)

 

 查詢:

查指定id的user記錄:

查指定id中的roleId欄位中的第1個關聯的role資訊

 

查指定id中的roleId欄位中的第2個關聯的role資訊

 http://www.yiibai.com/mongodb

mongodb關聯查詢

聯繫我們

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