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

查看:20
本文介绍了如何使用 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"

汇总表中已经存在这条记录

in summary table already exists this record

    {

        "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",

"mobilenumber":"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天全站免登陆