|
win版驱动:pymongo-1.9.win32-py2.6.exe
# -*- coding:UTF-8
'''
Created on 2010-12-23
@author: DOC
'''
from pymongo import Connection
from Utils import exeTime
import re
conn = Connection("127.0.0.1") # 创建数据库连接
local = conn.local # 得到数据库
collor = None # 表
auth = local.authenticate('doc12', '110210121') # 验证用户密码
if(auth):
collor = local.collor # 获得表
def add():
prop = {'name':'doc', 'age':21, 'skills':('PHP', 'python'), 'size':88} # 条件:map<key:value>
datas = {'collor':'yellow', 'size':16, 'properties':prop, 'num':1}
collor.save(datas) # size 字段如果没有,会自动加上该字段
def delete():
#collor.remove() # 删除所有
collor.remove({'collor':'yellow'})
def update():
collor.update({'collor':'yellow'},{'$set':{'num':2}},upsert=True,multi=True)
def select():
return collor.find()
def select_Limit(): # 检索第 2、3条记录:skip(>=), limit(<=)
return collor.find().skip(1).limit(2)
def select_Equal(): # 等于查询
return collor.find({'collor':'red'}) # 无条件则查询所有
def select_Than(): # gt(>),lt(<),gte(>=),lte(<=),in(包含),nin((不包含)
return collor.find({'size':{"$gte":20}})# 无条件则查询所有
def select_GtLt():
return collor.find({'size':{"$gt":5,"$lt":15}}) # >5 and <25 多个条件
def select_In(): # in(包含),nin((不包含)
return collor.find({'size':{"$in":(10,20,99)}})
def select_Regular(): # 正则:只能是字符串
return collor.find({'collor':{"$regex":r"[a-z]+"}}) # 或者用:re.compile(r'.*')
def select_Childen_Map(): # 子对象查找
return collor.find({'properties.age':23}) # map
def select_Childen_List(): # 子对象查找
return collor.find({'properties.skills':'java'}) # list
def select_Where(): # 条件表达式: >, <, >=, <=, ==
return collor.find({'$where':'this.size==10'})
#vs = select_Regular()
#for i in vs:
# print i
@exeTime
def test():
for i in range(200000):
select_In()
if(collor != None):
test() |
|
|