python operation mongodb

巴扎黑
Release: 2016-12-03 11:11:55
Original
1430 people have browsed it

import pymongo,sys 
from pymongo import MongoClient 
client = MongoClient(host="localhost:27017",read_preference=pymongo.read_preferences.ReadPreference.PRIMARY_PREFERRED)
db = client.database 
db.authenticate("database","passwd") 

1.search 
pi = db.locinfo.find({"con":p}) #search 
pi = db.locinfo.find_one({"con":p}) 
print pi 
{u'city': u'u56feu6728u8212u514bu5e02', u'wid': u'101130908', u'zip': u'843900', u'pro': u'u65b0u7586u7ef4u543eu5c14u81eau6cbbu533a', u'wkey': u'u56feu6728u8212u514bu5e02', u'acode': u'0998', u'_id': u'txpcx5', u'fix': True, u'con': u'u56feu6728u8212u514bu5e02'} 


2.update 
db.locinfo.update({"con":p},{'$set':{"con":zc}}) # update one item 
×××××××××××××××批量update ××× multi=True   ×××××××××××××××××××××××××××××××× 
db.locinfo.update({"con":lon},{'$set':{"wkey": sht }},multi=True) #ok,# update multiple items 
×××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××× 

3。insert  # 插入数组,及一般字符窜 
db.wbinfo.insert({"city":cityname,"jiaojing":[jiaojing],"lukuang":"lukuang,"}) 

4。push 、 pop  插入数组,删除数组元素 
db.wbinfo.update({"city":"六安市"},{'$push':{"jiaojing":"two","lukuang":"lk"}}) 
db.wbinfo.update({"city":"六安市"},{'$pop':{"jiaojing":"two","lukuang":"lk"}}) 

5. 正则查询 
res = db.devices.find({'$and':[{'stats.devcaps':{'$exists':True}},{'stats.fwver':{'$regex':'.*N91|N92.*'}}]})  # stats.fwver 字段包含字符 'N91' 或者 'N92' 

6.字典 
if devcap.has_key('dev') and devcap.has_key('cap'): 

7. 
python操作mongodb根据_id查询数据的代码 
ObjectId pymongo python 
如果pymongo的版本号小于2.2,使用下面的语句导入ObjectId 
from pymongo.objectid import ObjectId 

如果pymongo的版本号大于2.2,则使用下面的语句 
from bson.objectid import ObjectId 
查询代码如下: 
collection.find_one({'_id':ObjectId('50f0d76347f4ec148890ef1e')}) 

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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!