首页 > 解决方案 > 我如何只能从 MongoDB 打印今天的预订?

问题描述

我试过了

我是新手编码器。我的学习项目是时间表。

我想打印一份预订清单,例如 2021-04-06。

但不仅是 2021-04-06。

所有预订日期均已打印。

都失败了……

我使用 NodeJS、express、mongoDB、mongoose。

帮我。

User Schema({
    name: user1
    clients: [
        client1,
        client2
    ]
});
Client Schema({
    name: client1
    phone: 00000000,
    gender: 1,
    reservation:[
        {
            date: 2021-04-06T00:00:00.000Z,
            time: "14:00"
            cancel: "off",
            noshow: "on",
        },
        {
            date: 2021-04-10T00:00:00.000Z,
            time: "14:00"
            cancel: "off",
            noshow: "off",
        }
    ],
    memo: String,
    user: {
        type: mongoose.Schema.Types.ObjectId,
        ref: "User"
    }
},
{
    name: client2
    phone: 00000002,
    gender: 0,
    reservation:[
        {
            date: 2021-04-06T00:00:00.000Z,
            time: "10:00"
            note: "first"
            cancel: "off",
            noshow: "on",
        },
        {
            date: 2021-04-12T00:00:00.000Z,
            time: "14:00"
            note: "second"
            createdAt: Date,
            cancel: "off",
            noshow: "off",
        }
    ],
    memo: String,
    user: user1
})

标签: node.jsmongodbmongoosemongodb-queryaggregation-framework

解决方案


演示 - https://mongoplayground.net/p/m39zdyqq8id

使用聚合查询

使用$unwind分解单个文档并使用$match按日期过滤。

db.collection.aggregate([
  { $unwind: "$reservation" },
  { $match: { "reservation.date": ISODate("2021-04-06") } }
])

推荐阅读