在mongodb中获得平均值
所以我有这个集合名bankdata,我有这个信息在mongodb中获得平均值,mongodb,mongodb-query,Mongodb,Mongodb Query,所以我有这个集合名bankdata,我有这个信息 { "_id" : ObjectId("574462ffbb61fc35622bd1f3"), "first_name" : "GEORGE", "last_name" : "SMITH", "accounts" : [ { "account_type" : "Checking", "account_balance" : 7510049.716676093, "currency" : "PESO" }, { "account_type" : "I
{ "_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" } ] }
我想得到账户余额大于1000000.00的所有账户的平均账户余额
我做了这个查询,但我不知道如何得到平均帐户余额
db.bankdata.find({ accounts: { $elemMatch : { 'account_balance' : { $gt: 1000000 } } } }, { 'accounts.$' : 1, first_name: 1, last_name: 1} )
您可能可以使用聚合框架。但老实说,你应该停下来想想你到底想做什么 我只是匆匆瞥了一眼你问题的细节,但它已经打动了我 你可能需要考虑重新设计你的模型。可能
account\u type
应该是对账户类型文档的引用?可能科目
应该是对科目文档的引用。你把所有的东西都放在一个文件里,但对我来说,它看起来不对劲。如果你愿意,你可以这样做,但你肯定可以做得更好
否则,我建议使用一个数据库,该数据库可以存储十进制数而不会丢失您需要/定义的精度,并且提供ACID约束,因为您必须处理事务。您可以通过这样的分组获得平均值。(我刚刚创建了一个虚拟集合。您可能需要相应地修改属性名称) 但我也会根据Elyasin的意见重新考虑 包含有关上述命令使用的内容的详细信息。 首先为每个元素解构数组,因为您需要对它们进行分组。 然后在pipleline中添加匹配项,使余额大于“a”值。 最后在一个元素上分组(我在这里使用firstname)$avg是mongo命令
db.bank.aggregate([{$unwind:"$accounts"},{$match:{"accounts.balance":{"$gt":35555}}},{$group:{_id:{name:"$firstname"},avgbalance:{$avg:"$accounts.balance"}}}])