Insert vs. InsertMany and Update vs. UpdateMany

Hi All,

I am learning MongoDB had have a very basic question.

What is the difference between the following Collection methods, since they appear to do the same things:
db.collection.insert() vs. db.collection.insertMany()
db.collection.update() vs. db.collection.updateMany()

Thanks,
Dave

Hello @David_Geyer,

Good to know about learning data operations on MongoDB! Here is some info.

The update method is from older versions of MongoDB (for example, in v2.2 or earlier; and current version is 4.4), and by default it updates one document only; the first document that matches the query filter. There is an option to specify that the update can happen in multiple documents matching the query filter. The updateOne and updateMany methods are introduced in MongoDB v3.2 and their method names clearly state their function.

Similar is the case with the insert, insertOne and insertMany methods. 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.

Note that the update and insert methods also have the newer features, and can be used as you like.

You can refer (syntax and examples) the above methods in the MongoDB database server documentation for the latest as well as the older (a.k.a. legacy) versions at:

1 Like

Thanks Prasad!

Regarding the 4.4 Manual, I have been relying heavily on it, but it is not very user friendly. Do you have any suggestions regarding books, tutorials, etc. that would help to make the learning process easier?

Thanks,
Dave

Hi @David_Geyer,

For the first time users the manual can be little overwhelming - the size, the number of topics and the product features. After little bit of exposure it will be alright (I feel the documentation is quite comprehensive).

Another good way to learn is from the MongoDB University - the courses are free and online video based. There are basic / entry level courses one can start with. In addition, on the top of this page there are links to various resources - tutorials, webinars, blog posts, etc., you can benefit from. That said, one of the things I find useful in learning is by trying the code examples and studying posts/questions (for example, on this forum).

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.