根据键值进行计数聚合,使键成为mongoDB中的值
说明:我有几个文档,我想根据 typ
进行计数,并希望制作一个 name
和 date_key
列表。最后我们有一个文档,它计算typ
。请查看预期输出。
备注
-
typ
value是一个变量。它只会统计变量值出现的次数。 -
data
键包含一个元素,例如仅日期。但我们需要键作为值,键可以是多个,目前有2个键,可以是3个或更多。看到预期的输出键显示为列表["el1", "el2]
[
{
"typ": "ABC",
"data": [
{
"name": "XYZ",
"in_data": {
"date": {
"el2": "2015-01-10",
"el1": "2014-01-10"
},
"version" : "0.22",
"model" : "2015"
}
}
]
},
{
"typ": "ABC",
"data": [
{
"name": "LMNO",
"in_data": {
"date": {
"el2": "2015-01-10",
"el1": "2014-01-10"
},
"version" : "0.22",
"model" : "2014"
}
}
]
},
{
"typ": "EDC",
"data": [
{
"name": "QWERTY",
"in_data": {
"date": {
"el3": "2015-01-10",
"el4": "2014-01-10"
},
"version" : "0.52",
"model" : "2010"
}
}
]
}
]
预期输出
[
{
"typ_count" : 2,
"name" : ["XYZ", "LLMNO"],
"date_key" : ["el1", "el2"]
},
{
"typ_count" : 1,
"name" : ["QWERTY"],
"date_key" : ["el3", "el4"]
},
]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
鉴于
data
字段是一个仅包含一个元素的数组,您只需使用$unwind
,$first
或$arrayElemAt
运算符,但会推荐最后两个运算符中的任何一个以实现最佳聚合管道操作。您的聚合管道基本上应该有 4 个阶段:初始阶段是转换 (< code>$addFields pipeline)将
data
键插入嵌入文档中,因为它作为数组的设计是多余的,因为它仅有一个元素,最好把它压平,这样它就变成了嵌入文档。下一个管道应该是另一个转换操作,您可以在其中使用
$map< /code>
和
$objectToArray
创建date
键的列表。从那里您需要
$group
使用$sum
并使用 date 键列表以及name
列表://docs.mongodb.com/manual/reference/operator/aggregation/push/" rel="nofollow noreferrer">$push
。最终管道使用
$reduce
和$concatArrays
$cond
过滤掉重复项在上面生成的 date_key 列表中。您的整体管道应如下所示
Mongo Playground
Given that the
data
field is an array that contains only ONE element, you just need to flatten it either by using$unwind
,$first
or$arrayElemAt
operators but would recommend any of the last two operators for an optimal aggregation pipeline operation.Your aggregation pipeline should have essentially 4 stages: the initial stage is to transform (
$addFields
pipeline) thedata
key into an embedded document since its design as an array is redundant as it ONLY has one element, better flatten it so that it becomes an embedded document.The next pipeline should be another transformation operation where you use
$map
and$objectToArray
to create a list of thedate
keys.From there you need to
$group
the documents to aggregate thetyp_count
using$sum
and create another list of thedate
keys along with thename
list using$push
.The final pipeline uses
$reduce
and$concatArrays
with a$cond
to filter out duplicates in the date_key list produced above.Your overall pipeline should be as follows
Mongo Playground