【问题标题】:Getting average in mongodb在 mongodb 中达到平均水平
【发布时间】:2016-05-24 22:26:56
【问题描述】:

所以我有这个集合名称bankdata,我有这个信息

{ "_id" : ObjectId("574462ffbb61fc35622bd1f3"), "first_name" : "GEORGE", "last_name" : "SMITH", "accounts" : [ { "account_type" : "Checking", "account_balance" : 7510049.716676093, "currency" : "PESO" }, { "account_type" : "Investment", "account_balance" : 8978088.436168617, "currency" : "USD" }, { "account_type" : "401K", "account_balance" : 3536582.866462197, "currency" : "YEN" }, { "account_type" : "Checking", "account_balance" : 4390095.46173461, "currency" : "USD" } ] }

{ "_id" : ObjectId("574462ffbb61fc35622bd1f4"), "first_name" : "STEVEN", "last_name" : "SMITH", "accounts" : [ { "account_type" : "Investment", "account_balance" : 7606194.17605087, "currency" : "EURO" }, { "account_type" : "Investment", "account_balance" : 2099711.775821766, "currency" : "EURO" }, { "account_type" : "Investment", "account_balance" : 2961011.973269031, "currency" : "YUAN" }, { "account_type" : "Savings", "account_balance" : 5830306.873168334, "currency" : "USD" }, { "account_type" : "401K", "account_balance" : 1542091.8696738125, "currency" : "EURO" } ] }

{ "_id" : ObjectId("574462ffbb61fc35622bd1f5"), "first_name" : "BRIAN", "last_name" : "SMITH", "accounts" : [ { "account_type" : "401K", "account_balance" : 149839.10500178766, "currency" : "PESO" } ] }

{ "_id" : ObjectId("574462ffbb61fc35622bd1f6"), "first_name" : "EDWARD", "last_name" : "SMITH", "accounts" : [ { "account_type" : "401K", "account_balance" : 6577381.434625924, "currency" : "YEN" }, { "account_type" : "Checking", "account_balance" : 8998935.759297138, "currency" : "USD" }, { "account_type" : "Investment", "account_balance" : 588000.4045217587, "currency" : "YUAN" }, { "account_type" : "Savings", "account_balance" : 6743563.754405159, "currency" : "YUAN" }, { "account_type" : "401K", "account_balance" : 8580650.627761671, "currency" : "POUNDS STERLING" }, { "account_type" : "401K", "account_balance" : 7687815.685781645, "currency" : "YEN" }, { "account_type" : "Checking", "account_balance" : 9128407.633252997, "currency" : "EURO" } ] }

我想获取账户余额大于 1,000,000.00 的所有账户的平均账户余额。

我做了这个查询,但我不知道如何获得平均账户余额。

db.bankdata.find({ accounts: { $elemMatch : {  'account_balance' : { $gt: 1000000 } } } }, { 'accounts.$' : 1, first_name: 1, last_name: 1} )

【问题讨论】:

    标签: mongodb mongodb-query


    【解决方案1】:

    您也许可以使用 聚合 框架。但说实话,你应该停下来想想你到底想做什么。

    我只是快速浏览了您的问题详细信息,但它已经吸引了我。

    1. 您可能需要考虑重新设计模型。也许account_type 应该是对帐户类型文档的引用?也许accounts 应该是对帐户文件的引用。您将所有内容都放在一个文件中,但对我来说它看起来不正确。如果您愿意,您可以这样做,但您绝对可以做得更好。
    2. 第二个也是最重要的。 MongoDB根本不适合金融应用。这是因为浮点数在 MongoDB 存储它们的格式中会丢失精度;您必须阅读浮点数才能了解其影响。底线是浮点数有时无法以您希望它具有的确切值存储。当您总结浮点值时,这会很明显,并且您的结果是错误的。

    我建议您退后一步,考虑一下您想要实现的目标。如果是出于学习目的,那么您只是学到了一些东西。
    否则,我建议使用可以存储十进制数字而不会丢失您需要/定义的精度并且提供 ACID 约束的数据库,因为您必须处理事务。

    【讨论】:

      【解决方案2】:

      您可以通过这样的分组来获得平均值。 (我刚刚创建了一个虚拟集合。您可能需要相应地修改属性名称)

       db.bank.aggregate([{$unwind:"$accounts"},{$match:{"accounts.balance":{"$gt":35555}}},{$group:{_id:{name:"$firstname"},avgbalance:{$avg:"$accounts.balance"}}}])
      

      但我也会根据 Elyasin 的意见重新考虑。

      https://docs.mongodb.com/manual/reference/operator/aggregation/unwind/

      详细说明了上述命令的用途。 首先解构每个元素的数组,因为您需要对它们进行分组。 然后在管道中添加 Match 以使余额大于“a”值。 最后在一个元素上分组(我在这里使用了名字) $avg 是 mongo 命令。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-03-24
        • 1970-01-01
        • 2022-01-19
        • 1970-01-01
        相关资源
        最近更新 更多