我只想知道如何在1 Note.find( )
内连接这两个查询
- 第一部分:
Note.find({ creator: id })
- 第二部分:
Note.find({ $or: [{ tags: { $regex: req.params.key } }],})
整个代码块:
exports.searchUserNotes = (req, res) => {
const id = req.userId;
Note.find({ creator: id })
.then(
Note.find({
$or: [{ tags: { $regex: req.params.key } }],
})
)
.then((data) => {
if (data.length === 0) {
res.status(404).json({
Error: `No notes with the '${req.params.key}' tag were found.`,
});
} else {
res.status(200).json({
Success: `${data.length} notes with the '${req.params.key}' tag were found.`,
data,
});
}
});
};
1条答案
按热度按时间aoyhnmkz1#
您可以使用
$and
运算符联接两个查询。