Chinaunix首页 | 论坛 | 博客
  • 博客访问: 4995576
  • 博文数量: 921
  • 博客积分: 16037
  • 博客等级: 上将
  • 技术积分: 8469
  • 用 户 组: 普通用户
  • 注册时间: 2006-04-05 02:08
文章分类

全部博文(921)

文章存档

2020年(1)

2019年(3)

2018年(3)

2017年(6)

2016年(47)

2015年(72)

2014年(25)

2013年(72)

2012年(125)

2011年(182)

2010年(42)

2009年(14)

2008年(85)

2007年(89)

2006年(155)

分类: 数据库开发技术

2011-10-17 12:28:09

win版驱动:pymongo-1.9.win32-py2.6.exe
  1. # -*- coding:UTF-8
  2. '''
  3. Created on 2010-12-23

  4. @author: DOC
  5. '''
  6. from pymongo import Connection
  7. from Utils import exeTime
  8. import re

  9. conn = Connection("127.0.0.1") # 创建数据库连接
  10. local = conn.local # 得到数据库
  11. collor = None # 表
  12. auth = local.authenticate('doc12', '110210121') # 验证用户密码

  13. if(auth):
  14.     collor = local.collor # 获得表

  15. def add():
  16.     prop = {'name':'doc', 'age':21, 'skills':('PHP', 'python'), 'size':88} # 条件:map<key:value>
  17.     datas = {'collor':'yellow', 'size':16, 'properties':prop, 'num':1}
  18.     collor.save(datas) # size 字段如果没有,会自动加上该字段

  19. def delete():
  20.     #collor.remove() # 删除所有
  21.     collor.remove({'collor':'yellow'})
  22.     
  23. def update():
  24.     collor.update({'collor':'yellow'},{'$set':{'num':2}},upsert=True,multi=True)

  25. def select():
  26.     return collor.find()
  27. def select_Limit(): # 检索第 2、3条记录:skip(>=), limit(<=)
  28.     return collor.find().skip(1).limit(2)
  29. def select_Equal(): # 等于查询
  30.     return collor.find({'collor':'red'}) # 无条件则查询所有
  31. def select_Than(): # gt(>),lt(<),gte(>=),lte(<=),in(包含),nin((不包含)
  32.     return collor.find({'size':{"$gte":20}})# 无条件则查询所有
  33. def select_GtLt():
  34.     return collor.find({'size':{"$gt":5,"$lt":15}}) # >5 and <25 多个条件
  35. def select_In(): # in(包含),nin((不包含)
  36.     return collor.find({'size':{"$in":(10,20,99)}})
  37. def select_Regular(): # 正则:只能是字符串
  38.     return collor.find({'collor':{"$regex":r"[a-z]+"}}) # 或者用:re.compile(r'.*')
  39. def select_Childen_Map(): # 子对象查找
  40.     return collor.find({'properties.age':23}) # map
  41. def select_Childen_List(): # 子对象查找
  42.     return collor.find({'properties.skills':'java'}) # list
  43. def select_Where(): # 条件表达式: >, <, >=, <=, ==
  44.     return collor.find({'$where':'this.size==10'})

  45. #vs = select_Regular()
  46. #for i in vs:
  47. # print i

  48. @exeTime
  49. def test():
  50.     for i in range(200000):
  51.         select_In()
  52. if(collor != None):
  53.     test()
阅读(1790) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~