To import a JSON file into MongoDB, you can use the mongoimport
tool. Here's a step-by-step guide:
Install
mongoimport
: If you haven't already installedmongoimport
, you can download it from the MongoDB website. Starting from MongoDB 4.4,mongoimport
is released separately from the MongoDB server.Prepare your JSON file: Ensure your JSON file is properly formatted. Each line in the file should represent a single JSON document.
Use
mongoimport
to import the JSON file: Open your terminal and run the following command:
mongoimport --uri "mongodb+srv://<username>:<password>@<cluster-url>/<database>" --collection <collection> --file <path-to-json-file> --jsonArray
Replace <username>
, <password>
, <cluster-url>
, <database>
, <collection>
, and <path-to-json-file>
with your actual MongoDB credentials, cluster URL, database name, collection name, and the path to your JSON file.
Here's an example command:
mongoimport --uri "mongodb+srv://user:password@cluster0.mongodb.net/mydatabase" --collection mycollection --file /path/to/myfile.json --jsonArray
This command will import the JSON data into the specified collection in your MongoDB database.
Would you like more details on any of these steps?
References