我当前的代码不会覆盖存储在MongoDB中的json,而是创建另一个文档。我想让它覆盖。我该怎么做
下面是我目前的代码
import json
import pymongo
import dns
from pymongo import MongoClient
# Making Connection
myclient = MongoClient("mongodb+srv://<DBNAME>:<PASSWORD>@<DBNAME>.y3vh0.mongodb.net/<DBNAME>?retryWrites=true&w=majority")
db = myclient["PlayerPrices"]
Collection = db["Playstation"]
# Loading or Opening the json file
with open('players.json') as file:
file_data = json.load(file)
if isinstance(file_data, list):
Collection.insert_many(file_data)
else:
Collection.insert_one(file_data)
print("Done")
关于您的问题,如果要查找并更新特定文档,最好使用find_one_和_update()方法
https://kb.objectrocket.com/mongo-db/how-to-update-a-mongodb-document-in-python-356
我希望这对你有帮助
相关问题 更多 >
编程相关推荐