我是MongoDB的新手。我在编写以下过滤器逻辑时遇到了问题。你能帮我一把吗。=>
如果isNightParty:true,签入DB for isNightParty :true并返回其他所有文档,不要检查
这是我的代码:
router.get("/hotelList", (req,res) =>{
const {date, boys, girls, isNightParty} = req.body
businessUser.find(
{
$and:[
{isBlockedOn:{$ne:date}},
//{girlsWithBoys:isGirlsWithBoys},
{"isNightPartyAllowed":{$exists:true}}
}
).then((toListHotels)=>{
return (res.status(200).json(toListHotels))
})
.catch((err)=>{
console.log(err)
})
})我的DB结构如下:
{
"_id": {
"$oid": "6182603cce3b3b92991fee96"
},
"hotelName": "Hotel",
"email": "hotel1@hotel.com",
"password": "$2a$12$RXr2xTbnGP2ASUTqP2ptQOtS36QOr0.uq/OHTuIRLigYUM..T3lb6",
"location": "url",
"address": "address",
"isNightPartyAllowed": true,
"girlsWithBoys": true,
"roomSmallData": {
"smallPrice": "1000",
"smallPic": "url",
"smallCapacity": {
"$numberInt": "5"
},
"_id": {
"$oid": "6182603cce3b3b92991fee97"
}
},
"roomMediumData": {
"mediumPrice": "2000",
"mediumPic": "url",
"mediumCapacity": {
"$numberInt": "7"
},
"_id": {
"$oid": "6182603cce3b3b92991fee98"
}
},
"roomLargeData": {
"largePrice": "3000",
"largePic": "url",
"largeCapacity": {
"$numberInt": "10"
},
"_id": {
"$oid": "6182603cce3b3b92991fee99"
}
},
"__v": {
"$numberInt": "0"
},
"isBlockedOn": [
"Fri Nov 05 2021 00:00:00 GMT+0530 (India Standard Time)"
]
}发布于 2021-11-03 14:08:51
使用$or来满足以下两个条件:
isNightParty为空isNightPartyAllowed为真,这两种情况都应该允许返回文档。db.collection.aggregate([
{
$match: {
$expr: {
$or: [
// put your parameter <isNightParty> here
{
$eq: [
null,
<isNightParty>
]
},
{
$eq: [
{
"$ifNull": [
"$isNightPartyAllowed",
false
]
},
true
]
}
]
}
}
}
])这是供您参考的蒙戈游乐场。
https://stackoverflow.com/questions/69825279
复制相似问题