PyMongo upsert引发"upsert必须是bool的实例".错误 [英] PyMongo upsert throws "upsert must be an instance of bool" error

查看:498
本文介绍了PyMongo upsert引发"upsert必须是bool的实例".错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从Python在MongoDB上运行更新.我有这行:

I'm running an update on my MongoDB from Python. I have this line:

self.word_counts[source].update({'date':posttime},{"$inc" : words},{'upsert':True})

但是它抛出了这个错误:

But it throws this error:

raise TypeError("upsert must be an instance of bool")

但是True对我来说似乎是bool的一个实例!

But True looks like an instance of bool to me!

我应该如何正确编写此更新?

How should I correctly write this update?

推荐答案

PyMongo的

The third argument to PyMongo's update() is upsert and must be passed a boolean, not a dictionary. Change your code to:

self.word_counts[source].update({'date':posttime}, {"$inc" : words}, True)

或将upsert=True作为关键字参数传递:

Or pass upsert=True as a keyword argument:

self.word_counts[source].update({'date':posttime}, {"$inc" : words}, upsert=True)

您的错误很可能是由于在update()引起的rel ="noreferrer"> MongoDB文档. JavaScript版本的update将对象作为其第三个参数,其中包含可选参数,例如upsertmulti.但是由于Python允许将关键字参数传递给函数(与仅具有位置参数的JavaScript不同),因此这是不必要的,PyMongo会将这些选项用作可选的函数参数.

Your mistake was likely caused by reading about update() in the MongoDB docs. The JavaScript version of update takes an object as its third argument containing optional parameters like upsert and multi. But since Python allows passing keyword arguments to a function (unlike JavaScript which only has positional arguments), this is unnecessary and PyMongo takes these options as optional function parameters instead.

这篇关于PyMongo upsert引发"upsert必须是bool的实例".错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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