[英]Querying with mongoose/mongoDB on nested document
I have a car model given as below 我有如下车型
{ "_id": "54b8a71843286774060b8bed", "name": "Car1", "active": true, "model": [ { "name": "Model1", "active": true, "_id": "54b8a71843286774060b8bee", "available": [ { "Day": "Mon", "quantity": "6" }, { "Day": "Tue", "quantity": "6" }, { "Day": "Wed", "quantity": "6" }, { "Day": "Thurs", "quantity": "6" }, { "Day": "Fri", "quantity": "0" } ] }, { "name": "Model2", "active": true, "_id": "54b8a71843286774060b8bef", "available": [ { "Day": "Mon", "quantity": "6" }, { "Day": "Tue", "quantity": "6" }, { "Day": "Wed", "quantity": "6" }, { "Day": "Thurs", "quantity": "6" }, { "Day": "Fri", "quantity": "6" } ] }, { "name": "Model3", "active": true, "_id": "54b8a71843286774060b8beg", "available": [ { "Day": "Mon", "quantity": "6" }, { "Day": "Tue", "quantity": "6" }, { "Day": "Wed", "quantity": "6" }, { "Day": "Thurs", "quantity": "6" }, { "Day": "Fri", "quantity": "0" } ] } ] }
I am trying to search availability of car on given days. 我正在尝试搜索给定日期的汽车可用性。 Like if I select Friday then it should return me cars whose quantity more than 0 on Friday but currently it is returning all the cars having quantity 0 as well. 就像如果我选择“星期五”,那么它应该在周五退还我数量大于0的汽车,但目前它还在退还所有数量为0的汽车。
I have written query as below 我写了如下查询
Car.find({ 'active': true, 'model.available': { $elemMatch: { quantity: {$gte : 1} } } }) Car.find({'active':true,'model.available':{$ elemMatch:{数量:{$ gte:1}}}})
But it returning documents those are having quantity 0 also. 但是它也返回数量为0的那些文件。
For this, you'll need the aggregation pipeline . 为此,您将需要聚合管道 。
The following code snippet does this: 以下代码段可实现此目的:
{name: "Car1": 1, models: [{name: "Model1"}, {name: "Model2"}, {name: "Model3"}]}
{name: "Car1", models: {name: "Model1"}}
& {name: "Car1", models: {name: "Model2"}}
& {name: "Car1", models: {name: "Model3"}}
. {name: "Car1", models: {name: "Model1"}}
& {name: "Car1", models: {name: "Model2"}}
& {name: "Car1", models: {name: "Model3"}}
。 db.cars.aggregate([
// First, we do your query, which will return
// every document that has _at least one_
// model that is available.
{$match: {
'active': true,
'model.available': {$elemMatch: {
'quantity': {$gte: 1},
'Day': 'Fri'
}}
}},
// We split up the found documents,
// every document will now have exactly
// one 'model' in it.
{$unwind: "$model"},
// We now filter the split documents.
{$match: {
'model.available': {$elemMatch: {
'quantity': {$gte: 1},
'Day': 'Fri'
}}
}},
// If you want, you can now glue the
// models back together again.
{$group: {
_id: "$_id", // Group all documents with the same _id
name: {$first: "$name"},
active: {$first: "$active"},
model: {$push: "$model"} // Make an array of models
}}
])
Important note: For $gte
to work, you'll need to store your quantity as a Number, not a String. 重要说明:为使$gte
起作用,您需要将数量存储为数字而不是字符串。 Since your example has the numbers stored as strings, you might want to double check them in your database. 由于您的示例将数字存储为字符串,因此您可能需要仔细检查数据库中的数字。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.