Repository navigation
Expand file tree
/
Copy pathmongodb_utils.py
More file actions
211 lines (174 loc) · 5.32 KB
/
Copy pathmongodb_utils.py
File metadata and controls
211 lines (174 loc) · 5.32 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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 17-6-13 下午12:56
# @Author : Tom.Lee
# @Docs : http://www.cnblogs.com/hhh5460/p/5838516.html
# @File : mongodb.py
# @Product : PyCharm
import pymongo
class _Mongodb(object):
def __init__(self,
host=None,
port=None,
document_class=dict,
tz_aware=None,
connect=None,
**kwargs):
self.__mongodb = pymongo.MongoClient(
host, port, document_class, tz_aware, connect, **kwargs)
@property
def mongodb_client(self):
return self.__mongodb
class MongodbUtils(object):
def __init__(self,
host=None,
port=None,
document_class=dict,
tz_aware=None,
connect=None,
**kwargs):
self.__mongodb_client = _Mongodb(
host=host,
port=port,
document_class=document_class,
tz_aware=tz_aware,
connect=connect,
**kwargs).mongodb_client
self.__database = None
self.__collection = None
@property
def mongodb_client(self):
return self.__mongodb_client
@property
def mongodb_database(self):
assert self.__database
return self.__database
@property
def mongodb_collection(self):
assert self.__collection
return self.__collection
def use_db(self, db):
"""
切换数据库 > use tom_db
:param db:
:return:
"""
self.__database = self.db_create_or_get(db)
return self
def use_collection(self, collection, db=None):
"""
使用表操作 > db.tom_table
:param collection:
:param db:
:return:
"""
if db:
self.__database = self.db_create_or_get(db)
self.__collection = self.mongodb_database[collection]
return self
def db_list(self):
"""
数据库列表 show dbs
:return: ['db1','db2']
"""
return self.mongodb_client.database_names()
def db_exists(self, db_name):
"""
:param db_name:
:return: True/False
"""
return db_name in self.db_list()
def db_create_or_get(self, db_name):
"""
创建或使用
> use tom_db
> db.createCollection('table1') # 第二步开始创建数据库
:param db_name:
:return: __mongodb.get_database(db_name)
"""
# self.mongodb_client.get_database(db_name)
return self.mongodb_client[db_name]
def db_delete(self, db_name):
"""
删除
> use tom_db
> db.dropDatabase()
:param db_name:
:return:
error : {u'code': 26, u'ok': 0.0, u'errmsg': u'ns not found'}
success: {u'ns': u'tom_db.tom_table', u'ok': 1.0, u'nIndexesWas': 1}
"""
return self.mongodb_client.drop_database(db_name)
def collection_list(self):
"""
表(文档)列表 > show tables
:return:
"""
return self.mongodb_database.collection_names()
def collection_create_or_get(self, collection_name):
"""
创建或获取表 createCollection('table1')
:param collection_name:
:return:
"""
return self.mongodb_database[collection_name]
def collection_exists(self, collection_name):
"""
集合是否存在
:param collection_name:
:return:
"""
return collection_name in self.collection_list()
def collection_delete(self, collection_name):
"""
删除集合 db.tom_table2.drop()
:param collection_name:
:return:
"""
return self.mongodb_database.drop_collection(collection_name)
def document_count(self, filter_=None):
"""
db.tom_table.count()
:param filter_:{'name':'zs'}
:return:
"""
return self.mongodb_collection.count(filter=filter_)
def document_find(self, *args, **kwargs):
"""
db.tom_table.find({'seq':'_seq_7'})
:param args:
:param kwargs:{'seq':'_seq_7'}
:return:
"""
return self.mongodb_collection.find(*args, **kwargs)
def document_insert(self, dict_item):
"""
db.tom_table.insert({'name':'jack'})
:param dict_item: {'name':'jack'}
:return:
"""
return self.mongodb_collection.insert(dict_item)
def document_drop(self):
"""
删除全部文档
:return:
"""
return self.mongodb_collection.drop()
def document_delete(self, filter_, collation=None):
"""
db.tom_table.deleteOne({'seq':'_seq_7'})
:param filter_: {'name':'jack'}
:param collation:
:return:
"""
result = self.mongodb_collection.delete_one(filter_, collation)
return result.delete_count > 0
def document_delete_list(self, filter_, collation=None):
"""
db.tom_table.deleteMany({'seq':'_seq_7'})
:param filter_: {'seq':'_seq_7'}
:param collation:
:return:
"""
self.mongodb_collection.delete_many(filter_, collation)
return self.document_count(filter_) == 0