node.js - 我如何只能从 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
})
解决方案
演示 - https://mongoplayground.net/p/m39zdyqq8id
使用聚合查询
使用$unwind分解单个文档并使用$match按日期过滤。
db.collection.aggregate([
{ $unwind: "$reservation" },
{ $match: { "reservation.date": ISODate("2021-04-06") } }
])
推荐阅读
- node.js - NodeJS:通过 API 发出更少的请求
- php - Symfony 4 在 html.twig 中的选民检查角色
- sql - 通过加入具有不同行数和多列的数据框来填充空值
- reactjs - 如何多次使用Dispatch async?
- python-3.x - TypeError: unhashable type: 'Bunch' 从 ipywidgets 下拉菜单中选择值后出现
- python - 使用 3D 列表填充 Excel 文件 - Python
- android - android 设备上的 Flutter API 调用不起作用
- sql - 如何在 WHERE 条件下使用 HAVING 子句选择所有数据?
- reactjs - KeyCloak React refreshToken 过期令牌
- c# - 转换为 foreach 迭代的 LINQ 在 VS Asp.Net 2019 .Net 4.8 中产生编译错误