【发布时间】:2020-04-18 02:51:07
【问题描述】:
我想从 2 个集合中的任何一个中查找数据。如果第一个集合中没有找到数据,则必须查看第二个集合。
我尝试将 $lookup 与聚合管道一起使用,但不知何故,它在当前情况下不起作用。
我在这里使用 Mongoose(版本 4.10.8)
我将 DestinationPage 作为一个集合,我在其中找到与 destinationPage_name 匹配的文档,如果我没有获取数据(如果是,则返回 null)然后我希望在与 specialityPage_name 匹配的 SpecialityPage 集合中找到它。
DestinationPage.aggregate([
{ "$match" : { "destinationPage_name": { $regex: SEARCH_REF_DESTINATION, $options: 'i' } }},
{ "$lookup" : {
"from" : "SpecialityPage",
"localField" : "specialityPage_name",
"foreignField" : { $regex: SEARCH_REF_DESTINATION, $options: 'i' },
"as" : "data2"
}
},
]).then(doc => {
console.log('document', doc);
})
.catch(err => {
console.error("got error : ", err);
})
上面的代码给出了错误,
"The 'cursor' option is required, except for aggregate with the explain argument"
如果我在其中添加{ cursor:{} },我什至看不到控制台打印。
选项: 我可以检查一个集合中的数据,然后如果我没有得到任何数据,那么在回调中,我可以检查另一个集合。但是我不认为这是一种有效的方式。
谢谢。
'#stayhomestaysafe'
【问题讨论】:
标签: node.js mongodb mongoose aggregation-framework