1

I try using this but it won't work.

from pymongo import MongoClient
import json
client = MongoClient('localhost', 27017)
client('mongoimport --db myDatabase --collection restaurants --file c:\restaurants\restaurants.json')
print ('json import sucessfully')

Greatly appreciate of any helps. Thanks

Gunasekar
  • 611
  • 1
  • 8
  • 21
Nat
  • 679
  • 1
  • 9
  • 24

1 Answers1

1

Similar to this answer, mongoimport is a command-line program and not in the PyMongo API.

However you can use a different approach:

from pymongo import MongoClient
import json
client = MongoClient('localhost', 27017)
with open('restaurants.json') as f:
    data = json.load(f)
client['myDatabase']['restaurants'].insert_many(data)

If your json file is too big, you can use the subprocess lib to run the command-line program inside a python program. Check some SO answers here or here

Nadav
  • 574
  • 10
  • 24