forked from zhanghe06/python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_mongo.py
More file actions
187 lines (167 loc) · 4.63 KB
/
test_mongo.py
File metadata and controls
187 lines (167 loc) · 4.63 KB
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# encoding: utf-8
__author__ = 'zhanghe'
import sys
sys.path.append('..')
from tools.mongo import Mongodb
import time
import calendar
from datetime import datetime, timedelta, date
def time_local_to_utc(local_time):
"""
本地时间转UTC时间
:param local_time:
:return:
"""
# 字符串处理
if isinstance(local_time, str) and len(local_time) == 10:
local_time = datetime.strptime(local_time, '%Y-%m-%d')
elif isinstance(local_time, str) and len(local_time) >= 19:
local_time = datetime.strptime(local_time[:19], '%Y-%m-%d %H:%M:%S')
elif not (isinstance(local_time, datetime) or isinstance(local_time, date)):
local_time = datetime.now()
# 时间转换
utc_time = local_time + timedelta(seconds=time.timezone)
return utc_time
def time_utc_to_local(utc_time):
"""
UTC时间转本地时间
:param utc_time:
:return:
"""
# 字符串处理
if isinstance(utc_time, str) and len(utc_time) == 10:
utc_time = datetime.strptime(utc_time, '%Y-%m-%d')
elif isinstance(utc_time, str) and len(utc_time) >= 19:
utc_time = datetime.strptime(utc_time[:19], '%Y-%m-%d %H:%M:%S')
elif not (isinstance(utc_time, datetime) or isinstance(utc_time, date)):
utc_time = datetime.utcnow()
# 时间转换
local_time = utc_time - timedelta(seconds=time.timezone)
return local_time
db_config = {
'host': 'localhost',
'port': 27017,
'database': 'test_user'
}
test_date = [
{
'_id': 1,
'id': 1,
'name': 'Lily',
'sex': 'F',
'age': 20,
'city': 'shanghai',
'skill': ['word', 'excel']
},
{
'_id': 2,
'id': 2,
'name': 'Tom',
'sex': 'M',
'age': 22,
'city': 'shanghai',
'skill': ['php', 'java']
},
{
'_id': 3,
'id': 3,
'name': 'Jerry',
'sex': 'M',
'age': 22,
'city': 'beijing',
'skill': ['php', 'python']
}
]
def test():
try:
table_name = 'user'
conn = Mongodb(db_config)
print conn.db
print conn.find_one(table_name)
print conn.remove(table_name) # 清空记录
print conn.insert(table_name, test_date) # 插入记录
print conn.distinct(table_name, 'age') # 统计年龄范围
print conn.update(table_name, {'id': 3}, {'age': 24}) # id=3的记录年龄更新为24
print conn.distinct(table_name, 'age') # 统计年龄范围
conn.output_rows(table_name)
print conn.update(table_name, {}, {'age': 1}, 'inc') # 所有记录年龄增加1岁
conn.output_rows(table_name)
# print conn.update(table_name, {'id': 3}, {'skill': 'mysql'}, 'push') # 向数组字段中添加单个元素
# print conn.update(table_name, {'id': 3}, {'skill': 'mysql'}, 'pull') # 向数组字段中删除单个元素
print conn.update(table_name, {'id': 3}, {'skill': ['ruby', 'c#']}, 'pushAll') # 向数组字段中添加多个元素
print conn.update(table_name, {'id': 3}, {'skill': ['ruby', 'c#']}, 'pullAll') # 向数组字段中删除多个元素
conn.output_rows(table_name)
except Exception, e:
print e
def test_02():
table_name = 'user'
conn = Mongodb(db_config)
print conn.db
print conn.find_one(table_name)
test_doc = {
'_id': 1,
'id': 1,
'name': 'admin',
'create_time': datetime.strptime('2017-07-07 08:00:00', '%Y-%m-%d %H:%M:%S'),
'create_time_utc': time_local_to_utc('2017-07-07 08:00:00'),
'create_time_str': '2017-07-07 08:00:00'
}
print conn.remove(table_name) # 清空记录
print conn.insert(table_name, test_doc) # 插入记录
print conn.find_one(table_name)
conn.output_rows(table_name)
if __name__ == '__main__':
# test()
test_02()
"""
Database(MongoClient(host=['localhost:27017'], document_class=dict, tz_aware=False, connect=True), u'test_user')
None
0
[1, 2, 3]
[20, 22]
1
[20, 22, 24]
********** 表名[user] [1/3] **********
city : shanghai
name : Lily
sex : F
age : 20
id : 1
_id : 1
********** 表名[user] [2/3] **********
city : shanghai
name : Tom
sex : M
age : 22
id : 2
_id : 2
********** 表名[user] [3/3] **********
city : beijing
name : Jerry
sex : M
age : 24
id : 3
_id : 3
3
********** 表名[user] [1/3] **********
city : shanghai
name : Lily
sex : F
age : 21
id : 1
_id : 1
********** 表名[user] [2/3] **********
city : shanghai
name : Tom
sex : M
age : 23
id : 2
_id : 2
********** 表名[user] [3/3] **********
city : beijing
name : Jerry
sex : M
age : 25
id : 3
_id : 3
"""