共計(jì) 482 個(gè)字符,預(yù)計(jì)需要花費(fèi) 2 分鐘才能閱讀完成。
要在 Python 中連接到 MongoDB 并查詢數(shù)據(jù),你需要安裝 pymongo 庫,它是 MongoDB 官方提供的 Python 驅(qū)動(dòng)程序。你可以使用以下代碼連接到 MongoDB 并查詢數(shù)據(jù):
from pymongo import MongoClient
# 連接到 MongoDB
client = MongoClient("mongodb://localhost:27017")
# 選擇 數(shù)據(jù)庫
db = client["your_database_name"]
# 選擇集合
collection = db["your_collection_name"]
# 查詢數(shù)據(jù)
query = {"key": "value"}
result = collection.find(query)
# 打印查詢結(jié)果
for document in result:
print(document)
在代碼中,你需要將 "your_database_name"
替換為你要連接的數(shù)據(jù)庫的名稱,將 "your_collection_name"
替換為你要查詢的集合的名稱。你還可以根據(jù)你的需求修改查詢的條件{"key": "value"}
。
丸趣 TV 網(wǎng) – 提供最優(yōu)質(zhì)的資源集合!
正文完