What if you could add many items to your list with just one simple command?
Why $each modifier with $push in MongoDB? - Purpose & Use Cases
Imagine you have a list of favorite movies stored in a database for each user. Now, you want to add multiple new movies to a user's list one by one manually.
Adding each movie separately means many database calls, which is slow and can cause mistakes like missing some movies or adding duplicates. It's like writing down each movie title on a paper one by one instead of all at once.
The $each modifier with $push lets you add many items to an array in a single, simple command. This saves time, reduces errors, and keeps your data neat and consistent.
db.users.updateOne({_id: 1}, {$push: {movies: 'Inception'}})
db.users.updateOne({_id: 1}, {$push: {movies: 'Interstellar'}})db.users.updateOne({_id: 1}, {$push: {movies: {$each: ['Inception', 'Interstellar']}}})You can efficiently add many items to a list in one go, making your database updates faster and your code cleaner.
A streaming app adds multiple new recommended shows to a user's watchlist at once, improving performance and user experience.
Manually adding items one by one is slow and error-prone.
$each with $push adds multiple items in a single update.
This makes database operations faster and simpler.