-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathREADME.rst
160 lines (116 loc) · 2.79 KB
/
README.rst
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
===================
pymongos
===================
简化了一些`pymongo`的操作,像操作`dict`一样操作你的数据。
因为日常用增删查改最多,所以简化主要是围绕这三个操作来的。
===================
安装
===================
code
::
python -m pip install pymongos
python3 -m pip install pymongos
===================
连接数据库
===================
- 无密码连接
code
::
from pymongos import mongodb
table = mongodb(host='127.0.0.1', db="test", table="test")
# 无日志
table = mongodb(host='127.0.0.1', db="test", table="test", level="debug")
- 密码连接
code
::
from pymongos import mongodb
table = mongodb(host='127.0.0.1', db="test", table="test", user="admin", passwd="123456789")
===================
插入数据
===================
- 不设置_id(MongoDB随机生成)
code
::
table += {"字段1": "数据1", "字段2": "数据1"}
or
::
table + {"字段1": "数据1", "字段2": "数据1"}
因为分配的ID是随机的,不建议使用
- 设置_id
code
::
table["设置_id"] = {"字段1": "数据3", "字段2": "数据4"}
table["test"] = {"爱好": "看书", "书名": "鬼吹灯"}
===================
删除数据
===================
- 仅支持指定key方式
code::
del table["设置_id"]
===================
修改
===================
code::
table["test"] = {"书名": "鬼吹灯和三体"}
table["test"] = {"书名": "《鬼吹灯》《三体》", "爱好": "看书&游泳"}
table["test"] = {"name" : "e4ting"}
===================
取数据
===================
- 表长度
code::
len(table)
- _id是否存在
code::
if "test" in table:
print("yes")
- 获取所有_id
code::
table.keys()
- 根据_id取
code::
data = table["test"]
print(data)
- 取整个表数据
数据少的时候才可以这么豪横的用
code::
table.get()
- 遍历表
表太大的时候,迭代取
code::
for _ in table:
print(_)
- 过滤
code::
table.get(name="e4ting")
table.get(**{"字段1":"数据1"})
- 分段
code::
table.get(limit=10, offset=1)
- 搜索
code::
table.search(name="4ting")
- 搜索分段
code::
table.search(limit=10, offset=0, **{"字段1" : "数据"})
===================
其他操作
===================
- 关闭连接
code::
del table
- 获取所有库名
code::
table.dbs
- 获取该库下所有collection
code::
table.tables
- 获取所有字段名
code::
table.colums
- 关闭日志
code::
table.none_log()
- 开启日志
code::
table.en_log()