mongodb-如何在结果嵌入式文档中分组不同的字段
我有一个文档,我想通过某些特定字段进行分组,对于不包含在GroupBy中的其余字段,我想将它们包括在嵌入式数组中。
一个示例,假设我有以下数据,
[
{"owner": "A", "pet": "dog", "pet_name": "wouf"},
{"owner": "A", "pet": "cat", "pet_name": "miaou"},
{"owner": "B", "pet": "dog", "pet_name": "wouf_wouf"},
]
我想通过字段所有者
和剩下的字段pet
pet_name ,我想将它们分组为名为动物
的数组。
预期结果:
[
{
"owner": "A",
"animals": [
{"pet": "dog", "pet_name": "wouf"},
{"pet": "cat", "pet_name": "miaou"},
],
},
{
"owner": "B",
"animals": [{"pet": "dog", "pet_name": "wouf_wouf"}]
},
]
I have a document that I want to group by some specific fields, and for the remaining fields that are not included in the groupby, I want to include them in an embedded array.
An example, let's say that I have the following data
[
{"owner": "A", "pet": "dog", "pet_name": "wouf"},
{"owner": "A", "pet": "cat", "pet_name": "miaou"},
{"owner": "B", "pet": "dog", "pet_name": "wouf_wouf"},
]
I want to group this data by the field owner
and the remaining fields pet
and pet_name
, I want to group them in an array named animals
.
Expected result:
[
{
"owner": "A",
"animals": [
{"pet": "dog", "pet_name": "wouf"},
{"pet": "cat", "pet_name": "miaou"},
],
},
{
"owner": "B",
"animals": [{"pet": "dog", "pet_name": "wouf_wouf"}]
},
]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
查询
$所有者
和$ push
pet/pet_name
_id
所有者
并删除_id
playmongo
Query
$owner
, and$push
pet/pet_name
_id
toowner
and remove the_id
Playmongo