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

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

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

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

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

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.).
Keep Going!MongoDB - Backup & Restore Database