Model.insertMany()
Parameters
- doc(s) «Array|Object|*»
[options] «Object» see the mongodb driver options
[options.ordered «Boolean» = true] if true, will fail fast on the first error encountered. If false, will insert all the documents it can and report errors later. An
insertMany()
withordered = false
is called an “unordered”insertMany()
.[options.rawResult «Boolean» = false] if false, the returned promise resolves to the documents that passed mongoose document validation. If
true
, will return the raw result from the MongoDB driver with amongoose
property that containsvalidationErrors
if this is an unorderedinsertMany
.[options.lean «Boolean» = false] if
true
, skips hydrating and validating the documents. This option is useful if you need the extra performance, but Mongoose won’t validate the documents before inserting.[options.limit «Number» = null] this limits the number of documents being processed (validation/casting) by mongoose in parallel, this does NOT send the documents in batches to MongoDB. Use this option if you’re processing a large number of documents and your app is running out of memory.
[options.populate «String|Object|Array» = null] populates the result documents. This option is a no-op if
rawResult
is set.[callback] «Function» callback
Returns:
- «Promise» resolving to the raw result from the MongoDB driver if
options.rawResult
wastrue
, or the documents that passed validation, otherwise
Shortcut for validating an array of documents and inserting them into MongoDB if they’re all valid. This function is faster than .create()
because it only sends one operation to the server, rather than one for each document.
Mongoose always validates each document before sending insertMany
to MongoDB. So if one document has a validation error, no documents will be saved, unless you set the ordered
option to false.
This function does not trigger save middleware.
This function triggers the following middleware.
insertMany()
Example:
const arr = [{ name: 'Star Wars' }, { name: 'The Empire Strikes Back' }];
Movies.insertMany(arr, function(error, docs) {});