python import json into pymongo

Solutions on MaxInterview for python import json into pymongo by the best coders in the world

showing results for - "python import json into pymongo"
Joshua
02 Oct 2020
1import json
2from pymongo import MongoClient
3
4client = MongoClient('localhost', 27017)
5db = client['countries_db']
6collection_currency = db['currency']
7
8with open('currencies.json') as f:
9    file_data = json.load(f)
10
11# if pymongo < 3.0, use insert()
12collection_currency.insert(file_data)
13# if pymongo >= 3.0 use insert_one() for inserting one document
14collection_currency.insert_one(file_data)
15# if pymongo >= 3.0 use insert_many() for inserting many documents
16collection_currency.insert_many(file_data)
17
18client.close()
19