我如何使用mongodb或robomongo计算正价和负价? [英] How can i calculate price positive and negative price using mongodb or robomongo?

查看:64
本文介绍了我如何使用mongodb或robomongo计算正价和负价?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是我的用户定价收集数据

below is my userpricing collection data

{
    "_id" : ObjectId("584bc9ba420a6b189c510af6"),
    "user_id" : 1,
    "mobilenumber":"01234",
    "price" : 2000.0,
    "type" : "credit",

},
{
    "_id" : ObjectId("584bc9ba420a6b189c510af6"),
    "user_id" : 1,
    "mobilenumber":"01234",
    "price" : -1000.0,
    "type" : "credit",

},
{
    "_id" : ObjectId("584bc9ba420a6b189c3323w23"),
    "user_id" : 2,
    "mobilenumber":"04321",
    "price" : 1000.0,
    "type" : "credit",

}

在这里我要计算所有用户的总正价和负价,并且我需要检查该用户是否存在于汇总集合中.如果记录不存在,我们需要在汇总集合中创建文档(如果存在),我们需要更新"Totalpositiveprice","Totalnegativeprice"和"Balanceprice"

here i want to calculate total postive and total negative price of all user and i need to check whether that user is exists or not in summary collection.if record not exists we need to create document in summary collection if its exists we need to update "Totalpositiveprice","Totalnegativeprice" and "Balanceprice"

汇总表中已存在该记录

    {

        "user_id": "1",
        "mobilenumber":"01234",
        "Totalpositiveprice": 3000.0,
        "Totalnegativeprice": 0,
        "Balanceprice": 3000.0
    },
   {

        "user_id": "3",
        "mobilenumber":"05555",
        "Totalpositiveprice": 1000.0,
        "Totalnegativeprice": -100,
        "Balanceprice": 900.0
    }

  1. 我们需要为"mobilenumber":"01234"更新文档,

  1. we need to update the document for "mobilenumber":"01234",

我们需要为"mobilenumber":"04321"创建新文档,

we need to create new document for "mobilenumber":"04321",

手机号码":"05555"无需执行任何操作,因为用户定价中没有任何内容

"mobilenumber":"05555" no need to do anything bcoz nothing is there in userpricing

最后我应该得到像这样的摘要集合

finally i should get summary collection like this

 {

        "user_id": "1",
        "mobilenumber":"01234"
        "Totalpositiveprice": 5000.0,
        "Totalnegativeprice": -1000.0,
        "Balanceprice": 4000.0
    },
    {

        "user_id": "2",
         "mobilenumber":"04321"
        "Totalpositiveprice": 1000.0,
        "Totalnegativeprice": 0,
        "Balanceprice": 1000.0
    },
    {

    "user_id": "3",
    "mobilenumber":"05555",
    "Totalpositiveprice": 1000.0,
    "Totalnegativeprice": -100,
    "Balanceprice": 900.0
}

推荐答案

您可以尝试批量写入以批量上传从聚合结果创建的更新查询并更新summary集合.

You can try bulk write to bulk upload the update queries created from aggregation result and update the summary collection.

这是您可以在Mongo shell中尝试的快速代码,您可以根据需要进行调整.

Here is a quick code that you can try from Mongo shell and you can adjust to your needs.

下面的代码查询user_id,如果找不到匹配的user_id,则根据聚合值和向上插入递增价格.

The below code queries for user_id and increments the price values based on the aggregation values and upserts if no matching user_id is found.

您应根据需要更改batch.

var bulk = db.getCollection('summary').initializeUnorderedBulkOp();
var count = 0;
var batch = 1;

db.getCollection('userpricing').aggregate([
    {$group: {
        _id:"$user_id", 
        Totalpositiveprice:{$sum:{$cond:[{ '$gt': ['$price', 0]}, "$price", 0]}}, 
        Totalnegativeprice:{$sum:{$cond:[{ '$lt': ['$price', 0]}, "$price", 0]}},
        Balanceprice:{"$sum":"$price"}}
     },
    {$project: {_id:0, user_id:"$_id", Totalpositiveprice:1, Totalnegativeprice:1, Balanceprice:1}}
]).forEach(function(doc){ 
    var user_id = doc.user_id; 
    var totalpositiveprice = doc.Totalpositiveprice; 
    var totalnegativeprice = doc.Totalnegativeprice; 
    var balanceprice = doc.Balanceprice; 
    bulk.find({ "user_id" : user_id }).upsert().updateOne(
      { $inc: {"Totalpositiveprice" : totalpositiveprice, "Totalnegativeprice" : totalnegativeprice, "Balanceprice" : balanceprice } }
   ); 
    count++;  
    if (count == batch) { 
        bulk.execute(); 
        bulk = db.getCollection('summary').initializeUnorderedBulkOp(); 
        count = 0;
    } 
});

if (count > 0) { 
      bulk.execute(); 
 }

这篇关于我如何使用mongodb或robomongo计算正价和负价?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆