gpt4 book ai didi

mongodb - $lt 100 of $sum 使用聚合

转载 作者:可可西里 更新时间:2023-11-01 10:19:56 26 4
gpt4 key购买 nike

我正在尝试使用 $sum 计算每个州的总数。然后使用 $lt 仅显示小于 100 的状态。

每笔总和:

 db.zipcode.aggregate([{$group:{_id:"$state", count_of_cities:{$sum:1}}}])

尝试显示小于 100 的总和:

 db.zipcode.aggregate([{$group:{_id:"$state"}, less_than:{$cond:{if:{$lt:[$sum:1,100]}}}}])

样本收集:

 { "_id" : "01001", "city" : "AGAWAM", "loc" : [ -72.622739, 42.070206 ], "pop" : 15338, "state" : "MA" }
{ "_id" : "05042", "city" : "RYEGATE", "loc" : [ -72.072669, 44.193453 ], "pop" : 328, "state" : "VT" }
{ "_id" : "05043", "city" : "EAST THETFORD", "loc" : [ -72.19668, 43.825757 ], "pop" : 657, "state" : "VT" }
{ "_id" : "08052", "city" : "MAPLE SHADE", "loc" : [ -74.99464399999999, 39.951085 ], "pop" : 19365, "state" : "NJ" }

整个系列都可以在 MongoDB 官方网站上找到:http://media.mongodb.org/zips.json

最佳答案

你可以执行:

  • 1 $group 用于将城市汇总为 count_of_cities
  • 1 $match 过滤您的金额值

查询是:

db.zipcode.aggregate([{
$group: { _id: "$state", count_of_cities: { $sum: 1 } }
}, {
$match: {
count_of_cities: { $lt: 100 }
}
}])

关于mongodb - $lt 100 of $sum 使用聚合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40333815/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com