PyMongo $inc having issues

2019-02-18 00:34发布

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!

2条回答
▲ chillily
2楼-- · 2019-02-18 01:26
pymongo == 2.7 
campaign_id = bson.ObjectId(campaign_id)
mongo_db.campaign.update({'_id': campaign_id}, {'$inc': {'item': 1}})
查看更多
不美不萌又怎样
3楼-- · 2019-02-18 01:29

$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.

查看更多
登录 后发表回答