我想计算 nodejs mongodb 列中的三个字段,需要按月分组
Posted
技术标签:
【中文标题】我想计算 nodejs mongodb 列中的三个字段,需要按月分组【英文标题】:I want to count three fields in a nodejs mongodb column and need to group it by month 【发布时间】:2021-06-22 13:07:21 【问题描述】:我想统计每个月green,red,amber
的数量(即:按月分组),如下例所示:
_id:2,Green:20,Red:12,Amber:23
说_id代表月份(2:Feb)
注意:- 我正在从 mongodb 检索数据,并且日期列是 ISO 日期类型格式
谁能帮帮我?
【问题讨论】:
如果您的日期实际上不是一个字符串,正如您最初的问题所暗示的那样,您最好在节点中完成整个操作。 【参考方案1】:演示 - https://mongoplayground.net/p/0N7b2uNZJNL
db.collection.aggregate(
"$project":
color: 1,
month:
"$substr": [ "$date", 0, "$indexOfCP": [ "$date", "/" ] // extract month from date string
]
,
"$group":
"_id": month: "$month", color: "$color" ,
"count": "$sum": 1
,
"$project":
month: "$_id.month",
details: [ color: "$_id.color", count: "$count" ] // add color count to the array
,
$project:
_id: 0,
month: 1,
color:
$arrayToObject: // get object from array like green: 1
$map:
input: "$details", as: "pair", in: [ "$$pair.color", "$$pair.count" ]
,
"$group":
"_id": "$month",
"color": "$mergeObjects": "$color" // merge all the colors
,
$replaceWith:
$mergeObjects: [ _id: "$_id" , "$color" ] // bring color out of the object
)
$substr
$indexOfCP
$arrayToObject
$mergeObjects
$replaceWith
【讨论】:
谢谢,它运行良好,而且我只想从中检索最近三个月 @hariram 使用 $sort 和 $limit【参考方案2】:试试这个查询:
db.colors.aggregate([
$group:
_id:
month: $month: "$Date" ,
color: "$Rag"
,
count: $sum: 1
,
$group:
_id: "$_id.month",
array:
$push:
k: "$_id.color",
v: "$count"
,
$replaceRoot:
newRoot:
$arrayToObject:
$concatArrays: [[ k: "_id", v: "$_id" ], "$array"]
]);
问题中附加的测试数据的输出:
"_id" : 3,
"Red" : 1,
"Amber" : 14,
"Green" : 7
colors
集合中的数据:
[
"_id": ObjectId("..."),
"Date": ISODate("2021-03-16T00:00:00.000+05:30"),
"Rag": "Green"
,
"_id": ObjectId("..."),
"Date": ISODate("2021-03-16T00:00:00.000+05:30"),
"Rag": "Green"
,
"_id": ObjectId("..."),
"Date": ISODate("2021-03-16T00:00:00.000+05:30"),
"Rag": "Green"
,
"_id": ObjectId("..."),
"Date": ISODate("2021-03-16T00:00:00.000+05:30"),
"Rag": "Green"
,
"_id": ObjectId("..."),
"Date": ISODate("2021-03-16T00:00:00.000+05:30"),
"Rag": "Amber"
,
"_id": ObjectId("..."),
"Date": ISODate("2021-03-16T00:00:00.000+05:30"),
"Rag": "Green"
,
"_id": ObjectId("..."),
"Date": ISODate("2021-03-16T00:00:00.000+05:30"),
"Rag": "Green"
,
"_id": ObjectId("..."),
"Date": ISODate("2021-03-16T00:00:00.000+05:30"),
"Rag": "Green"
,
"_id": ObjectId("..."),
"Date": ISODate("2021-03-16T00:00:00.000+05:30"),
"Rag": "Red"
,
"_id": ObjectId("..."),
"Date": ISODate("2021-03-16T00:00:00.000+05:30"),
"Rag": "Amber"
,
"_id": ObjectId("..."),
"Date": ISODate("2021-03-16T00:00:00.000+05:30"),
"Rag": "Amber"
,
"_id": ObjectId("..."),
"Date": ISODate("2021-03-16T00:00:00.000+05:30"),
"Rag": "Amber"
,
"_id": ObjectId("..."),
"Date": ISODate("2021-03-16T00:00:00.000+05:30"),
"Rag": "Amber"
,
"_id": ObjectId("..."),
"Date": ISODate("2021-03-16T00:00:00.000+05:30"),
"Rag": "Amber"
,
"_id": ObjectId("..."),
"Date": ISODate("2021-03-16T00:00:00.000+05:30"),
"Rag": "Amber"
,
"_id": ObjectId("..."),
"Date": ISODate("2021-03-16T00:00:00.000+05:30"),
"Rag": "Amber"
,
"_id": ObjectId("..."),
"Date": ISODate("2021-03-16T00:00:00.000+05:30"),
"Rag": "Amber"
,
"_id": ObjectId("..."),
"Date": ISODate("2021-03-16T00:00:00.000+05:30"),
"Rag": "Amber"
,
"_id": ObjectId("..."),
"Date": ISODate("2021-03-16T00:00:00.000+05:30"),
"Rag": "Amber"
,
"_id": ObjectId("..."),
"Date": ISODate("2021-03-16T00:00:00.000+05:30"),
"Rag": "Amber"
,
"_id": ObjectId("..."),
"Date": ISODate("2021-03-16T00:00:00.000+05:30"),
"Rag": "Amber"
,
"_id": ObjectId("..."),
"Date": ISODate("2021-03-16T00:00:00.000+05:30"),
"Rag": "Amber"
]
【讨论】:
以上是关于我想计算 nodejs mongodb 列中的三个字段,需要按月分组的主要内容,如果未能解决你的问题,请参考以下文章
如何从 nodejs 中的多步表单发布数据并表达到 MongoDB?
如何对 mongoose/mongodb 和 nodejs 中的对象数组进行排序?
我们如何使用带有 expressjs-nodejs 的 mongoose 对 mongodb 中的 ObjectId 列执行排序?