Skip to content

Instantly share code, notes, and snippets.

@RWaltersMA
Created April 10, 2017 17:10
Show Gist options
  • Save RWaltersMA/135ee6731ccd15d6c34051499de8698c to your computer and use it in GitHub Desktop.
Save RWaltersMA/135ee6731ccd15d6c34051499de8698c to your computer and use it in GitHub Desktop.
Update showing effects of default upsert in mongodb PyMongo
from pymongo import MongoClient
#include pprint for readabillity of the
from pprint import pprint
#change the MongoClient connection string to your MongoDB database instance
client = MongoClient(port=27020)
db=client.business
ASingleReview = db.reviews.find_one({})
print('A sample document:')
pprint(ASingleReview)
result = db.reviews.update_one({'_id' : ASingleReview.get('_id') }, {'$inc': {'likes': 1}})
print('Number of documents modified : ' + str(result.modified_count))
UpdatedDocument = db.reviews.find_one({'_id':ASingleReview.get('_id')})
print('The updated document:')
pprint(UpdatedDocument)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment