【问题标题】:MongoDB Log Aggregation for ChartjsChartjs 的 MongoDB 日志聚合
【发布时间】:2023-03-24 19:58:01
【问题描述】:

集合由日志数据组成。

日志

[
    { "Module": "Admin",
        "UserEmail": "tony@dne.com",
        "Completed": "2020-02-29T01:21:24.128+00:00" },
    { "Module": "Admin",
        "UserEmail": "ricky@dne.com",
        "Completed": "2020-02-29T01:21:24.128+00:00" },
    { "Module": "Home",
        "UserEmail": "ricky@dne.com",
        "Completed": "2020-02-29T01:21:24.128+00:00" },
    { "Module": "Admin",
        "UserEmail": "santa@dne.com",
        "Completed": "2020-02-29T01:21:24.128+00:00" },
    { "Module": "Contact",
        "UserEmail": "tony@dne.com",
        "Completed": "2020-02-29T01:21:24.128+00:00" },
    { "Module": "Contact",
        "UserEmail": "santa@dne.com",
        "Completed": "2020-02-29T01:21:24.128+00:00" },
    { "Module": "Admin",
        "UserEmail": "ricky@dne.com",
        "Completed": "2020-02-29T01:21:24.128+00:00" },
    { "Module": "Home",
        "UserEmail": "tony@dne.com",
        "Completed": "2020-02-29T01:21:24.128+00:00" }
]

预期结果

[
    {
        "Module": "Admin",
        "Count":4,
        "Logs": [
            {
                "UserEmail": "tony@dne.com",
                "Completed": "2020-02-29T01:21:24.128+00:00"
            },
            {
                "UserEmail": "ricky@dne.com",
                "Completed": "2020-02-29T01:21:24.128+00:00"
            },
            {
                "UserEmail": "santa@dne.com",
                "Completed": "2020-02-29T01:21:24.128+00:00"
            },
            {
                "UserEmail": "ricky@dne.com",
                "Completed": "2020-02-29T01:21:24.128+00:00"
            }
        ]
    },
    {
        "Module": "Home",
        "Count":2,
        "Logs": [
            {
                "UserEmail": "ricky@dne.com",
                "Completed": "2020-02-29T01:21:24.128+00:00"
            },
            {
                "UserEmail": "tony@dne.com",
                "Completed": "2020-02-29T01:21:24.128+00:00"
            }
        ]
    },
    {
        "Module": "Contact",
        "Count":2,
        "Logs": [
            {
                "UserEmail": "tony@dne.com",
                "Completed": "2020-02-29T01:21:24.128+00:00"
            },
            {
                "UserEmail": "santa@dne.com",
                "Completed": "2020-02-29T01:21:24.128+00:00"
            }
        ]
    }
]

需要对日志进行聚合以获取模块的使用计数和基于用户的时间序列 grpah 以获得额外信息。预期结果将用于从 chartjs 绘制折线图。

在 c# mongodb 客户端中寻找为上述问题构建的聚合管道。

【问题讨论】:

  • 这就是使用$group 的东西。
  • this,但看起来你想要一个特定于mongodb的解决方案。
  • 这很好用。只需要从这里创建一个 mongo 聚合管道

标签: c# mongodb aggregation-framework pipeline


【解决方案1】:

您可以使用以下聚合管道来获得所需的内容:

db.collection.aggregate([
  {
    $group: {
      _id: "$Module",
      Count: {
        $sum: 1
      },
      Logs: {
        $push: {
          UserEmail: "$UserEmail",
          "Completed": "$Completed"
        }
      }
    }
  },
  {
    $project: {
      _id: 0,
      Module: "$_id",
      Count: 1,
      Logs: 1
    }
  }
])

这里的$project 阶段只是为了将_id 重命名为Module,如果它不相关,您可以将其删除。

你可以测试一下here

【讨论】:

  • 太棒了。我希望我早点知道 mongo 游乐场。 +1
猜你喜欢
  • 2011-06-07
  • 2016-08-23
  • 2020-09-16
  • 2020-10-27
  • 1970-01-01
  • 2019-10-29
  • 2020-01-26
  • 2019-01-24
  • 1970-01-01
相关资源
最近更新 更多