python3 -m pip install 'pymongo[srv]'
以下是连接到 MongoDB 所需的最少代码的概要。您将在接下来的几个步骤中添加内容以读取数据。
在第 4 行,将 URI 字符串替换为您自己的 Atlas 连接字符串。
from pymongo import MongoClient
# Replace the uri string with your MongoDB deployment's connection string.
uri = "mongodb+srv://<user>:<password>@<cluster-url>?retryWrites=true&writeConcern=majority"
client = MongoClient(uri)
# database and collection code goes here 数据库名,集合名哦
db = client.hongweizhuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu
coll = db.raspberrypi
# find code goes here
cursor = coll.find()
# iterate code goes here
for doc in cursor:
print("=>", doc)
# Close the connection to MongoDB when you're done.
client.close()