如何使用 PyMongo 为现有价值增加价值 [英] How to add value to existing value using PyMongo

查看:72
本文介绍了如何使用 PyMongo 为现有价值增加价值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试更新/创建一个新的数据集并将以前的值与新的值结合起来.

I am trying to update/create a new dataset and combine the previous value with the new one.

这就是它现在在我的 Python 脚本中的样子.

This is how it looks like in my Python script right now.

dailyDataset = {
    "pId" : pub,
    "oId" : off,
    "payout" : +addPayout,
}
db[dbName].update( { 'pId' : publisher, 'oId' : offer.id },  {"$set" : dailyDataset }, True)

我尝试实现的是,如果数据集 pId 和 oId 存在,则使用payout"中的当前值并添加 addPayout 中的值.

What I try to achieve is, if the dataset pId and oId exists use the current value from "payout" and add the value from addPayout.

例如payout = 1.22 和 addPayout = 1.22 结果应该是 2.44.

E.g. payout = 1.22 and addPayout = 1.22 result should be 2.44.

欢迎提供任何提示.

谢谢!

推荐答案

您可以使用 $inc 运算符:

You can use the $inc operator:

db[dbName].update( { 'pId' : publisher, 'oId' : offer.id },  {"$inc" : {'payout':1.22}}, True)

这篇关于如何使用 PyMongo 为现有价值增加价值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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