如何进行查询包括计数和集合数据

发布于 2025-01-28 13:21:22 字数 730 浏览 4 评论 0原文

示例数据

[
   { owner: '0x123124124', createdAt: '13 May 2022 at 04:20:21 UTC'},
   { owner: '0x123124124', createdAt: '13 May 2022 at 06:20:21 UTC'},
   { owner: '0x123124124', createdAt: '13 May 2022 at 03:20:21 UTC'},
   { owner: '0x123124124', createdAt: '12 May 2022 at 07:39:34 UTC'},
   { owner: '0x123124124', createdAt: '12 May 2022 at 07:39:34 UTC'},
   { owner: '0x454545627', createdAt: '11 May 2022 at 04:13:48 UTC'}
]

和我想查询这样的返回数据,

[
  { owner: '0x123124124', createdAt: '13 May 2022', count: 3},
  { owner: '0x123124124', createdAt: '13 May 2022', count: 2},
  { owner: '0x454545627', createdAt: '11 May 2022', count: 1},
]

我已经将其舍入数小时了几个小时。 感谢您的帮助。

Example data

[
   { owner: '0x123124124', createdAt: '13 May 2022 at 04:20:21 UTC'},
   { owner: '0x123124124', createdAt: '13 May 2022 at 06:20:21 UTC'},
   { owner: '0x123124124', createdAt: '13 May 2022 at 03:20:21 UTC'},
   { owner: '0x123124124', createdAt: '12 May 2022 at 07:39:34 UTC'},
   { owner: '0x123124124', createdAt: '12 May 2022 at 07:39:34 UTC'},
   { owner: '0x454545627', createdAt: '11 May 2022 at 04:13:48 UTC'}
]

And I want to query return data like this

[
  { owner: '0x123124124', createdAt: '13 May 2022', count: 3},
  { owner: '0x123124124', createdAt: '13 May 2022', count: 2},
  { owner: '0x454545627', createdAt: '11 May 2022', count: 1},
]

I have rounded with this for hours.
Thanks for help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一百个冬季 2025-02-04 13:21:22
  1. $ set - set 创建 with

    1.1。 $ trim - 1.1.1

    的修剪字符串

    1.1.1。 $ arrayelemat - 从 1.1.1.1

    获取第一个值

    1.1.1.1。 $ split - split 创建 word“ at”。

    的字符串

  2. $ group - 组成的组所有者创建并执行计数。

  3. $ project - 装饰输出文档。

db.collection.aggregate([
  {
    $set: {
      createdAt: {
        $trim: {
          input: {
            "$arrayElemAt": [
              {
                $split: [
                  "$createdAt",
                  "at"
                ]
              },
              0
            ]
          }
        }
      }
    }
  },
  {
    $group: {
      _id: {
        owner: "$owner",
        createdAt: "$createdAt"
      },
      count: {
        $sum: 1
      }
    }
  },
  {
    $project: {
      _id: 0,
      owner: "$_id.owner",
      createdAt: "$_id.createdAt",
      count: 1
    }
  }
])

示例mongo playground

  1. $set - Set createdAt with

    1.1. $trim - Trim string from 1.1.1.

    1.1.1. $arrayElemAt - Get the first value from 1.1.1.1.

    1.1.1.1. $split - Split createdAt string with word "at".

  2. $group - Group by owner and createdAt and perform count.

  3. $project - Decorate output documents.

db.collection.aggregate([
  {
    $set: {
      createdAt: {
        $trim: {
          input: {
            "$arrayElemAt": [
              {
                $split: [
                  "$createdAt",
                  "at"
                ]
              },
              0
            ]
          }
        }
      }
    }
  },
  {
    $group: {
      _id: {
        owner: "$owner",
        createdAt: "$createdAt"
      },
      count: {
        $sum: 1
      }
    }
  },
  {
    $project: {
      _id: 0,
      owner: "$_id.owner",
      createdAt: "$_id.createdAt",
      count: 1
    }
  }
])

Sample Mongo Playground

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文