[英]MongoDB aggregate from adjustable foreign collections
My documents in the orders collection has _client
key, which is an ObjectId references to another entity in another collection.我在 orders 集合中的文档有_client
键,它是对另一个集合中另一个实体的 ObjectId 引用。 The collection could be organization and could be users - I mean - it's variable collection.集合可以是组织,也可以是用户- 我的意思是 - 它是变量集合。 I want to tell Mongo to lookup if the _client id is found in both collections.我想告诉 Mongo 查找是否在两个集合中都找到了 _client id。
{
$lookup: {
from: "users", // could be "organizations"
let: { "client": "$_client" }, // could be "_organization"
pipeline: [
{ $match: { $expr: { $eq: ["$_id", "$$client"] }}},
],
as: "client"
}
},
{
$unwind: "$client"
},
I have tried to just set up two look ups, once for _client and one for _organization however when there one of them is missing, I just got no results at all.我试图刚刚成立两个看起坐,一次_client,一个用于_组织结构其中有一个缺失然而,当我刚刚都没有结果。
$unwind
filtered out the documents where arrays are empty and do not contain any element. $unwind
过滤掉数组为空且不包含任何元素的文档。
So, You have to use preserveNullAndEmptyArrays
and set it to true
所以,你必须使用preserveNullAndEmptyArrays
并将其设置为true
{ "$unwind": { "path": "$client", "preserveNullAndEmptyArrays": true }}
and same for the or organizations
和organizations
相同
{ "$unwind": { "path": "$organization", "preserveNullAndEmptyArrays": true }}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.