我怎么只能从MongoDB打印今天的预订?



我试过

我是程序员新手。我的学习计划是时间表。

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

但不仅仅是2021-04-06。

所有的预订日期都打印出来了。

全部失败。。。

我使用NodeJS,express,mongoDB,mongose。

帮帮我。

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

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

使用聚合查询

使用$unvent可分解为各个文档,并使用$match按日期进行筛选。

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

最新更新