Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between insert(), insertOne(), and insertMany() method?

Tags:

mongodb

nosql

What's the difference between insert(), insertOne(), and insertMany() methods on MongoDB. In what situation should I use each one?

I read the docs, but it's not clear when use each one.

like image 313
Marcos Mendes Avatar asked Apr 22 '16 11:04

Marcos Mendes


People also ask

What is the difference between insertOne and insertMany?

With insertOne you can insert one document into the collection. insertMany accepts an array of documents and these are inserted. The insert (the method from older versions) takes a single document by default, and there is an option to insert multiple documents supplied as an array.

Is insertMany faster than insertOne?

If you have multiple documents to insert, insertMany is better, faster & recommended.

What is the difference between insert and create in Mongodb?

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.


1 Answers

What's the difference between insert(), insertOne() and insertMany() methods on MongoDB

  • db.collection.insert() as mentioned in the documentation inserts a document or documents into a collection and returns a WriteResult object for single inserts and a BulkWriteResult object for bulk inserts.

    > var d = db.collection.insert({"b": 3}) > d WriteResult({ "nInserted" : 1 }) > var d2 = db.collection.insert([{"b": 3}, {'c': 4}]) > d2 BulkWriteResult({         "writeErrors" : [ ],         "writeConcernErrors" : [ ],         "nInserted" : 2,         "nUpserted" : 0,         "nMatched" : 0,         "nModified" : 0,         "nRemoved" : 0,         "upserted" : [ ] }) 
  • db.collection.insertOne() as mentioned in the documentation inserts a document into a collection and returns a document which look like this:

    > var document = db.collection.insertOne({"a": 3}) > document {         "acknowledged" : true,         "insertedId" : ObjectId("571a218011a82a1d94c02333") } 
  • db.collection.insertMany() inserts multiple documents into a collection and returns a document that looks like this:

    > var res = db.collection.insertMany([{"b": 3}, {'c': 4}]) > res {         "acknowledged" : true,         "insertedIds" : [                 ObjectId("571a22a911a82a1d94c02337"),                 ObjectId("571a22a911a82a1d94c02338")         ] } 

In what situation should I use each one?

The insert() method is deprecated in major driver so you should use the the .insertOne() method whenever you want to insert a single document into your collection and the .insertMany when you want to insert multiple documents into your collection. Of course this is not mentioned in the documentation but the fact is that nobody really writes an application in the shell. The same thing applies to updateOne, updateMany, deleteOne, deleteMany, findOneAndDelete, findOneAndUpdate and findOneAndReplace. See Write Operations Overview.

like image 101
styvane Avatar answered Sep 21 '22 13:09

styvane