猫鼬InsertMany-如何获取插入文档的数量?

时间:2019-10-24 16:09:45

标签: node.js mongodb mongoose

在InsertMany和Mongoose之后,我们如何知道有多少文档成功进入数据库?

  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);
        });

1 个答案:

答案 0 :(得分:3)

插入完成后,它将返回一个包含所有文档的数组,因此您可以使用length属性来查找其中的数量:

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