这段代码展示了我是如何在 Pymongo 中做到这一点的,但是如果我想在 RMongo 中做到这一点呢?
from pymongo import MongoClient
connection = MongoClient()
db = connection.mydb
collection = db.transactions
pipe = [{'$group': {'_id': 'TRANS_AMNT_AVG', 'average': {'$avg':'$TRANS_AMOUNT'}}}]
result = collection.aggregate(pipeline=pipe)
print(list(result))
connection.close()
我的数据集被称为transactions
,列被称为TRANS_AMOUNT
。我想计算平均值TRANS_AMOUNT
。