17

I have this JSON file, currencies.json:

{
    "AUD": 1.5978,
    "BGN": 1.9558,
    "BRL": 4.0726,
    "CAD": 1.5868,
    "CHF": 1.1703,
    "CNY": 7.7975,
    "CZK": 25.405,
    "DKK": 7.4478,
    "GBP": 0.87285,
    "HKD": 9.6889,
    "HRK": 7.4398,
    "HUF": 312.9,
    "IDR": 16993.0,
    "ILS": 4.2984,
    "INR": 80.255,
    "ISK": 122.1,
    "JPY": 129.74,
    "KRW": 1330.3,
    "MXN": 22.88,
    "MYR": 4.8365,
    "NOK": 9.5715,
    "NZD": 1.7024,
    "PHP": 64.64,
    "PLN": 4.2262,
    "RON": 4.663,
    "RUB": 70.539,
    "SEK": 10.194,
    "SGD": 1.6216,
    "THB": 38.495,
    "TRY": 4.888,
    "USD": 1.2346,
    "ZAR": 14.52
}

And this connection in Python:

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

My db name is countries_db with the currency collection. Is there a way to import the file to the db using python?
Thanks for your help.

Ivan Vinogradov
  • 4,269
  • 6
  • 29
  • 39
Filipe Santos
  • 371
  • 1
  • 2
  • 11

1 Answers1

32

You can read data from file and insert it into collection using insert_one method:

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()
Ivan Vinogradov
  • 4,269
  • 6
  • 29
  • 39
  • 3
    If you have a large file, you may be better of using `mongoimport` as per [this question](https://stackoverflow.com/a/15941263/420867) with python's `subprocess.run()` – drevicko Aug 01 '19 at 04:40
  • May I know How to insert multiple json files at once? – KcH Dec 27 '19 at 11:01
  • @Codenewbie hi) what do you mean by "at once"? In parallel? Concurrently? Or just in one large batch? – Ivan Vinogradov Dec 27 '19 at 11:03
  • could you please look at this question and help me out with , https://stackoverflow.com/questions/59499237/insert-multiple-json-files-to-mongodb-using-python – KcH Dec 27 '19 at 11:09
  • @IvanVinogradov hey have you got the question? , I have taken your answer as reference and trying to load atleast one file in to db – KcH Dec 27 '19 at 11:16
  • 1
    take a look at the answer – Ivan Vinogradov Dec 27 '19 at 11:30
  • I am wondering why I cannot see the records on the MongoDB compass after executing this code. I can see the correct number of documents by executing `db.collection.count()` function. If I import the JSON files manually, then only I can see the documents on the compass. – SBDK8219 Mar 20 '20 at 13:58
  • @IvanVinogradov link is dead – jtlz2 Jul 07 '21 at 08:37
  • 1
    @jtlz2 fixed, ty) – Ivan Vinogradov Jul 07 '21 at 12:55