python 连接 mongodb

来源:互联网 发布:java se for mac10.11 编辑:程序博客网 时间:2024/04/30 06:17

首先下载pymongo并安装之:

https://pypi.python.org/pypi/pymongo/

代码奉上:

__author__ = 'bloodchilde'import pymongoclient = pymongo.MongoClient("localhost",27017)db = client.studycollection = db.personinsert_item = {    "name":"zhengjinwei",    "age":24,    "email":"2538698032@qq.com",    "country":"china"}ret = collection.insert(insert_item)if ret:    print "insert success"ret = collection.remove({"name":"zhengjinwei"})if ret['ok'] and ret['n']:    print "delete success"ret = collection.update({"name":"tom"},{"$set":{"name":"tom111"}})if ret['updatedExisting'] and ret['nModified']:    print "update success"else:    print "update failed"items = collection.find().sort("_id",pymongo.ASCENDING)for item in items:    print item['name'],item['age'],item['email'],item['country']



0 0
原创粉丝点击