Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PyMongo $inc having issues

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!

like image 628
Patrick Avatar asked Jun 11 '13 21:06

Patrick


People also ask

Is PyMongo client thread safe?

PyMongo is thread-safe and even provides built-in connection pooling for threaded applications.

Do I need to close PyMongo connection?

There's no need to close a Connection instance, it will clean up after itself when Python garbage collects it. You should use MongoClient instead of Connection ; Connection is deprecated. To take advantage of connection pooling, you could create one MongoClient that lasts for the entire life of your process.

Do I need to install MongoDB to use PyMongo?

MongoDB must be installed on the machine or server that is being used to run Python scripts.


1 Answers

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

like image 62
Cairnarvon Avatar answered Oct 26 '22 06:10

Cairnarvon