在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);
});
答案 0 :(得分:3)
插入完成后,它将返回一个包含所有文档的数组,因此您可以使用length
属性来查找其中的数量:
Employees.insertMany(employees)
.then(function(docs) {
console.log(docs.length);
})
.catch(function(err) {
console.log(err);
});