I want to find data from either of 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.
I am using Mongoose here (version 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.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.
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'
Your mongoose version is not compatible with MongoDB v4.10.8
.
You need to upgrade to the recent version v5.9.9
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.