【发布时间】:2014-07-01 10:01:40
【问题描述】:
我正在使用这个问题How to filter array in subdocument with MongoDB的方法
它按预期工作,除非数组中没有任何元素与测试匹配。在这种情况下,我只会得到一个没有父数据的空数组。
样本数据
{
"_id": "53712c7238b8d900008ef71c",
"dealerName": "TestDealer",
"email": "test@test.com",
"address": {..},
"inventories": [
{
"title": "active",
"vehicles": [
{
"_id": "53712fa138b8d900008ef720",
"createdAt": "2014-05-12T20:08:00.000Z",
"tags": [
"vehicle"
],
"opts": {...},
"listed": false,
"disclosures": {...},
"details": {...}
},
{
"_id": "53712fa138b8d900008ef720",
"createdAt": "2014-05-12T20:08:00.000Z",
"tags": [...],
"opts": {...},
"listed": true,
"disclosures": {...},
"details": {...}
}
]
},
{
"title": "sold",
"vehicles": []
}
]
}
努力去做
在我的查询中,我想返回用户(文档)顶级信息(经销商姓名、电子邮件)和一个名为车辆的属性,其中包含“活动”库存中列出的属性设置为 true 的所有车辆.
我走了多远
这是我的查询。 (我使用 Mongoose,但主要使用原生 Mongo 功能)
{
$match:
email: params.username
}
{
$unwind: '$inventories'
}
{
$match:
'inventories.title': 'active'
}
{
$unwind:
'$inventories.vehicles'
}
{
$match:
'inventories.vehicles.listed':
$eq: true
}
{
$group:
_id: '$_id'
dealerName:
$first: '$dealerName'
email:
$first: '$email'
address:
$first: '$address'
vehicles:
$push: '$inventories.vehicles'
}
问题
起初,我认为我的查询很好,但是,如果没有车辆被标记为listed,则查询只会返回一个空数组。这是有道理的,因为
{
$match:
'inventories.vehicles.listed':
$eq: true
}
不匹配任何内容,但我仍想获取经销商名称以及他的电子邮件
没有车辆匹配时的期望输出
[{"dealerName": "TestDealer", "email": "test@test.com", vehicles : []}]
实际输出
[]
【问题讨论】:
标签: javascript arrays mongodb mongoose