Skip to content

Commit a364e44

Browse files
committed
改进mongo工具类更新操作
1 parent 1f92746 commit a364e44

File tree

2 files changed

+40
-8
lines changed

2 files changed

+40
-8
lines changed

test/test_mongo.py

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,16 @@
4444

4545
def test():
4646
try:
47+
table_name = 'user'
4748
conn = Mongodb(db_config)
4849
print conn.db
49-
print conn.find_one('user')
50-
print conn.remove('user')
51-
print conn.insert('user', test_date)
52-
print conn.update('user', {'id': 1}, {'$set': {'age': 28}})
53-
conn.output_rows('user')
50+
print conn.find_one(table_name)
51+
print conn.remove(table_name) # 清空记录
52+
print conn.insert(table_name, test_date) # 插入记录
53+
print conn.update(table_name, {'id': 3}, {'age': 24}) # id=3的记录年龄更新为24
54+
conn.output_rows(table_name)
55+
print conn.update(table_name, {}, {'age': 1}, 'inc') # 所有记录年龄增加1岁
56+
conn.output_rows(table_name)
5457
except Exception, e:
5558
print e
5659

@@ -62,7 +65,9 @@ def test():
6265
"""
6366
Database(MongoClient(host=['localhost:27017'], document_class=dict, tz_aware=False, connect=True), u'test_user')
6467
None
68+
0
6569
[1, 2, 3]
70+
1
6671
********** 表名[user] [1/3] **********
6772
city : shanghai
6873
name : Lily
@@ -81,7 +86,30 @@ def test():
8186
city : beijing
8287
name : Jerry
8388
sex : M
84-
age : 22
89+
age : 24
90+
id : 3
91+
_id : 3
92+
3
93+
********** 表名[user] [1/3] **********
94+
city : shanghai
95+
name : Lily
96+
sex : F
97+
age : 21
98+
id : 1
99+
_id : 1
100+
********** 表名[user] [2/3] **********
101+
city : shanghai
102+
name : Tom
103+
sex : M
104+
age : 23
105+
id : 2
106+
_id : 2
107+
********** 表名[user] [3/3] **********
108+
city : beijing
109+
name : Jerry
110+
sex : M
111+
age : 25
85112
id : 3
86113
_id : 3
114+
87115
"""

tools/mongo.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,17 +109,21 @@ def insert(self, table_name, data):
109109
logger.error('插入错误:%s' % e)
110110
return None
111111

112-
def update(self, table_name, condition, update_data):
112+
def update(self, table_name, condition, update_data, update_type='set'):
113113
"""
114114
批量更新数据
115115
upsert : 如果不存在update的记录,是否插入;true为插入,默认是false,不插入。
116116
:param table_name:
117117
:param condition:
118118
:param update_data:
119+
:param update_type: 范围:['inc', 'set', 'unset', 'push', 'pushAll', 'addToSet', 'pop', 'pull', 'pullAll', 'rename']
119120
:return:
120121
"""
122+
if update_type not in ['inc', 'set', 'unset', 'push', 'pushAll', 'addToSet', 'pop', 'pull', 'pullAll', 'rename']:
123+
logger.error('更新类型错误:%s' % update_type)
124+
return None
121125
try:
122-
result = self.db.get_collection(table_name).update_many(condition, update_data)
126+
result = self.db.get_collection(table_name).update_many(condition, {'$%s' % update_type: update_data})
123127
logger.info('匹配数量:%s;更新数量:%s' % (result.matched_count, result.modified_count))
124128
return result.modified_count # 返回更新数量,仅支持MongoDB 2.6及以上版本
125129
except Exception, e:

0 commit comments

Comments
 (0)