{
"_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
]
}
}
}
}
}
}
])
例子MongoPlayground