{
    "_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}

推荐答案

您可以使用$size$filter执行类似操作:

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

Example MongoPlayground

Mongodb相关问答推荐

如何限制/筛选子文档中的条目?

MongoDB 聚合 - $project 和 $match 阶段未按预期工作

使用Go的Mongo驱动程序,从MongoDB文档中获取元素并更新其值需要帮助

Mongoose updateMany 以及 Where 和 orWhere?

mongo shell 命令不接受使用 db 命令

如何使用 mongoose 从 MongoDb 获取数据?

无法使用机器 ip 连接到 mongodb

声明多个模式后无法从数据库中获取数据(mongoose + express + mongodb

在 mongoDB 中存储 java 8 LocalDate

MongoDB获取聚合查询的executionStats

MongoDB:单个数据库处理程序的 >5 个打开连接

什么是 mongodb 中的admin数据库?

在 MongoDB 中按条件分组

具有简单密码认证的 MongoDB 副本集

如果我在 MongoDB 上使用 LINQ,为什么会失go 性能?

如何使用 MongoDB 以编程方式预拆分基于 GUID 的分片键

NumberLong和简单整数之间的MongoDB区别?

MongoDB:查询和检索嵌入式数组中的对象?

Mongoose:查询starts with

通过浏览器连接mongodb?