Categories
JavaScript Answers

How to save multiple documents concurrently in Mongoose and Node.js?

Spread the love

To save multiple documents concurrently in Mongoose and Node.js, we call the create method.

For instance, we write

const array = [{ type: "jelly bean" }, { type: "snickers" }];
Candy.create(array, (err, jellybean, snickers) => {
  if (err) {
    //...
  }
});

to call the create method with the array to write the entries into the collection.

And then we get the written entries from the parameters after err.

err is the error if there is any.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

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