My model looks like this:
我的模型看起来像这样:
Collection = new Schema({
name: {type: String, required: true},
tags: [String],
movies: [{_id: false,
original_title: {type: String}}]
)};
I need to alter the following query to use 'req.body' (an array of strings) to not only find a match in the 'tags' array but also find match(es) with the 'original_title' field in the movies array.
我需要更改以下查询以使用'req.body'(字符串数组),不仅可以在'tags'数组中找到匹配项,还可以在movies数组中找到与'original_title'字段匹配的匹配项。
Collection.find({'tags' : { $in : req.body}}, (err, collections) => {
if(err){res.status(400).json(err);}
if(!collections)
{
res.status(404).json({message: 'No collections found'});
}else{
res.json(collections);
}
}).limit(8);
1 个解决方案
#1
2
Try this:
db.collection.find({
$or: [{
'tags': {
$in: req.body
}
},
{
'original_title': {
$in: req.body
}
}
]
}, (err, collections) => {
if (err) {
res.status(400).json(err);
}
if (!collections) {
res.status(404).json({
message: 'No collections found'
});
} else {
res.json(collections);
}
}).limit(8);
#1
2
Try this:
db.collection.find({
$or: [{
'tags': {
$in: req.body
}
},
{
'original_title': {
$in: req.body
}
}
]
}, (err, collections) => {
if (err) {
res.status(400).json(err);
}
if (!collections) {
res.status(404).json({
message: 'No collections found'
});
} else {
res.json(collections);
}
}).limit(8);