【发布时间】:2021-11-25 22:53:17
【问题描述】:
我是 SQL 和 MongoDB 的新手。我正在尝试转换:
SELECT accountType, ROUND(AVG(balance), 2) avgBalance
FROM customers
WHERE gender="female"
GROUP BY accountType
HAVING COUNT(*) < 140
ORDER BY avgBalance
LIMIT 1
到 MongoDB 但我无法让它工作。我不太明白顺序($group、$match、$project、$round、$avg 等)应该如何以及“ROUND 和 AVG”如何一起使用。答案应该是这样的: { "accountType" : "account-type", "avgBalance" : NumberDecimal("9999.99") }
这是我目前所拥有的:
db.customers.aggregate( [ { $group: { _id: { accountType: "accountType", avgBalance: { $avg: { "balance" } } }, { $match: { count: { $lt: 140 } } }, { gender: "female" }, { $project: { "accountType": { $round: [ $agv: "balance", 2 ] } } }, { $limit: 1 } ] )
【问题讨论】: