Mongo 按 2 个字段对文档 属性 的数据进行分组
Mongo group document property's data by 2 fields
我有一个结构如下的文档:
{
_id: "some_id",
... ,
properties: [
{
"document": "doc1",
"sheet": "sheet1",
"property": "property1"
},
{
"document": "doc1",
"sheet": "sheet2",
"property": "property2"
},
{
"document": "doc1",
"sheet": "sheet2",
"property": "property3"
},
{
"document": "doc2",
"sheet": "sheet1",
"property": "property4"
},
]
}
我想创建一个查询来查找 ID 为 _id
的文档的所有 properties
,然后按 document
和 sheet
属性 值
示例:
{
"document": "File 1",
"result": [
{
"sheet": "sheet1",
"data": [{...}]
},
{
"sheet": "sheet2",
"data": [{...}]
},
{
"sheet": "sheet3",
"data": [{...}]
}
]
}
如何执行某个文档的properties
的grouping/aggregation?
我不习惯MongoDB,我唯一能做的就是通过以下查询获取properties
投影文档:
db.getCollection('myCollection')
.find({}, {_id:UUID("21e1fd87-6e22-4487-85d5-18e639f9b710"), properties: 1})
您可以使用聚合框架来完成:
$match
- 使用 _id 字段 过滤文档
$unwind
- 解构属性数组字段
$group
- 按文档和sheet字段 分组
$project
到 return 只有您想要的字段
db.collection.aggregate([
{
"$match": {
"_id": 1
}
},
{
"$unwind": "$properties"
},
{
"$group": {
"_id": {
"document": "$properties.document",
"sheet": "$properties.sheet"
},
"data": {
"$addToSet": "$properties.property"
}
}
},
{
"$project": {
"_id": 0,
"document": "$_id.document",
"result": {
"sheet": "$_id.sheet",
"data": "$data"
}
}
}
])
我有一个结构如下的文档:
{
_id: "some_id",
... ,
properties: [
{
"document": "doc1",
"sheet": "sheet1",
"property": "property1"
},
{
"document": "doc1",
"sheet": "sheet2",
"property": "property2"
},
{
"document": "doc1",
"sheet": "sheet2",
"property": "property3"
},
{
"document": "doc2",
"sheet": "sheet1",
"property": "property4"
},
]
}
我想创建一个查询来查找 ID 为 _id
的文档的所有 properties
,然后按 document
和 sheet
属性 值
示例:
{
"document": "File 1",
"result": [
{
"sheet": "sheet1",
"data": [{...}]
},
{
"sheet": "sheet2",
"data": [{...}]
},
{
"sheet": "sheet3",
"data": [{...}]
}
]
}
如何执行某个文档的properties
的grouping/aggregation?
我不习惯MongoDB,我唯一能做的就是通过以下查询获取properties
投影文档:
db.getCollection('myCollection')
.find({}, {_id:UUID("21e1fd87-6e22-4487-85d5-18e639f9b710"), properties: 1})
您可以使用聚合框架来完成:
$match
- 使用 _id 字段 过滤文档
$unwind
- 解构属性数组字段$group
- 按文档和sheet字段 分组
$project
到 return 只有您想要的字段
db.collection.aggregate([
{
"$match": {
"_id": 1
}
},
{
"$unwind": "$properties"
},
{
"$group": {
"_id": {
"document": "$properties.document",
"sheet": "$properties.sheet"
},
"data": {
"$addToSet": "$properties.property"
}
}
},
{
"$project": {
"_id": 0,
"document": "$_id.document",
"result": {
"sheet": "$_id.sheet",
"data": "$data"
}
}
}
])