我在一个集合中有数千个文档。下面是一个示例文档
{
_id: 12345,
createdDate: "2020-07-10",
cars: [
{
type: "Sedan",
engine: [
{
type: "Petrol",
power: 150,
brake: {
type: "Disc",
hasABS: false
}
},
{
type: "Petrol",
power: 190,
brake: {
type: "Drum",
hasABS: false
}
},
{
type: "Diesel",
power: 160,
brake: {
type: "Disc",
hasABS: true
}
}
]
},
{
type: "SUV",
engine: [
{
type: "Petrol",
power: 150,
brake: {
type: "Disc",
hasABS: false
}
},
{
type: "Petrol",
power: 190,
brake: {
type: "Drum",
hasABS: false
}
},
{
type: "Diesel",
power: 160,
brake: {
type: "Disc",
hasABS: true
}
}
]
}
]
}
现在我想找到在 7 月份创建的汽车并聚合以找到带有 abs 刹车的汽车
以下是我的查询:
db.getCollection('collection')
.find({
$and: [
{"createdDate": {"$gte": new Date("2020-07-01"), "$lt": new Date("2020-07-10")}},
aggregate(
{"$unwind": "$cars"},
{"$unwind": "$cars.engine"},
{"$match": {"cars.engine.brake.hasABS": true}},
{"$project": {"cars": 1}},
{"$group": {"_id": "$cars"}}
)
]
})
.pretty()
当我尝试运行上述查询时,出现错误。这是应该怎么做还是有更好的方法?