DEV Community

Cover image for How to do bulk updates in MongoDB
Donald Feury
Donald Feury

Posted on • Originally published at donaldfeury.xyz on

2 1

How to do bulk updates in MongoDB

How to do bulk updates in MongoDB

For a full overview of MongoDB and all my posts on it, check out my overview.

MongoDB provides several ways to update specifically one document and bulk updating is done in almost the same way.

Using the following data:

db.podcasts.insertMany([
    {
        "name": "Tech Over Tea",
        "episodeName": "#75 Welcome Our Hacker Neko Waifu | Cyan Nyan",
        "dateAired": ISODate("2021-08-02"),
        "listenedTo": true,
    },
    {
        "name": "Tech Over Tea",
        "episodeName": "Neckbeards Anonymous - Tech Over Tea #20 - feat Donald Feury",
        "dateAired": ISODate("2020-07-13"),
        "listenedTo": true
    },
    {
        "name": "Tech Over Tea",
        "episodeName": "#34 The Return Of The Clones - feat Bryan Jenks",
        "dateAired": ISODate("2020-10-19"),
        "listenedTo": false
    },
    {
        "name": "Cinemassacre Podcast",
        "episodeName": "AVGN Fan Q&A, Starting a Band, and the Last Year - Cinemassacre Podcast",
        "dateAired": ISODate("2021-08-10"),
        "listenedTo": true
    }
])

Enter fullscreen mode Exit fullscreen mode

Let's update all the "Tech Over Tea" podcasts entry to be marked as listened to using update. In order to update multiple documents using update, we have to pass in an optional argument to tell MongoDB to do that.

db.podcasts.update(
    {name: "Tech Over Tea"},
    {$set: { listenedTo: true} },
    {multi: true}
)

Enter fullscreen mode Exit fullscreen mode

We can achieve the same result using updateMany.

db.podcasts.updateMany(
    {name: "Tech Over Tea"},
    {$set: { listenedTo: true} }
)

Enter fullscreen mode Exit fullscreen mode

AWS Security LIVE!

Tune in for AWS Security LIVE!

Join AWS Security LIVE! for expert insights and actionable tips to protect your organization and keep security teams prepared.

Learn More

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Engage with a sea of insights in this enlightening article, highly esteemed within the encouraging DEV Community. Programmers of every skill level are invited to participate and enrich our shared knowledge.

A simple "thank you" can uplift someone's spirits. Express your appreciation in the comments section!

On DEV, sharing knowledge smooths our journey and strengthens our community bonds. Found this useful? A brief thank you to the author can mean a lot.

Okay