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

Updated: February 2, 2024 By: Guest Contributor Post a comment

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.