1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
| def search_data():
""" search data
:return:
"""
client = con_mongo()
# 年龄大于 25 且小于 30 且不等于 29
age_res = client.test.user.find({"age": {'$gt': 25, '$lt': 30, '$ne': 29}})
for item in age_res:
age = item.get('age', '')
print "expected age info: ", age
pattern_string1 = '^l'
pattern_string2 = 'r$'
regx1 = re.compile(pattern_string1, re.IGNORECASE)
regx2 = re.compile(pattern_string2, re.IGNORECASE)
regx_list = [regx1, regx2]
# name 以 'l' 开头且以 'r' 结尾
res_and = client.test.user.find({"name": {'$all': regx_list}})
for item in res_and:
name = item.get('name', '')
print "name that begins with 'l' and ends with 'r' are ", name
# name 以 'l' 开头或以 'r' 结尾
res_or = client.test.user.find({"name": {'$in': regx_list}})
for item in res_or:
name = item.get('name', '')
print "name that begins with 'l' or ends with 'r' are ", name
# name 以 'l' 开头或以 'r' 结尾 -- 第 2 种方法
pattern_strings = ['^l', 'r$']
pattern_string = '|'.join(pattern_strings)
regx = re.compile(pattern_string, re.IGNORECASE)
res = client.test.user.find({"name": regx})
for item in res:
name = item.get('name', '')
print "name that begins with 'l' or ends with 'r' are ", name
|