MongoDB is an open-source database management system that allows its users to add downloads and connect them easily with the server. As the name implies, like other databases, it will store data and will allow users to manage it. For this purpose, it has several functions (i.e. updateMany()). UpdateMany() is the command line function that is used to modify documents in the collection of MongoDB depending on the query we used. Modification is always related to either replacing the existing data or adding a new field in the record. Our current topic refers to the updateMany() feature in MongoDB. Let us start with the implementation to see how this command is relatable to each scenario.
When you open the terminal, you are now supposed to write any command regarding MongoDB. If you are not sure on the database you are connected with, simply write ‘db’. This will bring the name of the database.
The database will be either the default or a user-defined db. If you have not created a database of your own, then most probably MongoDB will use the default database, ‘test’. But to use the database of your own, you are required to use a command of ‘use’ with the database name.
Hence, demo is the user-defined database. MongoDB terminal will respond to which the database is switched from the test to the demo database.
Once you are done with database connectivity and creatione, your concern will rely on data entry. Similar to other databases, MongoDB also offers containers to store data. These are COLLECTIONS. Let’s see how collections are formed by using the name ‘test’.
The createcollection command with the db keyword is used to create a collection along with the name in parenthesis. Make sure that the collection is created in the same database you want to use in the future. Because while creating a collection, the database name is not specified.
The response of MongoDB is ‘ok’, which means the collection is created successfully without any exception. We had already added data to the collection. Three attributes are added and assigned with the values respectively. To see all data, use the find() command.
This command will fetch all the records of the test collection.
You can observe that 4 rows are entered into the collection.
Sometimes, you have entered the wrong data, or you need to add more fields to the record. In other words, you need modification of data. So, it is done through the UPDATE command.
MongoDB provides a facility to update the existing documents. There are two types to update the documents.
- db.collection.updateOne() – It updates a single attribute in a collection.
- db.collection.updateMany() – It is used to update many attributes through a single command, in the collection.
Note: Both Update types are useful, but it is recommended by the developers to use UpdateManny() as it consumes less time and makes the code shorter.
As our topic under discussion is updateMany, so we will go with it by describing the syntax.
Here db denotes the current database and collection_name is the collection on which you want to apply the update command. Mainly there are three arguments in the function of the updateMany parameter.
- Filter: It acts the same as the find() method. It shows the selection criteria for the update.
- Document: It contains the value you want to add in the row or the one that can be used for replacement.
- Options: It is an optional value that can be removed.
As we have created a collection and added sample data to it already, let’s apply the updateMany command on the collection.
Example 1: Update an existing record
This example refers to the modification of an already existing value. For instance, the filter portion of the command will find the name attribute. When the match is found, the value of the age feature is replaced.
In response to the command, MongoDB will acknowledge the query as true and will show that one match was found in the whole record. Then, the concerned row is modified.
To see the record we will use the find () command
As a result, you will notice that the age of David was 26 when the record was entered but on applying the updateMany() function, the age feature is replaced with 20.
Example 2: Add a new attribute to the existing record
Now we will update a single field in the record of more than one person. Previously, we have used a name that is a unique attribute. It specifies only a single person. To target more, we have selected a section attribute. This time we will not modify the existing record, but the new one will be added. The attribute is “team” with a value. This will be added only in those rows that have section A.
The command will return that 2 rows are modified after finding the match. On using the find() function, you will get:
Example 3: Modify all record
If you want to add a new field in each row, we do not use any attribute in the filter of the command but empty brackets are mentioned.
So the new eligibility attribute will be added in all rows.
This discussion was to provide the use of the updateMany() command in the MongoDB database. We gave a brief introduction to the database, collections, and insertion of data because they are the prerequisites for applying the updateMany() command. The update command can be further used in two subfields; Updateone() and UpdateMany(). UpdateMany is used in detail by using some examples that assist in learning and understanding this concept.