所以这是我的文档,代表每天的酒店日历
"calendar": {
"_id": "5cd26a886458720f7a66a3b8",
"hotel": "5cd02fe495be1a4f48150447",
"calendar": [
{
"_id": "5cd26a886458720f7a66a413",
"date": "1970-01-01T00:00:00.001Z",
"rooms": [
{
"_id": "5cd26a886458720f7a66a415",
"room": "5cd17d82ca56fe43e24ae5d3",
"price": 100000000,
"total": 8,
"reserved": 0
},
]
},
{
"_id": "6cr26a8864578720f7a66a231",
"date": "1970-01-02T00:00:00.001Z",
"rooms": [
{
"_id": "5ij26a876458560f7a66n678",
"room": "5cd17d82ca56fe43e24ae5d3",
"price": 0,
"total": 0,
"reserved": 0
},
]
},
{
"_id": "6gt26a98464578720f7a655a002",
"date": "1970-01-03T00:00:00.001Z",
"rooms": [
{
"_id": "5ity26a873358560f7a66i88f1",
"room": "5cd17d82ca56fe43e24ae5d3",
"price": 400000,
"total": 12,
"reserved": 0
},
]
},
}
,我有这个汇总查询来获取有2个条件的酒店: 1.日期范围:开始日期,结束日期 2.每个房间的总和价格=>条件是:总和价格具有大于0的值。
查询:
Calendar.aggregate([{
$match: {
hotel: mongoose.Types.ObjectId(req.body.hotel_id)
}
},
{
$project: {
hotel: 1,
calendar: {
$filter: {
input: {
$map: {
input: "$calendar",
as: "cal",
in: {
$cond: [{
$and: [{
$gt: [
"$$cal.date",
startDate
]
},
{
$lt: [
"$$cal.date",
endDate
]
}
]
},
{
rooms: {
$filter: {
input: "$$cal.rooms",
as: "room",
cond: {
$and: [{
$gt: [
"$$room.price",
0
]
},
{
$gt: [
"$$room.total",
0
]
}
]
}
}
},
date: "$$cal.date"
},
null
]
}
}
},
as: "final",
cond: {
$size: {
$ifNull: ["$$final.rooms", []]
}
}
}
}
}
},
{
$match: {
"calendar.0": {
$exists: true
}
}
}
])
此查询仅返回该范围内大于0个房间。但是我的结果必须是: 仅返回整天大于0的房间。