Mongoose Insert

Have a Database Problem? Speak with an Expert for Free
Get Started >>

Introduction

If you have ever worked with MongoDB, you would have definitely used the insert() method to insert data documents in a collection. This method is the most common method used to insert data into a MongoDB collection. But when using mongoose, the insert() method does not work. Why? simply, because it doesn’t exist in mongoose.

Mongoose provides two different methods for inserting data in MongoDB collection. These methods are create() and insertMany().

create()

The create() method is similar to the insert() method. It is used to insert a single document in a collection. Let’s understand the create() method with the help of an example.

We have a details collection and currently, it contains no documents. Observe the following route handler.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
router.route("/add").post(function(req, res) {
  var object = {
    name: "John",
    age: 21
  };

  detail.create(object, function(err, result) {
    if (err) {
      res.send(err);
    } else {
      console.log(result);
      res.send(result);
    }
  });
});

We have an object inside this route handler.

1
2
3
4
var object = {
  name: "John",
  age: 21
};

Then we used the create() method to insert this object in the collection. The create() method has two arguments – the object that is to be inserted and a callback function. The callback function, in turn, has two arguments – error (if any occurs) and the returned value.

Let’s see what is inside this callback function.

1
2
3
4
5
if (err) {
  console.log(err);
} else {
  console.log(result);
}

There is an if-else statement. If any error occurs, it will be handled. In the else statement, we used a console statement to print the returned value (result). Let’s see what happens here when we execute this route.

I am using the postman tool for API testing. You can download the tool from https://www.getpostman.com.

Image from Gyazo

The create() method returns an object. This object contains the fields that were inserted, plus the auto-generated _id field. There is one more field, v. It is the versionKey auto-generated when mongoose insert a document. The default is v. This will not be part of the document inserted into the collection. It contains the internal revision of the document.

So this is how we insert a single document in a collection using the create() method. But sometimes, we may need to add multiple documents at a time in the collection. Let’s try to add an array of objects using the create() method.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
var object = [
  {
    name: "John",
    age: 21
  },
  {
    name: "Sam",
    age: 25
  },
  {
    name: "Lisa",
    age: 33
  }
];

Let’s see what is printed in the console.

Image from Gyazo

Everything works fine.

InsertMany()

We can also use the insertMany() method to insert a document or multiple documents into a collection. Let’s see how can we use this method in mongoose.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
router.route("/add").post(function(req, res) {
  var object = {
    name: "John",
    age: 21
  };

  detail.insertMany(object, function(err, result) {
    if (err) {
      console.log(err);
    } else {
      console.log(result);
    }
  });
});

Let’s look at the output.

Yes! document is inserted into the collection.

There is only one difference between these two methods. The create method returns a single object when single document is inserted and the insertMany() method always returns an array of objects, no matter how many objects are inserted. But this isn’t any major difference.

Performance-wise, the insertMany() method is faster than the create() method.

Conclusion

So there is no method named insert() in mongoose. Instead, we have two methods that we discussed in this article. We can insert single or multiple documents using any of these methods. Both of these methods are almost the same, but the insertMany() method is faster.

Pilot the ObjectRocket Platform Free!

Try Fully-Managed CockroachDB, Elasticsearch, MongoDB, PostgreSQL (Beta) or Redis.

Get Started

Keep in the know!

Subscribe to our emails and we’ll let you know what’s going on at ObjectRocket. We hate spam and make it easy to unsubscribe.