mongodb高级复杂多过滤器查询

时间:2019-07-02 10:39:05

标签: mongodb aggregate

这是我的模特:

const calendarSchema = new mongoose.Schema(
    {
        hotel: {
            type: mongoose.Schema.ObjectId,
            ref: "Hotel",
            required: true
        },
        city: {
            type: mongoose.Schema.ObjectId,
            ref: "City"
        },
        calendar: [
            {
                date: Date,
                rooms: [
                    {
                        room: {
                            type: mongoose.Schema.ObjectId,
                            ref: "Room",
                            required: true
                        },
                        price: {
                            type: Number
                        },
                        total: {
                            type: Number
                        },
                        reserved: {
                            type: Number
                        }
                    }
                ]
            }
        ]
    },
    { timestamps: true, versionKey: false }
);

和示例文档:

"calendar": {
        "_id": "5cd26a886458720f7a66a3b8",
        "hotel": "5cd02fe495be1a4f48150447",
        "calendar": [
            {
                "_id": "5cd26a886458720f7a66a413",
                "date": "1970-01-01T00:00:00.001Z",
                "rooms": [
                    {
                        "_id": "5cd26a886458720f7a66a415",
                        "room": "5cd17d82ca56fe43e24ae5d3",
                        "price": "",
                        "total": 0,
                        "reserved": 0
                    },
                    {
                        "_id": "5cd26a886458720f7a66a414",
                        "room": "5cd17db6ca56fe43e24ae5d4",
                        "price": "",
                        "total": 0,
                        "reserved": 0
                    }
                ]
            },
   }

我知道这是一个非常混乱的查询,但是我需要这个, 感谢您的帮助,查询或参考

更新
此数据应返回两个日期之间的查询日历:

"calendar": [
                    {
                        "rooms": [
                            {
                                "_id": "5d11edeb9d679218dcb25fd1",
                                "room": "5d11eddd3def481d26d14b88",
                                "price": 180000,
                                "total": 50,
                                "reserved": 0
                            },
                            {
                                "_id": "5d11edeb9d679218dcb25fd0",
                                "room": "5d11eddd3def481d26d14b91",
                                "price": 180000,
                                "total": 50,
                                "reserved": 0
                            }
                        ],
                        "date": "2019-07-01T19:30:00.000Z"
                    },
                    {
                        "rooms": [
                            {
                                "_id": "5d11edeb9d679218dcb25fcd",
                                "room": "5d11eddd3def481d26d14b88",
                                "price": 180000,
                                "total": 50,
                                "reserved": 0
                            },
                            {
                                "_id": "5d11edeb9d679218dcb25fcc",
                                "room": "5d11eddd3def481d26d14b91",
                                "price": 180000,
                                "total": 50,
                                "reserved": 0
                            }
                        ],
                        "date": "2019-07-02T19:30:00.000Z"
                    },
                    {
                        "rooms": [
                            {
                                "_id": "5d11edeb9d679218dcb25fc9",
                                "room": "5d11eddd3def481d26d14b88",
                                "price": 180000,
                                "total": 50,
                                "reserved": 0
                            },
                            {
                                "_id": "5d11edeb9d679218dcb25fc8",
                                "room": "5d11eddd3def481d26d14b91",
                                "price": 180000,
                                "total": 50,
                                "reserved": 0
                            }
                        ],
                        "date": "2019-07-03T19:30:00.000Z"
                    },
                    {
                        "rooms": [
                            {
                                "_id": "5d11edeb9d679218dcb25fc5",
                                "room": "5d11eddd3def481d26d14b88",
                                "price": 180000,
                                "total": 50,
                                "reserved": 0
                            },
                            {
                                "_id": "5d11edeb9d679218dcb25fc4",
                                "room": "5d11eddd3def481d26d14b91",
                                "price": 180000,
                                "total": 50,
                                "reserved": 0
                            }
                        ],
                        "date": "2019-07-04T19:30:00.000Z"
                    },
                    {
                        "rooms": [
                            {
                                "_id": "5d11edeb9d679218dcb25fc1",
                                "room": "5d11eddd3def481d26d14b88",
                                "price": 180000,
                                "total": 50,
                                "reserved": 0
                            },
                            {
                                "_id": "5d11edeb9d679218dcb25fc0",
                                "room": "5d11eddd3def481d26d14b91",
                                "price": 180000,
                                "total": 50,
                                "reserved": 0
                            }
                        ],
                        "date": "2019-07-05T19:30:00.000Z"
                    }
                ]

,该数据应不返回任何内容,因为日期2019-07-03中房间price or total中所有房间之一为零:

"calendar": [
                    {
                        "rooms": [
                            {
                                "_id": "5d11edeb9d679218dcb25fd1",
                                "room": "5d11eddd3def481d26d14b88",
                                "price": 180000,
                                "total": 50,
                                "reserved": 0
                            },
                            {
                                "_id": "5d11edeb9d679218dcb25fd0",
                                "room": "5d11eddd3def481d26d14b91",
                                "price": 180000,
                                "total": 50,
                                "reserved": 0
                            }
                        ],
                        "date": "2019-07-01T19:30:00.000Z"
                    },
                    {
                        "rooms": [
                            {
                                "_id": "5d11edeb9d679218dcb25fcd",
                                "room": "5d11eddd3def481d26d14b88",
                                "price": 180000,
                                "total": 50,
                                "reserved": 0
                            },
                            {
                                "_id": "5d11edeb9d679218dcb25fcc",
                                "room": "5d11eddd3def481d26d14b91",
                                "price": 180000,
                                "total": 50,
                                "reserved": 0
                            }
                        ],
                        "date": "2019-07-02T19:30:00.000Z"
                    },
                    {
                        "rooms": [
                            {
                                "_id": "5d11edeb9d679218dcb25fc9",
                                "room": "5d11eddd3def481d26d14b88",
                                "price": 0,
                                "total": 50,
                                "reserved": 0
                            },
                            {
                                "_id": "5d11edeb9d679218dcb25fc8",
                                "room": "5d11eddd3def481d26d14b91",
                                "price": 180000,
                                "total": 0,
                                "reserved": 0
                            }
                        ],
                        "date": "2019-07-03T19:30:00.000Z"
                    },
                    {
                        "rooms": [
                            {
                                "_id": "5d11edeb9d679218dcb25fc5",
                                "room": "5d11eddd3def481d26d14b88",
                                "price": 180000,
                                "total": 50,
                                "reserved": 0
                            },
                            {
                                "_id": "5d11edeb9d679218dcb25fc4",
                                "room": "5d11eddd3def481d26d14b91",
                                "price": 180000,
                                "total": 50,
                                "reserved": 0
                            }
                        ],
                        "date": "2019-07-04T19:30:00.000Z"
                    },
                    {
                        "rooms": [
                            {
                                "_id": "5d11edeb9d679218dcb25fc1",
                                "room": "5d11eddd3def481d26d14b88",
                                "price": 180000,
                                "total": 50,
                                "reserved": 0
                            },
                            {
                                "_id": "5d11edeb9d679218dcb25fc0",
                                "room": "5d11eddd3def481d26d14b91",
                                "price": 180000,
                                "total": 50,
                                "reserved": 0
                            }
                        ],
                        "date": "2019-07-05T19:30:00.000Z"
                    }
                ]

更新: 删除不必要的代码

0 个答案:

没有答案