Use insertMany
function to insert many documents. This sends only one operation to the server and Mongoose
validates all the documents before hitting the mongo server. By default Mongoose
inserts item in the order they exist in the array. If you are ok with not maintaining any order then set ordered:false
.
Important - Error handling:
When ordered:true
validation and error handling happens in a group means if one fails everything will fail.
When ordered:false
validation and error handling happens individually and operation will be continued. Error will be reported back in an array of errors.