Pymongo implements a method to control the addition of number fields in mongodb

  • 2020-04-02 14:42:29
  • OfStack

This article illustrates pymongo's method to control the addition of number fields in mongodb. Share with you for your reference. Specific analysis is as follows:

This is very practical, for example, we need to do access statistics for the article, you can set a number field: hit, and then after each click to change the field to add 1


import pymongo
# The import pymongo  The module 
conn = pymongo.Connection()
# Connected to the local mongodb The database 
db=conn.database #database Is the database name 
collection = db.article
#article for collection Name, equivalent to mysql In the name of the table 
collection.update({'_id':ObjectId(id),'$inc':{'hit':1}})
#  for hit Field increase 1

I hope this article has helped you with your Python programming.


Related articles: