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

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.