【发布时间】:2015-06-20 07:17:30
【问题描述】:
所以我有一个如下所示的 mongodb 文档:
[
{
"_id": "04",
"name": "test service 4",
"id": "04",
"version": "0.0.1",
"title": "testing",
"description": "test",
"protocol": "test",
"operations": [
{
"_id": "99",
"oName": "test op 52222222222",
"sid": "04",
"name": "test op 52222222222",
"oid": "99",
"description": "testing",
"returntype": "test",
"parameters": [
{
"oName": "Param1",
"name": "Param1",
"pid": "011",
"type": "582",
"description": "testing",
"value": ""
},
{
"oName": "Param2",
"name": "Param2",
"pid": "012",
"type": "58222",
"description": "testing",
"value": ""
}
]
}
]
}
]
我希望能够找到所有参数和一个单独的参数,但我不完全确定如何使用它:
collection.find({operations: {$elemMatch: {oid: oid}}}, {"operations.$.parameters": 1}).toArray(function(error, result) {
if (error) {
console.log('Error retrieving parameter: ' + error);
res.send({'error':'An error has occurred'});
} else {
// console.log(result);
res.send(result);
}
});
我得到的输出是这样的:
[
{
"_id": "04",
"operations": [
{
"_id": "99",
"oName": "test op 52222222222",
"sid": "04",
"name": "test op 52222222222",
"oid": "99",
"description": "testing",
"returntype": "test",
"parameters": [
{
"oName": "Param1",
"name": "Param1",
"pid": "011",
"type": "582",
"description": "testing",
"value": ""
}
]
}
]
}
]
即使我唯一想要的是单个参数。有什么办法可以完成吗?
【问题讨论】:
标签: node.js mongodb api nested