Mongodb嵌套数组聚合
我的数据库中有这种情况: 注意:所有ID都是由mongo自动生成的 酒店架构(为了简洁起见,我删除了额外字段) 这些酒店的一些房间有一系列的可用日期 订单模式Mongodb嵌套数组聚合,mongodb,Mongodb,我的数据库中有这种情况: 注意:所有ID都是由mongo自动生成的 酒店架构(为了简洁起见,我删除了额外字段) 这些酒店的一些房间有一系列的可用日期 订单模式 order: Schema no: { type: String }, hotels: [ { hotel: { type: Schema.Types.ObjectId, ref: 'Hotels', required: true }, rooms: [
order: Schema
no: { type: String },
hotels: [
{
hotel: { type: Schema.Types.ObjectId, ref: 'Hotels', required: true },
rooms: [
{
roomReferenceId: { type: String, required: true },
arrivalDate: { type: Date, required: true },
departureDate: { type: Date, required: true },
}
]
}
]
任何订单都可以有一个或多个酒店。订单中的每个酒店都有一个房间数组,其中包含一个参考ID和日期
问题是:
如何实现对这些模式进行聚合并显示的单个查询
- 所有酒店、所有客房及入住日期
- 如果酒店的房间被预订,以某种方式显示预订的日期
提前感谢您提供的帮助或提示。您可以在mongodb聚合管道中使用$lookup,并在一个查询中加入两个集合我不知道如何将子阵列(在酒店房间)与订单子阵列(订单->酒店->房间)连接起来
order: Schema
no: { type: String },
hotels: [
{
hotel: { type: Schema.Types.ObjectId, ref: 'Hotels', required: true },
rooms: [
{
roomReferenceId: { type: String, required: true },
arrivalDate: { type: Date, required: true },
departureDate: { type: Date, required: true },
}
]
}
]