Home > Backend Development > Python Tutorial > pymongo实现控制mongodb中数字字段做加法的方法

pymongo实现控制mongodb中数字字段做加法的方法

WBOY
Release: 2016-06-10 15:16:55
Original
1598 people have browsed it

本文实例讲述了pymongo实现控制mongodb中数字字段做加法的方法。分享给大家供大家参考。具体分析如下:

这个非常实用,比如我们需要给文章做访问统计,可以设置一个数字字段:hit,然后每次点击后给改字段增加1即可

import pymongo
#导入pymongo 模块
conn = pymongo.Connection()
#连接本地mongodb数据库
db=conn.database #database为数据库名称
collection = db.article
#article为collection名称,相当于mysql里的表名
collection.update({'_id':ObjectId(id),'$inc':{'hit':1}})
# 为hit字段增加1
Copy after login

希望本文所述对大家的Python程序设计有所帮助。

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template