在 MongoDB 中分组并添加到 Group
Posted
技术标签:
【中文标题】在 MongoDB 中分组并添加到 Group【英文标题】:Grouping in MongoDB and adding to Group 【发布时间】:2021-03-14 07:14:59 【问题描述】:我有一个MeterReadings
集合,如下所示。
"_id" : ObjectId("5fc768b33561870a262813c6"),
"installedAppId" : "A",
"readings" : [
"n" : "daf43d66-6c3b-4553-80af-6a0b1cf97418:power",
"t" : 1606902984662,
"u" : "W",
"v" : 100
]
"_id" : ObjectId("5fc768c73561870a262813c7"),
"installedAppId" : "B",
"readings" : [
"n" : "daf43d66-6c3b-4553-80af-6a0b1cf97418:power",
"t" : 1606902984662,
"u" : "W",
"v" : 200
]
"_id" : ObjectId("5fc768e43561870a262813c8"),
"installedAppId" : "A",
"readings" : [
"n" : "daf43d66-6c3b-4553-80af-6a0b1cf97418:power",
"t" : 1606902984672,
"u" : "W",
"v" : 300
]
我想要的输出是按installedAppId
分组,然后将每个readings
从所有匹配的installedAppId
中附加到一个条目中。下面显示的是我的目标。
"_id" : ObjectId("5fc768b33561870a262813c6"),
"installedAppId" : "A",
"readings" : [
"n" : "daf43d66-6c3b-4553-80af-6a0b1cf97418:power",
"t" : 1606902984662,
"u" : "W",
"v" : 100
,
"n" : "daf43d66-6c3b-4553-80af-6a0b1cf97418:power",
"t" : 1606902984672,
"u" : "W",
"v" : 300
]
"_id" : ObjectId("5fc768c73561870a262813c7"),
"installedAppId" : "B",
"readings" : [
"n" : "daf43d66-6c3b-4553-80af-6a0b1cf97418:power",
"t" : 1606902984662,
"u" : "W",
"v" : 200
]
按installedAppId
分组确实会使用上述数据返回两个组。
> db.MeterReadings.aggregate([ $group: _id: installedAppId: "$installedAppId" ])
"_id" : "installedAppId" : "B"
"_id" : "installedAppId" : "A"
由于每个读数都不同,尽管在 $group
中添加 readings
作为另一个条目与查询整个数据库相同。
> db.MeterReadings.aggregate([ $group: _id: installedAppId: "$installedAppId", readings: "$readings" ]).pretty()
"_id" :
"installedAppId" : "A",
"readings" : [
"n" : "daf43d66-6c3b-4553-80af-6a0b1cf97418:power",
"t" : 1606902984672,
"u" : "W",
"v" : 10.2
]
"_id" :
"installedAppId" : "B",
"readings" : [
"n" : "daf43d66-6c3b-4553-80af-6a0b1cf97418:power",
"t" : 1606902984662,
"u" : "W",
"v" : 10.2
]
"_id" :
"installedAppId" : "A",
"readings" : [
"n" : "daf43d66-6c3b-4553-80af-6a0b1cf97418:power",
"t" : 1606902984662,
"u" : "W",
"v" : 10.2
]
欢迎任何帮助!
【问题讨论】:
【参考方案1】:$push
或 $addToSet
似乎都可以解决问题,但是为每个添加数组。如果可以只推送到一个数组会更好。
addToSet
> db.MeterReadings.aggregate([$group : _id : "$installedAppId", readings: $addToSet : "$readings"]).pretty()
"_id" : "B",
"readings" : [
[
"n" : "daf43d66-6c3b-4553-80af-6a0b1cf97418:power",
"t" : 1606902984672,
"u" : "W",
"v" : 200
]
]
"_id" : "A",
"readings" : [
[
"n" : "daf43d66-6c3b-4553-80af-6a0b1cf97418:power",
"t" : 1606902984672,
"u" : "W",
"v" : 300
],
[
"n" : "daf43d66-6c3b-4553-80af-6a0b1cf97418:power",
"t" : 1606902984672,
"u" : "W",
"v" : 100
]
]
推
> db.MeterReadings.aggregate([$group : _id : "$installedAppId", readings: $push: "$readings"]).pretty()
"_id" : "B",
"readings" : [
[
"n" : "daf43d66-6c3b-4553-80af-6a0b1cf97418:power",
"t" : 1606902984672,
"u" : "W",
"v" : 200
]
]
"_id" : "A",
"readings" : [
[
"n" : "daf43d66-6c3b-4553-80af-6a0b1cf97418:power",
"t" : 1606902984672,
"u" : "W",
"v" : 100
],
[
"n" : "daf43d66-6c3b-4553-80af-6a0b1cf97418:power",
"t" : 1606902984672,
"u" : "W",
"v" : 300
]
]
【讨论】:
以上是关于在 MongoDB 中分组并添加到 Group的主要内容,如果未能解决你的问题,请参考以下文章
按月对记录进行分组并计数 - Mongoose、nodeJs、mongoDb
MongoDB/Mongoose 一对多,在子节点中引用父节点 - 分组并加入