Nearby lessons

21 of 22

📥 MongoDB: Import JSON using mongoimport Tool

MongoDB provides command-line tools to manage databases efficiently. One of the most useful tools is mongoimport, which allows importing JSON, CSV, or TSV files directly into MongoDB collections. Before using it, you must install the official MongoDB Database Tools.

🧰 Step 1: Download MongoDB Database Tools

Example01
MongoDB Shell
NoSQL
Database
1// Search on Google:
2"mongodb database tools"
3
4// Click the official MongoDB link:
5https://www.mongodb.com/try/download/database-tools
6
7// Select Options:
8Version: 100.12.0
9Platform: Windows x86_64
10Package: msi
11
12// Click the green "Download" button.
Query Result
JSON Output
✅ A file named "mongodb-database-tools-windows-x86-..." will be downloaded.
📅 Date: 17-04-2023
💾 Size: 67,448 KB
📦 Type: Windows Installer (.msi)
⚙️ These tools include mongoimport, mongodump, mongorestore, and more.

📦 Step 2: Example JSON File — students.json

Example02
MongoDB Shell
NoSQL
Database
1[
2 { "name": "Akshay Kumar", "age": 25 },
3 { "name": "Salman Khan", "age": 30 }
4]
Query Result
JSON Output
💡 The file contains an array of JSON documents to be imported into MongoDB.

⚙️ Step 3: Import JSON Data into MongoDB

Example03
MongoDB Shell
NoSQL
Database
1mongoimport "D:\students.json" -d school -c testing --jsonArray
Query Result
JSON Output
✅ Imports the JSON file into the "school" database and "testing" collection.
🔹 "D:\students.json" → Path to your JSON file
🔹 -d school → Target database
🔹 -c testing → Target collection
🔹 --jsonArray → Used because data is an array of JSON documents

💻 Step 4: Example mongoimport Command in Windows

Example04
MongoDB Shell
NoSQL
Database
1C:\Users\admin> mongoimport "C:\Users\admin\Downloads\Rajesh\Rajesh\test.json" --db school --collection users
Query Result
JSON Output
Connected to: mongodb://localhost/
Progress: [####################] school.users 5.74KB/5.74KB (100.0%)
✅ 10 document(s) imported successfully.
❌ 0 document(s) failed to import.

📄 Step 5: Sample Imported Documents

Example05
MongoDB Shell
NoSQL
Database
1[
2 {
3 "_id": ObjectId("6800e67619d3210623f3fd6d"),
4 "id": 3,
5 "name": "Clementine Bauch",
6 "username": "Samantha",
7 "email": "Nathan@yesenia.net",
8 "phone": "1-463-123-4447",
9 "website": "ramiro.info"
10 },
11 {
12 "_id": ObjectId("6800e67619d3210623f3fd6e"),
13 "id": 4,
14 "name": "Patricia Lebsack",
15 "username": "Karianne",
16 "email": "Julianne.OConner@kory.org",
17 "phone": "493-170-9623 x156",
18 "website": "kale.biz"
19 },
20 {
21 "_id": ObjectId("6800e67619d3210623f3fd6f"),
22 "id": 2,
23 "name": "Ervin Howell",
24 "username": "Antonette",
25 "email": "Shanna@melissa.tv",
26 "phone": "010-692-6593 x09125",
27 "website": "anastasia.net"
28 }
29]
Query Result
JSON Output
✅ Documents imported successfully into the "users" collection.
🧾 Each document has unique _id and user profile fields (id, name, username, email, etc.).