1

I have a mongodump from a mongodb cloud.Its available locally on my pc.Now I want to create a new mongdb database. And access files locally using queries.I used pymongo for this. Here is my code:

    import pymongo
    client = pymongo.MongoClient('mongodb://localhost:27017')
    database = client['db_name']
    mongorestore -d db_name adress_of_mongodump
    //mongorestore -d database C:\Users\Lenovo\Documents\home\ubuntu\dump2020

but i'm unable to access the mongodump.Instead getting a syntax error on mongorestore

nandi1596
  • 59
  • 7

1 Answers1

0

pymongo does not include a mongorestore capability. If you want to run this from python, you must install the mongorestore to the machine, and execute it with the subprocess.run() command; something like:

import subprocess

address_of_mongodump = 'c:/address_of_mongodump'
command = ['mongorestore', '--uri', 'mongodb://localhost:27017', f'--archive={address_of_mongodump}']

subprocess.run(command, capture_output=True)
Belly Buster
  • 8,224
  • 2
  • 7
  • 20