[英]mongodb: how to get documents from more than 2 collections
我在mongodb中有2個收藏集
collection1 **user's**
_id:ObjectId("5a1bedd219001b168e33835e")
password:$2a$05$H5wz7kCm/UIGYpvGWruV0eRd.Blgndd4i8pzZcyW7uCG3U4kUzZM2
socket_id:ljlZzY73BZjnwjZBAAAD
nickName:des
email:desmond.dd31@gmail.com
__v:0
collection2 **messages**
_id:ObjectId("5a1bedd219001b168e33835e")
userId:5a1bedd219001b168e33835e
message: test
time: 2017-11-28 16:57:44.000
__v:0
我想從“消息集合”中獲取每個用戶的消息,消息集合中的userId是用戶集合中用戶的對象ID。
從兩個以上的集合中獲取文檔的最佳方法是什么?
您必須執行查找查詢,並且該查詢僅適用於MongoDB> 3.2+。 更多信息在這里
如果mongodb上具有3.4或更高版本,請使用查找
$lookup:
{
from: <collection to join>,
localField: <field from the input documents>,
foreignField: <field from the documents of the "from" collection>,
as: <output array field>
}
有關更多參考,請點擊此處$ Lookup
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.