JAN
JAN

Reputation: 21885

Mongoose InsertMany - how to get count of inserted documents?

After InsertMany with Mongoose , how can we know how many documents have succeeded to get into the DB ?

  Employees.insertMany(employees)
        .then(function(docs) {
          // do something with docs
          console.log("Number of documents inserted: " + ... );
        })
        .catch(function(err) {
          // error handling here
          console.log("Failed to insert Bulk..." + err.message);
        });

Upvotes: 2

Views: 2272

Answers (1)

user12251171
user12251171

Reputation:

When the insert finishes, it returns an array with all the documents so you can use the length property to find out how many there are:

Employees.insertMany(employees)
  .then(function(docs) {
    console.log(docs.length);
  })
  .catch(function(err) {
    console.log(err);
  });

Upvotes: 4

Related Questions