Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 将查找变量设置为表达式的值_Javascript_Mongodb_Mongodb Query_Aggregation Framework - Fatal编程技术网

Javascript 将查找变量设置为表达式的值

Javascript 将查找变量设置为表达式的值,javascript,mongodb,mongodb-query,aggregation-framework,Javascript,Mongodb,Mongodb Query,Aggregation Framework,这是我的notecolection文档的示例 { 标题:“一些标题”, 主体:“描述主体”, 出资人:[ { 出资人ID:ObjectId(5353ff535f5f3a5) } ]//由于某些原因,此字段必须是一个对象数组 } 现在,当使用$lookup来“查找”贡献者时,我需要贡献者ID字段 问题是,我不知道如何用MongoDB查询方式来表达contributedBy[0].contributorId //查找聚合 { 来自:'贡献者', 作为‘贡献者’, let:{'contributor

这是我的
notecolection
文档的示例

{
标题:“一些标题”,
主体:“描述主体”,
出资人:[
{
出资人ID:ObjectId(5353ff535f5f3a5)
}
]//由于某些原因,此字段必须是一个对象数组
}
现在,当使用
$lookup
来“查找”贡献者时,我需要
贡献者ID
字段

问题是,我不知道如何用MongoDB查询方式来表达contributedBy[0].contributorId

//查找聚合
{
来自:'贡献者',
作为‘贡献者’,
let:{'contributorUserId':'$contributedBy[0].contributorId'},//这里,如何获取'contributedBy'字段中第0个元素的'contributorId'属性的值
管道:[
{$match:{
$expr:{$eq:['$\u id','$$contributorUserId']}
}}   
]
}

如果您只想在
0th
索引处通过一个
contributedBy.contributorId
加入这两个集合,请使用运算符:

db.notecolection.aggregate([
{
$lookup:{
来自:'贡献者',
作为‘贡献者’,
让:{
“contributorUserId”:{
$arrayElemAt:['$contributedBy.contributorId',0]
}
},
管道:[
{
$match:{
$expr:{$eq:['$\u id','$$contributorUserId']}
}
}
]
}
}
])
如果要将两个集合w.r.t加入到它们的引用
\u id
s,请使用运算符:

db.notecolection.aggregate([
{
$lookup:{
来自:'贡献者',
作为‘贡献者’,
让:{
“ContributorUserId”:“$contributedBy.contributorId”
},
管道:[
{
$match:{
$expr:{$in:['$\u id','$$ContributorUserId']}
}
}
]
}
}
])