- def query():
- conn = Connection('127.0.0.1',27017)
- db = getattr(conn,'dbname')
- coll = getattr(db,'collname')
- #使用正则查询
- import re
- q = rs.compile(r'add') #完成正则 r'.*' ,r'[a-z]+'
- rst = coll.find({'field' : { '$regex' : q } })
- print rst.count() #打印查询结果
- rst =coll.find({'field':{'$regex':'^'+query}})
- #查询所有以query开头的文档
- #查询方式2
- rst = coll.find({'field' : { '$regex' : r '[a-z]+'} })
- print rst.count()