📜  python 将 json 导入 pymongo - Python 代码示例

📅  最后修改于: 2022-03-11 14:45:09.162000             🧑  作者: Mango

代码示例1
import json
from pymongo import MongoClient

client = MongoClient('localhost', 27017)
db = client['countries_db']
collection_currency = db['currency']

with open('currencies.json') as f:
    file_data = json.load(f)

# if pymongo < 3.0, use insert()
collection_currency.insert(file_data)
# if pymongo >= 3.0 use insert_one() for inserting one document
collection_currency.insert_one(file_data)
# if pymongo >= 3.0 use insert_many() for inserting many documents
collection_currency.insert_many(file_data)

client.close()