Horje
How to Update Multiple Array Elements in MongoDB?

MongoDB, the popular NoSQL database, offers powerful features for manipulating data, including the ability to update multiple array elements within a document. Efficiently updating multiple elements in an array can be important in various scenarios, such as managing user preferences, and handling inventory quantities.

In this article, we will learn about How to Update Multiple Array Elements in MongoDB by understanding various methods along with the multiple examples and so on.

How to Update Multiple Array Elements in MongoDB?

  • Updating multiple array elements in MongoDB is a common requirement in database operations, often encountered in scenarios such as managing user preferences, tracking inventory, or processing historical data.
  • MongoDB provides powerful methods to update multiple array elements within documents efficiently.
  • updateMany() method in MongoDB to update multiple array elements. Unlike traditional approaches, updateMany() allows us to perform bulk updates across multiple documents, improving performance and scalability.

Examples of How to Update Multiple Array Elements in MongoDB

To understand How to to Update Multiple Array Elements in MongoDB we need a collection and some documents on which we will perform various queries. Here we will consider a collection called members which contains information like Name, Country, Age and Games of the members in various documents.

// Inserting example documents into the database
db.members.insertMany([
{ "Name": "Makin",
"Country": "India",
"Age": 26,
"Games": ["Tennis", "Football"]
},

{ "Name": "Dani",
"Country": "USA",
"Age": 25,
"Games": ["Carroms", "Cricket"]
},

{ "Name": "Carlin",
"Country": "India",
"Age": 24,
"Games": ["Hockey", "Basketball"]
},

{ "Name": "Bobby",
"Country": "Dubai",
"Age": 28,
"Games": ["Tennis", "Football"]
},

{ "Name": "Sundhar",
"Country": "Nepal",
"Age": 27,
"Games": ["Tennis", "Baseball"]
}
]);

Output:

membersCollection

collection created

Example 1: By using the “$set” and “$” positional operators

Let’s Update the ‘Games’ array for the document where Name is ‘Makin’ to replace ‘Tennis’ with ‘Badminton’.

db.members.updateOne(
{ Name: 'Makin' },
{ $set: { 'Games.$[element]': 'Badminton' } },
{ arrayFilters: [ { 'element': 'Tennis' } ] }
)

Output:

{ "acknowledged" : true, "matchedCount" : 1, "modifiedCount" : 1 }

Example 2: Updating Multiple Array Elements based on Array Position

Let’s Update the first two elements of the ‘Games’ array in documents where Country is ‘India’ to ‘Chess’ and ‘Volleyball’ respectively.

db.members.updateMany(
{ Country: 'India' },
{ $set: { 'Games.0': 'Chess', 'Games.1': 'Volleyball' } }
)

Output:

{ "acknowledged" : true, "matchedCount" : 2, "modifiedCount" : 2 }

Example 3: Updating Multiple Array Elements in all Documents based on the Array Filter

Let’s Update all documents to replace all occurrences of ‘Tennis’ in the ‘Games’ array with ‘Swimming’.

db.members.updateMany(
{},
{ $set: { 'Games.$[element]': 'Swimming' } },
{ arrayFilters: [ { 'element': 'Tennis' } ] }
)

Output:

{ "acknowledged" : true, "matchedCount" : 5, "modifiedCount" : 5 }

Example 4: Updating Multiple Specific Array Elements using the $eleMatch operator

Let’s Update documents where Country is ‘India’ to replace occurrences of ‘Tennis’ or ‘Hockey’ in the ‘Games’ array with ‘Badminton’.

db.members.updateMany(
{ Country: 'India' },
{ $set: { 'Games.$[element]': 'Badminton' } },
{ arrayFilters: [ { 'element': { $in: ['Tennis', 'Hockey'] } } ] }
)

Output:

{ "acknowledged" : true, "matchedCount" : 2, "modifiedCount" : 2 }

Conclusion

Overall, Updating multiple array elements in MongoDB is a powerful feature that can simple your database operations. By understanding these techniques discussed in article, you can efficiently manage and manipulate arrays within your MongoDB documents.




Reffered: https://www.geeksforgeeks.org


Databases

Related
How to Query MongoDB with "like"? How to Query MongoDB with "like"?
How to Find MongoDB Records Where Array Field is not Empty? How to Find MongoDB Records Where Array Field is not Empty?
How to Get a Random Record From MongoDB How to Get a Random Record From MongoDB
How to Find MongoDB Records Where the Array Field is Empty? How to Find MongoDB Records Where the Array Field is Empty?
How to Print to Console an Object in a MongoDB Script? How to Print to Console an Object in a MongoDB Script?

Type:
Geek
Category:
Coding
Sub Category:
Tutorial
Uploaded by:
Admin
Views:
13