卓越飞翔博客卓越飞翔博客

卓越飞翔 - 您值得收藏的技术分享站
技术文章34282本站已运行391

检索 MongoDB 聚合上每个日期的项目计数

检索 mongodb 聚合上每个日期的项目计数

问题内容

我有一个包含事件的集合。每个事件都有精确到毫秒的时间戳。我想将每天的事件分组。 例如:

我有

key       value

_id       111222333444555ddd666fff
time      2023-04-23t15:35:19.631z
type      pizza-event

_id       111222333444555ddd666fff
time      2023-04-23t01:41:20.631z
type      tv-event

_id       111222333444555ddd666fff
time      2023-04-22t05:00:05.631z
type      some-event

我愿意

key        value

date       04-22-2023
count      1.0

date       04-23-2023
count      2.0

最终目标是在 golang 项目中使用查询。

到目前为止我已经

[
        {
            "$match" : {
                "$and" : [
                    {
                        "type" : "foo.bar.event"
                    },
                    {
                        "time" : {
                            "$gte" : isodate("2023-04-23t00:00:00.000+0000")
                        }
                    },
                    {
                        "time" : {
                            "$lte" : isodate("2023-04-25t00:00:00.000+0000")
                        }
                    }
                ]
            }
        }, 
        {
            "$group" : {
                "_id" : {
                    "$datetostring" : {
                        "format" : "%m-%d-%y",
                        "date" : "$time"
                    }
                },
                "count" : {
                    "$sum" : 1.0
                }
            }
        }
    ]

返回

key            value

_id            04-24-2023
count          476.0

_id            04-23-2023
count          28.0

这本来可以工作,但是当我在 go 项目中编写此查询时,“$datetostring”下会出现红色波浪线,并显示消息“无效的字段名称”,理想情况下我希望日期有一个“时间”键而不是“_id”。当我在小组赛阶段进行以下更改时:

{
    _id: null,
    "date": {"$dateToString": { "format": "%m-%d-%Y", "date": "$time"}},
    "count": {"$sum": 1}
}

我收到未知组运算符“$datetostring”错误。因此,我想到创建组,然后为“$datetostring”添加一个项目阶段,但现在小组阶段每毫秒都会返回组,这违背了分组的目的。

我意识到我正在讨论两个不同的问题。然而,虽然一切方面的帮助都会很棒,但这个问题具体是关于修复 mongo 查询的。如果有必要,我会在另一个线程上返回 golang 编码。 如果我能说得更清楚,请告诉我。


正确答案


首先,如果您有一个应用程序 ui 来显示查询结果,则不必费心格式化查询中的输出。这是应用程序 ui 的职责。顺便说一句,如果您有应用程序 ui,请考虑使用 $datetrunc 而不是 $datetostring

无论如何,关于您问题中的要求,像这样的 $project 阶段应该适合您:

[
  {
    "$group": {
      "_id": {
        "$datetostring": { "date": "$time", "format": "%m-%d-%y" }
      },
      "count": { "$sum": 1 }
    }
  },
  { "$project": { "_id": 0, "time": "$_id", "count": "$count" } }
]

mongodb shell 的输出:

{ "time" : "02-08-2020", "count" : 2 }
{ "time" : "05-18-2020", "count" : 2 }
{ "time" : "03-20-2021", "count" : 3 }
{ "time" : "01-11-2021", "count" : 1 }

关于在go项目中使用查询,这里有一个演示:

package main

import (
    "context"
    "fmt"
    "time"

    "go.mongodb.org/mongo-driver/bson"
    "go.mongodb.org/mongo-driver/mongo"
    "go.mongodb.org/mongo-driver/mongo/options"
)

func main() {
    ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second)
    defer cancel()
    client, err := mongo.Connect(ctx, options.Client().ApplyURI("mongodb://localhost:27017"))
    if err != nil {
        panic(err)
    }

    coll := client.Database("baz").Collection("cakeSales")

    matchStage := bson.D{
        {"$match", bson.D{
            {"$and", []bson.D{
                {{"time", bson.D{
                    {"$gte", time.Date(2019, 6, 1, 0, 0, 0, 0, time.UTC)},
                }}},
                {{"time", bson.D{
                    {"$lte", time.Date(2021, 2, 1, 0, 0, 0, 0, time.UTC)},
                }}},
            }},
        }},
    }

    groupStage := bson.D{
        {"$group", bson.D{
            {"_id", bson.D{
                {"$dateToString", bson.D{
                    {"date", "$time"},
                    {"format", "%m-%d-%Y"},
                }},
            }},
            {"count", bson.D{
                {"$sum", 1},
            }},
        }},
    }

    projectStage := bson.D{
        {"$project", bson.D{
            {"_id", 0},
            {"time", "$_id"},
            {"count", "$count"},
        }},
    }

    cursor, err := coll.Aggregate(context.TODO(),
        mongo.Pipeline{matchStage, groupStage, projectStage})
    if err != nil {
        panic(err)
    }

    var results []bson.M
    if err = cursor.All(context.TODO(), &results); err != nil {
        panic(err)
    }
    for _, result := range results {
        fmt.Printf(
            "time: %s count: %vn",
            result["time"],
            result["count"])
    }
}
卓越飞翔博客
上一篇: 我的神经网络(从头开始)训练,让它离目标更远
下一篇: 返回列表
留言与评论(共有 0 条评论)
   
验证码:
隐藏边栏