import pymongo
import dns
import pandas as pd
from pymongo.mongo_client import MongoClient
uri = "mongodb+srv://<username>:<password>@cluster0.rifrpxv.mongodb.net/?retryWrites=true&w=majority"
# Create a new client and connect to the server
client = MongoClient(uri)
# Send a ping to confirm a successful connection
try:
client.admin.command('ping')
print("Pinged your deployment. You successfully connected to MongoDB!")
except Exception as e:
print(e)
到目前为止,这有效,我能够连接。
df = pd.read_csv(file)
data = df.to_dict(orient = 'records')
db = client['database']
db.collections.insert_many(data)
当我运行这个时,我得到:
OperationFailure: Unsupported OP_QUERY command: insert. The client driver may require an upgrade. For more details see https://dochub.mongodb.org/core/legacy-opcode-removal
我检查了python版本(3.8.3)和pymongo版本(4.6)和mongodb版本(6.0)。基于此处的文档:https://www.mongodb.com/docs/drivers/pymongo/一切都应该兼容,但我仍然无法连接。
非常感谢任何帮助!
我尝试将所有版本更新到最新版本,但仍然无法连接。
已解决...必须将 pymongo 恢复到 4.1 以实现兼容性