mongodb advance complex multi filter query

2019-08-26 04:53发布

this is my model:

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 }
);

and sample document:

"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
                    }
                ]
            },
   }

i know this is very confused query, but i need this, thanks for any help, query or reference

update:
this data should return calendar for query between two date:

"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"
                    }
                ]

and this data should return nothing, because one of date 2019-07-03 all room in rooms price or total is zero:

"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"
                    }
                ]

update: remove unnecessary code

0条回答
登录 后发表回答