Skip to content

Commit 02aa310

Browse files
committed
更新mongo工具类
1 parent b85bd9b commit 02aa310

2 files changed

Lines changed: 7 additions & 3 deletions

File tree

test/test_mongo.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,9 @@ def test():
5050
print conn.find_one(table_name)
5151
print conn.remove(table_name) # 清空记录
5252
print conn.insert(table_name, test_date) # 插入记录
53+
print conn.distinct(table_name, 'age') # 统计年龄范围
5354
print conn.update(table_name, {'id': 3}, {'age': 24}) # id=3的记录年龄更新为24
55+
print conn.distinct(table_name, 'age') # 统计年龄范围
5456
conn.output_rows(table_name)
5557
print conn.update(table_name, {}, {'age': 1}, 'inc') # 所有记录年龄增加1岁
5658
conn.output_rows(table_name)
@@ -67,7 +69,9 @@ def test():
6769
None
6870
0
6971
[1, 2, 3]
72+
[20, 22]
7073
1
74+
[20, 22, 24]
7175
********** 表名[user] [1/3] **********
7276
city : shanghai
7377
name : Lily

tools/mongo.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ def insert(self, table_name, data):
106106
ids = self.db.get_collection(table_name).insert(data)
107107
return ids
108108
except Exception, e:
109-
logger.error('插入错误:%s' % e)
109+
logger.error('插入失败:%s' % e)
110110
return None
111111

112112
def update(self, table_name, condition, update_data, update_type='set'):
@@ -120,11 +120,11 @@ def update(self, table_name, condition, update_data, update_type='set'):
120120
:return:
121121
"""
122122
if update_type not in ['inc', 'set', 'unset', 'push', 'pushAll', 'addToSet', 'pop', 'pull', 'pullAll', 'rename']:
123-
logger.error('更新类型错误:%s' % update_type)
123+
logger.error('更新失败,类型错误:%s' % update_type)
124124
return None
125125
try:
126126
result = self.db.get_collection(table_name).update_many(condition, {'$%s' % update_type: update_data})
127-
logger.info('匹配数量:%s;更新数量:%s' % (result.matched_count, result.modified_count))
127+
logger.info('更新成功,匹配数量:%s;更新数量:%s' % (result.matched_count, result.modified_count))
128128
return result.modified_count # 返回更新数量,仅支持MongoDB 2.6及以上版本
129129
except Exception, e:
130130
logger.error('更新失败:%s' % e)

0 commit comments

Comments
 (0)