Model.create() vs Model.collection.insert(): a faster approach
Model.create()
is a bad way to do inserts if you are dealing with a very large bulk. It will be very slow. In that case you should use Model.collection.insert
, which performs much better. Depending on the size of the bulk, Model.create()
will even crash! Tried with a million documents, no luck. Using Model.collection.insert
it took just a few seconds.
Model.collection.insert(docs, options, callback)
docs
is the array of documents to be inserted;options
is an optional configuration object – see the docscallback(err, docs)
will be called after all documents get saved or an error occurs. On success, docs is the array of persisted documents.
As Mongoose’s author points out here, this method will bypass any validation procedures and access the Mongo driver directly. It’s a trade-off you have to make since you’re handling a large amount of data, otherwise you wouldn’t be able to insert it to your database at all (remember we’re talking hundreds of thousands of documents here).
A simple example
var Potato = mongoose.model('Potato', PotatoSchema);
var potatoBag = [/* a humongous amount of potato objects */];
Potato.collection.insert(potatoBag, onInsert);
function onInsert(err, docs) {
if (err) {
// TODO: handle error
} else {
console.info('%d potatoes were successfully stored.', docs.length);
}
}
Update 2019-06-22: although insert()
can still be used just fine, it’s been deprecated in favor of insertMany()
. The parameters are exactly the same, so you can just use it as a drop-in replacement and everything should work just fine (well, the return value is a bit different, but you’re probably not using it anyway).
Reference
- Mongo documentation
- Aaron Heckman on Google Groups discussing bulk inserts