Mongo Group Document属性由2个字段进行数据
我有一个具有以下结构的文档:
{
_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"
},
]
}
我想创建一个查询,以查找文档的所有properties
,其中id _id
,然后按document 和
表格
属性值
示例:
{
"document": "File 1",
"result": [
{
"sheet": "sheet1",
"data": [{...}]
},
{
"sheet": "sheet2",
"data": [{...}]
},
{
"sheet": "sheet3",
"data": [{...}]
}
]
}
如何执行特定文档的属性
的分组/聚合?
我不习惯MongoDB,我唯一要做的就是获得properties
投影文档,并带有以下查询:
db.getCollection('myCollection')
.find({}, {_id:UUID("21e1fd87-6e22-4487-85d5-18e639f9b710"), properties: 1})
I have a document with the following structure:
{
_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"
},
]
}
I would like to create a query to find all the properties
of the document with ID _id
, and then group the data by document
and sheet
property values
Example:
{
"document": "File 1",
"result": [
{
"sheet": "sheet1",
"data": [{...}]
},
{
"sheet": "sheet2",
"data": [{...}]
},
{
"sheet": "sheet3",
"data": [{...}]
}
]
}
How can I perform the grouping/aggregation of the properties
of a certain document?
I am not used to MongoDB, and the only thing I managed to do is to obtain the properties
projection document with the following query:
db.getCollection('myCollection')
.find({}, {_id:UUID("21e1fd87-6e22-4487-85d5-18e639f9b710"), properties: 1})
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用聚合框架进行操作:
$ match
- 用 _id field$ untind
- 以解析 properties 数组字段$ group
- document 和表字段字段$ project
仅返回仅返回的字段您想要工作示例
You can do it with Aggregation Framework:
$match
- to filter the document with _id field$unwind
- to deconstruct properties array field$group
- to group by document and sheet fields$project
to return only the fields that you wantWorking example