How to Insert Data into MongoDB with Node.js and Mongoose

2020/07/113 min read
bookmark this
Responsive image

Table of Contents

  1. Introduction
  2. Getting Started
  3. Connect to MongoDB
  4. Define the MongoDB Schema
  5. Insert New Data into MongoDB
  6. Insert Data into MongoDB by Using mongoose.create
  7. Conclusion

Introduction

This blog shows a tutorial on how to insert data into MongoDB by using Node.js with Mongoose. Mongoose is the Object Data Modeling library for MongoDB and Node.js. It provides functions for schema, query building, and more. If you don't use Mongoose to access MongoDB with Node.js, then you can use the MongoDB package for Node.js directly.

Getting Started

First, initialize a new npm package. We'll create a new package and a new file, and add our code for how to insert data into MongoDB.

npm init -y

Next, let's install the mongoose package. This will be the only package we'll use for this tutorial.

npm install --save mongoose

Create an index.js file. We'll add our code to this file to connect to MongoDB and insert data into the database.

Connect to MongoDB

Now, the following is a sample code to connect to MongoDB. Assume you already have MongoDB set up locally and have a database called your-database.

const {connect} = require('mongoose');

connect('mongodb://localhost:27017/your-database')
.then(() => {
    console.info('connect successfully')
})
.catch(() => {
    console.error('connection error');
});

Define the MongoDB Schema

Here, we'll define our collection in MongoDB. The collection name will be my-collection-name.

let localModel = new Schema({
    key: {
        type: String
    },
    value: {
        type: String
    },
    culture: {
        type: String
    }
})

let localSchema = model('localization', localModel, 'my-collection-name');

Insert New Data into MongoDB

Now, this is the final step. We'll insert new data into the collection using insertMany. After this code runs, it should insert new data into the collection my-collection-name.

localSchema.insertMany({
    key: 'test-key',
    value: 'key description',
    culture: 'en-US'
}).then(function() {
    console.info('insert new data successfully')
}).catch(function() {
    console.error('has error when insert new data');
})

Insert Data into MongoDB by Using mongoose.create

Another way to insert data is the following, but which one is better? Should you use insert or create?

const createAsync = async() => {
    await localSchema.create({
        key: 'test-key-2',
        value: 'key2 description',
        culture: 'en-US'
    });
}

createAsync();

It looks like it is better to use insertMany because it's faster and has fewer issues. I did some research by inserting 100,000 records into MongoDB.

When I executed the following code on my macOS, the insert-timer time was 11922.764ms.

const createTestMillionAsync = async() => {
    console.time('insert-timer')
    let testTargetList = [];
    for (let index = 0; index < 100000; index++) {

        testTargetList.push({
            key: `test-key-${index}`,
            value: `my test value : ${index}`,
            culture: 'en-US'
        });

    }

    await localSchema.insertMany(testTargetList);

    console.timeEnd('insert-timer');
}

createTestMillionAsync();

However, when I tried inserting 100,000 records with create, I got a timeout error. The default connection timeout is 10000ms.

const createTestMillionAsync = async(roopLimit) => {
    let timerName = `insert-timer-${roopLimit}`;
    console.time(timerName)
    let testTargetList = [];
    for (let index = 0; index < roopLimit; index++) {

        testTargetList.push({
            key: `test-key-${index}`,
            value: `my test value : ${index}`,
            culture: 'en-US'
        });

    }

    await localSchema.insertMany(testTargetList);

    console.timeEnd(timerName);
}

createTestMillionAsync(100000);

So I extended the connectionTimeout from 10000 to 50000 and re-ran the following combinations. You can see that insertMany is 7 times faster than create.

Records mongoose.create mongoose.insertMany
100000 75261.921ms 11869.793.ms
500000 Error: Javascript heap out of memory 61775.343ms

Conclusion

Above is how you can insert new data into MongoDB with Node.js and Mongoose. It looks like it's better to use insertMany for better performance.