[英]Mongodb: Find in another collection, if data not found in first collection
I want to find data from either of 2 collections.我想从 2 个 collections 中的任何一个中查找数据。 If the data is not found in first, it must look into the second collection.
如果在第一个集合中没有找到数据,它必须查看第二个集合。
I tried to use $lookup with aggregation pipeline, but somehow, it won't work in the present case.我尝试将 $lookup 与聚合管道一起使用,但不知何故,它在目前的情况下不起作用。
I am using Mongoose here (version 4.10.8)我在这里使用 Mongoose(版本 4.10.8)
I have DestinationPage as one collection, where I find a document matching with destinationPage_name , if I don't get the data (will return null, if so) then I wish to find it in SpecialityPage collection matching with specialityPage_name .我有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 above code gives error as,上面的代码给出了错误,
"The 'cursor' option is required, except for aggregate with the explain argument"
If I add { cursor:{} }
to it, I don't see even console printing.如果我添加
{ cursor:{} }
,我什至看不到控制台打印。
OPTION: I can check data in one collection, then if I don't get any data, then in callback, I can check another collection.选项:我可以检查一个集合中的数据,然后如果我没有得到任何数据,那么在回调中,我可以检查另一个集合。 However I don't consider it as a efficient way of doing.
但是我不认为这是一种有效的方式。
Thanks.谢谢。
'#stayhomestaysafe' '#stayhomestaysafe'
Your mongoose version is not compatible with MongoDB v4.10.8
.您的 mongoose 版本与 MongoDB
v4.10.8
不兼容。
You need to upgrade to the recent version v5.9.9
您需要升级到最新版本
v5.9.9
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.