计数数字大于MongoDB阵列中的某个数字

发布于 2025-02-13 11:22:57 字数 532 浏览 0 评论 0原文

{
    "_id" : ObjectId(""),
    "CustomerId" : 13038,
    "AT" : ISODate("2021-12-01T04:00:00.000Z"),
    "dwell" : [ 
        7, 
        6, 
        12, 
        6     ]
},
{
    "_id" : ObjectId(""),
    "CustomerId" : 12036,
    "AT" : ISODate("2021-12-01T04:00:00.000Z"),
    "dwell" : [ 
        15, 
        3, 
        12
    ]
}

在这些文档中,我只想获取大于10的停留数字的计数。

例如:

{"CustomerId": 13038, "Count": 1} //because only 12 bigger than 10
{"CustomerId": 12036, "Count": 2}
{
    "_id" : ObjectId(""),
    "CustomerId" : 13038,
    "AT" : ISODate("2021-12-01T04:00:00.000Z"),
    "dwell" : [ 
        7, 
        6, 
        12, 
        6     ]
},
{
    "_id" : ObjectId(""),
    "CustomerId" : 12036,
    "AT" : ISODate("2021-12-01T04:00:00.000Z"),
    "dwell" : [ 
        15, 
        3, 
        12
    ]
}

In these documents, I only want to get the count of the numbers in the dwell which are greater than 10.

For Example:

{"CustomerId": 13038, "Count": 1} //because only 12 bigger than 10
{"CustomerId": 12036, "Count": 2}

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

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

发布评论

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

评论(1

像你 2025-02-20 11:22:58

您可以使用 $ size size

db.collection.aggregate([
  {
    $project: {
      _id: 0,
      CustomerId: 1,
      Count: {
        "$size": {
          "$filter": {
            "input": "$dwell",
            "as": "num",
            "cond": {
              $gt: [
                "$num",
                10
              ]
            }
          }
        }
      }
    }
  }
])

示例mongoplayground

You could do something like this using $size and $filter:

db.collection.aggregate([
  {
    $project: {
      _id: 0,
      CustomerId: 1,
      Count: {
        "$size": {
          "$filter": {
            "input": "$dwell",
            "as": "num",
            "cond": {
              $gt: [
                "$num",
                10
              ]
            }
          }
        }
      }
    }
  }
])

Example MongoPlayground

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