22

I want do a batch insert job in MongoDB and I found two ways in mongoose:

One way is use insert:

dataArr = [
   {
       id: "",
       name: ""
   }
   {
       id: "",
       name: ""
   }
]

Collection.insert(dataArr)

and another way is Model.create:

Model.create(dataArr)

both could complete the batch insert job, but what's the difference between them?

Which one is more efficiency?

robertklep
  • 198,204
  • 35
  • 394
  • 381
hh54188
  • 14,887
  • 32
  • 113
  • 184
  • I'm always a big supporter of actually diving in and tracing through the code. It's one of the biggest benefits to open source. – WiredPrairie Oct 31 '13 at 11:18
  • Also see [this answer](http://stackoverflow.com/a/24848148/778272) to a previous question regarding differences in **performance** of both approaches. – Lucio Paiva Jan 23 '15 at 03:37

4 Answers4

39

In Mongoose, there is Model.create and Collection.insert (the latter isn't strictly part of Mongoose, but of the underlying MongoDB driver).

According to the Mongoose developer, they are basically the same when called with an array of documents, although looking at the code makes me think that there are subtle differences (warning: I haven't looked at the code that well so I might be mistaken about the following):

  • using Model.create will call any validators/hooks declared on your schema;
  • Model.create does a .save for each document in the array, resulting in N database calls (where N is the number of documents in the array); Collection.insert performs one large database call;
robertklep
  • 198,204
  • 35
  • 394
  • 381
  • 2
    As of Mongoose v3.8.12, ``Model.collection.insert`` runs **much** faster than ``Model.create``. You should take it into account if doing bulk inserts. See http://stackoverflow.com/a/24848148/778272. – Lucio Paiva Jul 20 '14 at 06:56
  • Can you give a real live example with this method. Using `Model.collection` in my code does nothing. Defining your collection in the mongoos schema with {collection: 'myName'} works, but accessing it, don't. – Stephan Kristyn Sep 17 '14 at 15:45
  • 5
    There's another important difference worth being aware about. `Model.create` when validating will also convert string ObjectId's to actual ObjectId's if the schema specifies it as the type of the field, whereas `Model.collection.insert` does not so it will insert strings as plain strings. – Adam Reis Jul 23 '16 at 20:18
  • @AdamBuczynski good point. Basically, by using the underlying MongoDB driver a lot of what Mongoose offers will be circumvented (which explains why it's much faster, but it _is_ something to be aware of). – robertklep Jul 24 '16 at 08:18
1

according to what i've read, Collection.insert is a function of mongoDB driver it's much faster when inserting big amounts of data like millions or such at the cost that it bypasses mongoose validations.

handle with care

chuni
  • 136
  • 1
  • 14
0

They loosely mean the same thing. You can use either of them.

Himansh
  • 879
  • 9
  • 15
0

checking the documentation for insertMany, apparently it's faster that create as it only sends one operation to the server, rather than one for each document.

insertMany documentation, one thing to note is that insertMany does not trigger save middleware, while on the other hand, create does.

WadeeSami
  • 59
  • 4