如何在 Nodejs 中進行查找以獲取不包括給定 ID 的值。
{"_id":
{"$oid": "1"},
"image_name": "img1.jpg",
"price": 120,
"user": [{
"$oid": "userid1"
}],
"category": [{
"$oid": "cat1"
}]
"__v": 1
}
{"_id":
{"$oid": "2"},
"image_name": "img2.jpg",
"price": 120,
"user": [{
"$oid": "userid2"
}],
"category": [{
"$oid": "cat2"
}]
"__v": 1
}
{"_id":
{"$oid": "3"},
"image_name": "img3.jpg",
"price": 120,
"user": [{
"$oid": "userid3"
}],
"category": [{
"$oid": "cat3"
}]
"__v": 1
}
例如,如果用戶 ID 為 2,我想排除 ID 為 2 的用戶并獲取所有其他資料。像
{"_id":
{"$oid": "1"},
"image_name": "img1.jpg",
"price": 120,
"user": [{
"$oid": "userid1"
}],
"category": [{
"$oid": "cat1"
}]
"__v": 1
}
{"_id":
{"$oid": "3"},
"image_name": "img3.jpg",
"price": 120,
"user": [{
"$oid": "userid3"
}],
"category": [{
"$oid": "cat3"
}]
"__v": 1
}
如上。請幫忙。我嘗試了該$expr, $not, $in方法,但它拋出了一個錯誤,即
val[key] = val[key].map(v => _castExpression(v, schema, strictQuery)); TypeError: val[key].map 不是函式
編輯:下面是我嘗試過的代碼并遇到了上述錯誤
app.get("/get-images", (req, res) => {
image
.find(
{
$expr: {
$not: {
$in: [{
_id: req.query.userId
}]
}
}
}
)
.populate("user")
.populate("category")
.then((img) => {
res.status(200).send(img);
});
});
uj5u.com熱心網友回復:
試試這個
image.find({
_id: { $ne: req.query.userId }
})
$ne會給你所有_id不等于給定值的檔案。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/478317.html
