NodeJS, Express, Mongoose and MongoDB RESTful Web Service – POST Request [Part 2]

Hey guys, welcome back to my blog. In my previous post we discussed about saving the document to mongodb database using save() method. But there are 2 more methods available to for saving the documents which create() and insertMany(). So in this post, let’s discuss those two methods.

  • create() – This will take an object/document to insert and it will return promise. so we will use then() to handle the promise

This is a shortcut for saving one or more documents to the database. Blog.create(docs) does new Blog(doc).save() for every doc in docs. This function triggers the save() middleware.'/blogs', (req, res) => {
    Blog.create(req.body).then((blog) => {
    }).catch((error) => {
  • insertMany() – This will take an array of objects/documents to insert. This will return a promise.

Shortcut for validating an array of documents and inserting them into MongoDB if they’re all valid. This function is faster than .create() because it only sends one operation to the server, rather than one for each document.

Mongoose always validates each document before sending insertMany() to MongoDB. So if one document has a validation error, no documents will be saved, unless you set the ordered option to false.

This function does not trigger save() middleware.'/blogs', (req, res) => {
    Blog.insertMany(req.body).then((blogs) => {
    }).catch((error) => {

That’s it for this post. In the next post, we will discuss about the updating a document.

You can find the github repository here

About the author

Bushan Sirgur

Hey guys, I am Bushan Sirgur from Banglore, India. Currently, I am working as an Associate project in an IT company.

View all posts

Leave a Reply

Your email address will not be published. Required fields are marked *