mongoimport: Loading JSON and CSV Files
Learners will import JSON and CSV data files into a collection using mongoimport with various mode flags.
What Is mongoimport?
mongoimport is a command-line tool bundled with the MongoDB Database Tools package. It loads data from JSON, JSONL (JSON Lines / newline-delimited JSON), and CSV files directly into a MongoDB collection. It is the standard way to seed a collection with initial data, migrate from another database, or bulk-load a data export from a third-party system.
Basic JSON Import
The simplest mongoimport command specifies the connection string, database, collection, and input file. By default it expects a JSON array of objects ([{...},{...}]). The --jsonArray flag is required when importing a file that contains a JSON array; omit it for JSONL (one document per line) format.
# Import a JSON array file into the 'products' collection
mongoimport \
--uri 'mongodb://localhost:27017/myapp' \
--collection products \
--file products.json \
--jsonArray
# Output:
# 2024-01-01T12:00:00.000+0000 connected to: mongodb://localhost:27017
# 2024-01-01T12:00:00.100+0000 100 document(s) imported successfullyAll lessons in this course
- mongoimport: Loading JSON and CSV Files
- mongoexport: Dumping Collections to Files
- mongodump and mongorestore for Full Backups
- Seeding Data With Node.js Scripts