To create a new collection in MongoDB from a JSON file using mongosh (MongoDB Shell) on Ubuntu, you can follow these steps:
Step-by-Step Guide
-
Prepare the JSON File
Ensure that your JSON file is properly formatted. For example, if your file contains an array of documents, it should look like this:
[
{ "name": "Alice", "age": 25 },
{ "name": "Bob", "age": 30 },
{ "name": "Charlie", "age": 35 }
]
If your file contains individual JSON objects (one per line), it should look like this:
{ "name": "Alice", "age": 25 }
{ "name": "Bob", "age": 30 }
{ "name": "Charlie", "age": 35 }
-
Install mongoimport (if not already installed)
The mongoimport tool is part of the MongoDB Database Tools package. If it's not installed, you can install it by running:
sudo apt-get update
sudo apt-get install mongodb-database-tools
-
Use mongoimport to Import the JSON File
Open your terminal and run the following command to import the JSON file into a new collection:
mongoimport --uri="mongodb://localhost:27017/<database_name>" --collection=<collection_name> --file=<path_to_json_file> --jsonArray
Replace the placeholders with the appropriate values:
<database_name>: The name of the database where you want to create the collection.
<collection_name>: The name of the new collection.
<path_to_json_file>: The full path to your JSON file.
Example:
mongoimport --uri="mongodb://localhost:27017/myDatabase" --collection=users --file=/home/user/data.json --jsonArray
- The
--jsonArray flag is required if your JSON file contains an array of documents. If your file contains individual JSON objects (one per line), omit this flag.
-
Verify the Import
After the import is complete, you can verify the data by connecting to the database using mongosh:
mongosh
Then, switch to the database and query the collection:
use myDatabase;
db.users.find().pretty();
Alternative: Using mongosh Scripting
If you prefer to use mongosh scripting instead of mongoimport, you can write a script to read the JSON file and insert its contents into a collection.
-
Create a JavaScript Script
Create a file named import.js with the following content:
const fs = require('fs');
const data = JSON.parse(fs.readFileSync('<path_to_json_file>', 'utf8'));
// Connect to the database
const uri = "mongodb://localhost:27017";
const client = Mongo(uri);
const db = client.getDB('<database_name>');
// Insert data into the collection
db.<collection_name>.insertMany(data);
print("Data imported successfully!");
Replace <path_to_json_file>, <database_name>, and <collection_name> with the appropriate values.
-
Run the Script
Execute the script using mongosh:
mongosh import.js
Troubleshooting
- Error: "mongoimport not found": Ensure that the MongoDB Database Tools are installed and added to your system's PATH.
- Error: "JSON parse error": Check the format of your JSON file. It must be valid JSON.
- Authentication Issues: If your MongoDB instance requires authentication, include the
--username, --password, and --authenticationDatabase options in the mongoimport command.
Final Answer
To create a new collection from a JSON file in mongosh on Ubuntu, the recommended approach is to use the mongoimport tool:
mongoimport --uri="mongodb://localhost:27017/<database_name>" --collection=<collection_name> --file=<path_to_json_file> --jsonArray
Alternatively, you can use a mongosh script to achieve the same result.