I have two collections, Movies and Shows.
const ShowsSchema = new Schema({
title: {
type: String,
required: true
},
description: {
type: String,
required: true
},
image: {
type: String,
required: true
})
Currently I'm only able to search data from one collection but I want to fetch data from both. After looking through some solutions on the website I came across the $lookup field.
router.get('/:query', (req, res, next) => {
const query = req.params.query;
Show.aggregate([{
$lookup: {
from: "Movies",
localField: "title",
foreignField: "title",
as: "movies"
}
}], () => {
Show.find({
title: {
$regex: new RegExp(query),
$options: "$i"
},
}, (err, shows) => {
if (err) {
console.error(err);
}
res.json(shows);
});
});
});
I'm stuck here any help would be greatly appreciated.
router.get('/:query', (req, res, next) => {
const query = req.params.query;
Show.aggregate([{
$match : {
title: {
$regex: new RegExp(query),
$options: "$i"
}
}
},{
$lookup: {
from: "Movies",
localField: "title",
foreignField: "title",
as: "movies"
}
}],(err, shows) => {
if (err) {
console.error(err);
}
res.json(shows);
});
});
Hope this is helpfull.!
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.