PyMongo $inc having issues

不打扰是莪最后的温柔 提交于 2019-12-19 09:19:28

问题


When trying to use $inc I get a Syntax error stating that $set is wrong. My query is as follow:

mongo_db.campaign.update({'_id': str(campaign_id)}, { $inc: { 'item': 1 } }):

I've looked at the mongo documentation (http://docs.mongodb.org/manual/reference/operator/inc/) and other examples on SO but I can't find what's wrong. Any suggestions?

Thanks!


回答1:


$inc isn't a valid Python identifier. You should pass it as a string, like everything else:

mongo_db.campaign.update({'_id': str(campaign_id)}, {'$inc': {'item': 1}})

The MongoDB docs you linked are the general MongoDB documentation, not PyMongo-specific; you can't copy/paste them literally and expect things to work.




回答2:


pymongo == 2.7 
campaign_id = bson.ObjectId(campaign_id)
mongo_db.campaign.update({'_id': campaign_id}, {'$inc': {'item': 1}})


来源:https://stackoverflow.com/questions/17054494/pymongo-inc-having-issues

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!