【发布时间】:2021-03-28 11:58:52
【问题描述】:
这是我noteCollection的文档示例
{
title: "Some title",
body: "Descreptive body",
contributedBy: [
{
contributorId: ObjectId(5353ff535f5f3a5)
}
] // for some reason, this field has to be an array of objects
}
现在,在使用 $lookup“查找”贡献者时,我需要 contributorId 字段
问题是,我不知道如何用MongoDB查询方式表达contributedBy[0].contributorId。
// lookup aggregation
{
from: 'contributors',
as: 'contributor',
let: {'contributorUserId': '$contributedBy[0].contributorId'}, // here, how to get the value of `contributorId` property of the 0th element in `contributedBy` field
pipeline: [
{$match: {
$expr: {$eq: ['$_id', '$$contributorUserId']}
}}
]
}
【问题讨论】:
标签: javascript mongodb mongodb-query aggregation-framework