我只想知道如何將這兩個查詢加入只有 1Note.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,
});
}
});
};
uj5u.com熱心網友回復:
您可以使用$and運算子來連接兩個查詢。
Note.find({
$and: [
{ creator: id },
{ $or: [{ tags: { $regex: req.params.key } }] }
]
})
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/521149.html
上一篇:如何將字串引數轉換為列名
