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})
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}} ) # 항목 1개 업데이트
×××××××××××××××批weightupdate ××× multi=True ××××××××××××××××××× ×××××××××××××
db.locinfo.update({"con":lon},{'$set':{"wkey": sht }},multi=True) # 좋아요, # 여러 항목 업데이트
×××××××××××××××××××××××××××××××××××××××× ×××××××××××××××××××
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') 및 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('50f0d76347f4ec14 8890ef1e')})