forked from GC-ZF/Comment-Backups
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest pymongo.py
34 lines (29 loc) · 1.17 KB
/
test pymongo.py
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
'''
借助 pymongo 读写数据库测试
文档:
https://pymongo.readthedocs.io/en/stable/tutorial.html Querying for More Than One Document章节有查询方法
中文实例:
https://blog.csdn.net/xHibiki/article/details/84524645
https://blog.csdn.net/Java_cola/article/details/121865491
'''
import pymongo
# Replace the uri string with your MongoDB deployment's connection string.
conn_str = "mongodb+srv://1310446718:zf632852@cluster0.i7omd.mongodb.net/?retryWrites=true&w=majority"
# set a 5-second connection timeout
client = pymongo.MongoClient ( conn_str, serverSelectionTimeoutMS=5000 )
# db = client.myFirstDatabase # myFirstDatabase数据库
# collection = db.comment # comment表
db = client[ 'myFirstDatabase' ] # myFirstDatabase数据库
collection = db[ 'comment' ] # comment表
output = [ ]
for i in collection.find ():
output.append ( i )
print ( output )
# 标准的JSON格式
# with open('images.json', 'w', encoding="UTF-8") as jf:
# jf.write(json.dumps(output, indent=1))
# 一行一条数据
with open ( 'Comment test.json', 'w', encoding="UTF-8" ) as f:
for line in output:
line = str ( line )
f.writelines ( line + '\n' )