【发布时间】:2020-03-05 17:01:45
【问题描述】:
我有 2 个收藏:
用户集合:
{
_id: ObjectId
}
多多收藏:
{
_id: ObjectId
fieldA: {_id: ObjectId}
fieldB: [{_id: ObjectId}, {_id: ObjectId}]
}
我正在尝试加入 fieldA 的 id 或 fieldB 的 id 之一与 user's id 匹配的 A 集合的元素以下聚合:
db.users.aggregate([
{
$lookup: {
from: "Toto",
let: { "user_id": "$_id" },
as: "fcollection",
pipeline: [
{ $match:
{$or:[
{"fieldB": {$elemMatch: {"_id": "$$user_id"}}},
{$expr: {$eq :["$fieldA._id", "$$user_id"]}}
]}
}
]
}
])
但在输出中,我只是获得了与 fieldA 的 id 匹配但没有 fieldB 匹配的文档。我做错了什么?
【问题讨论】:
标签: mongodb mongodb-query nosql aggregation-framework