Sling Academy
Home/MongoDB/MongoDB: 3 Ways to Import JSON/CSV Files to Database

MongoDB: 3 Ways to Import JSON/CSV Files to Database

Last updated: February 02, 2024

Introduction

Transferring data into MongoDB from JSON or CSV files is a common operation that developers and database administrators need to perform. In this guide, we will explore several methods to import or restore data to a MongoDB database, each with an explanation, step-by-step instructions, code examples, and notes regarding performance and use cases.

Using mongoimport Utility

The mongoimport command-line utility is bundled with MongoDB and designed to import content from JSON, CSV, or TSV files into MongoDB.

Steps to Implement:

  1. Ensure MongoDB server is running.
  2. Craft mongoimport command with appropriate flags for your file.
  3. Execute the command in the terminal or command prompt.

Example:

mongoimport --db usersDB --collection contacts --type csv --file /path/to/contacts.csv --headerline

Output:

2023-03-22T11:28:02.193+0000    connected to: mongodb://localhost/
2023-03-22T11:28:02.596+0000    100 document(s) imported successfully. 0 document(s) failed to import.

Notes:

  • This method is suitable for one-time or infrequent imports.
  • It doesn’t support relationships between collections.
  • Large imports can impact database performance.

MongoDB Compass Import Feature

MongoDB Compass is the official GUI for MongoDB and offers an import feature directly within the interface.

Steps to follow:

  1. Open MongoDB Compass and connect to your database.
  2. Select the desired collection for import.
  3. Click on the ‘ADD DATA’ dropdown and select ‘Import File’.
  4. Choose your JSON or CSV file and set the import options.
  5. Click the ‘Import’ button to start the process.

Notes:

  • Compass reduces the need for command-line knowledge.
  • May not be as fast as command-line tools for large imports.
  • Helpful for visually managing data and imports.

Scripting a Node.js Importer

Write a custom Node.js script that reads JSON/CSV files and inserts data into MongoDB using a driver. This approach is highly customizable and can handle complex logic. It is also good for recurring imports with specific requirements.

Steps to Implement:

  1. Setup a Node.js environment and install the MongoDB driver.
  2. Write a script that connects to the database, reads the file, and inserts documents.
  3. Run the Node.js script.

Code Example:

const MongoClient = require('mongodb').MongoClient;
const fs = require('fs');

let uri = 'mongodb://localhost:27017';
let client = new MongoClient(uri);

async function run() {
    try {
        await client.connect();
        const database = client.db('usersDB');
        const collection = database.collection('contacts');
        const contacts = JSON.parse(fs.readFileSync('/path/to/contacts.json', 'UTF-8'));

        const result = await collection.insertMany(contacts);
        console.log(`${result.insertedCount} documents were inserted`);
    } finally {
        await client.close();
    }
}

run().catch(console.dir);

Output:

100 documents were inserted

Notes:

  • Allows for integration with existing Node.js applications and workflows.
  • Can be overkill for simple, one-time operations.
  • Greatest control over data transformation and import logic.

Conclusion

In conclusion, various techniques are available for importing JSON or CSV data into MongoDB, each with its own set of benefits and drawbacks. Choice of method depends on factors such as the complexity of the data, volume, recurrence of the import tasks, and the user’s technical proficiency. mongoimport is ideal for quick and straightforward tasks, MongoDB Compass offers a more user-friendly interface for less technical users, while custom Node.js scripts are perfect for complex import scenarios that require more customization and automation.

Next Article: How to migrate from MySQL to MongoDB

Previous Article: MongoDB: 3 ways to dump/export a database to a JSON or CSV file

Series: MongoDB Tutorials

MongoDB

You May Also Like

  • MongoDB: How to combine data from 2 collections into one
  • Hashed Indexes in MongoDB: A Practical Guide
  • Partitioning and Sharding in MongoDB: A Practical Guide (with Examples)
  • Geospatial Indexes in MongoDB: How to Speed Up Geospatial Queries
  • Understanding Partial Indexes in MongoDB
  • Exploring Sparse Indexes in MongoDB (with Examples)
  • Using Wildcard Indexes in MongoDB: An In-Depth Guide
  • Matching binary values in MongoDB: A practical guide (with examples)
  • Understanding $slice operator in MongoDB (with examples)
  • Caching in MongoDB: A practical guide (with examples)
  • CannotReuseObject Error: Attempted illegal reuse of a Mongo object in the same process space
  • How to perform cascade deletion in MongoDB (with examples)
  • MongoDB: Using $not and $nor operators to negate a query
  • MongoDB: Find SUM/MIN/MAX/AVG of each group in a collection
  • References (Manual Linking) in MongoDB: A Developer’s Guide (with Examples)
  • MongoDB: How to see all fields in a collection (with examples)
  • Type checking in MongoDB: A practical guide (with examples)
  • How to query an array of subdocuments in MongoDB (with examples)
  • MongoDB: How to compare 2 documents (with examples)