【发布时间】:2015-06-04 19:27:47
【问题描述】:
我在我的 MongoDB 中得到了下面图解的 JSON。我收到了几百个条目。问题是其中一些在嵌入式数组中有多个条目。在此图中,不允许使用数组类型的 entry_2 和以下 entry_2 对象。我想删除类型数组名称值为“entry_2”的两个对象之一。
{
"id": null,
"name": "",
"array" [
{
"name": "entry_1"
},
{
"name": "entry_2"
},
{
"name": "entry_2"
},
{
"name": "entry_3"
}
]
}
因此,我的 JSON 在 Query 之后应该如下所示:
{
"id": null,
"name": "",
"array" [
{
"name": "entry_1"
},
{
"name": "entry_2"
},
{
"name": "entry_3"
}
]
}
我尝试浏览 SO 并阅读 http://docs.mongodb.org/manual/tutorial/query-documents/#exact-match-on-the-embedded-document,但找不到解决方案。
-- 编辑--
我必须使用选项 { allowDiskUse: true } 并且不知道如何在查询中实现它。此外,我尝试将查询调整为我的特定用例,其中我得到了以下结构:
{
"_id": {
"$oid": "556ccf6f59bbda5ea20a8884"
},
"id": 1159,
"description": "Cheese, goat, soft type",
"tags": [],
"manufacturer": "",
"group": "Dairy and Egg Products",
"portions": [
{
"unit": "oz",
"grams": 28.35,
"amount": 1
}
],
"nutrients": [
{
"description": "Protein",
"group": "Composition",
"value": 18.52,
"units": "g"
},
{
"group": "Composition",
"value": 21.08,
"units": "g",
"description": "Total lipid (fat)"
},
{
"description": "Protein",
"group": "Composition",
"value": 18.52,
"units": "g"
}
]
}
根据我尝试过的以下答案:
var pipeline = [
{
"$unwind": "$nutrients"
},
{
"$group": {
"_id": "$_id",
"id": { "$first": "$id" }
"description": { "$first": "$description" },
"tags" : { "$first": "$tags" },
"manufacturer" : { "$first": "$manufacturer" },
"group" : { "$first": "$group" },
"portions" : { "$first": "$portions" },
"nutrients": {
"$addToSet": "$nutrients"
}
}
}
],
options = { "allowDiskUse": true };
db.collection.aggregate(pipeline, options);
我收到错误消息:“意外字符串”。 我想这与“_id”对象和“tags”数组有关。
【问题讨论】:
-
您收到错误是因为您的管道在
$group运算符表达式中的"id": { "$first": "$id" }表达式之后缺少逗号,。
标签: mongodb