Partially Update Document to Change Many in MongoDB

In this tutorial, we will learn how to partially update documents in MongoDB to change many or all documents inside our database.

To achieve this, we will use the $addFields and the $set aggregate pipelines with the update() or updateMany() methods in the Mongo shell. If you want a Nodejs version of this tutorial on partially update document to change many, let me know in the comments section at the end of the article.

Creating an application, or learning to create one, entails a lot of trial and error. You may be unclear about what features your apps must have at times. It might be tough to make a decision sometimes.

Then there are situations when you want to completely modify things or perhaps get rid of them for good or maybe even add something.   Well, there may come a time when you wish to newly add a field to a MongoDB document after your project and database are created. Or perhaps, in all of your docs.

This guide will explain to you how to partially update document to change many or even all others in MongoDB. I will illustrate examples to support the explanations. So, let us get this quick and easy guide on how to partially update document to change many, started.

Using $addFields and $set Aggregate Pipelines to Partially Update Document to Change Many in MongoDB

As I mentioned above, I will be using the $addFields and $set aggregate pipelines to easily partially update document to change many documents inside our MongoDB database.

To start, let us first look at the $addFields and $set pipelines syntaxes:

{ $addFields: { newField: expression, ... } }
{ $set: { newField: expression, ... } }

Partially Update Document to Change Many in MongoDB – Examples

Let us get with our guide to easily partially update document to change many documents. We will look at a few examples from here on how to use both $addFields and $set aggregate pipelines in MongoDB.

Partially Update Document to Change Many Using $addFields Pipeline

  • Start up the MongoDB server
  • Choose which database you want to work with and navigate into it:
show dbs
use shoeStore
  • Let us first check what all documents do we have available in our shoes collection:
> db.shoes.find({}).pretty()
{
        "_id" : ObjectId("616714c6461c6d31d88b4d90"),
        "name" : "Dmitri Vu's Highs",
        "price" : 5300,
        "size" : 8,
        "style" : "Chelsea Boots",
        "__v" : 0
}
{
        "_id" : ObjectId("616714c6461c6d31d88b4d91"),
        "name" : "Purple Haven Lavender Kittens",
        "price" : 14610,
        "size" : 9,
        "style" : "Kitten Heels"
}
{
        "_id" : ObjectId("616714c6461c6d31d88b4d92"),
        "name" : "Marlo Wood’s Lite Moccasins",
        "price" : 8880,
        "size" : 9,
        "style" : "Moccasins"
}
{
        "_id" : ObjectId("616714c6461c6d31d88b4d93"),
        "name" : "Myrna's D'Orsays",
        "price" : 9760,
        "size" : 7,
        "style" : "D'Orsay",
        "__v" : 0
}
{
        "_id" : ObjectId("616716ad33e9f410d4cdfded"),
        "name" : "Choco-Lita YMX 60",
        "price" : 3400,
        "size" : 9,
        "style" : "Lita",
        "__v" : 0
}
{
        "_id" : ObjectId("61671f63e97d0a62702d57b5"),
        "name" : "Starvis Thigh Highs Mango Yellow",
        "price" : 10580,
        "size" : 8,
        "style" : "Thigh High Boots"
}

Cool. We can now start working with this collection to partially update document to change many in MongoDB.

  • If you wish to add a new field to all documents by simply inserting it to a single document, you can use the $addFields pipeline. Let us look at an example:
> db.shoes.updateMany({}, [{ $addFields: { onSale: false } }])

{ "acknowledged" : true, "matchedCount" : 6, "modifiedCount" : 6 }
  • Let us check our documents:
> db.shoes.find({}).pretty()

{
        "_id" : ObjectId("616714c6461c6d31d88b4d90"),
        "name" : "Dmitri Vu's Highs",
        "price" : 5300,
        "size" : 8,
        "style" : "Chelsea Boots",
        "__v" : 0,
        "onSale" : false
}
{
        "_id" : ObjectId("616714c6461c6d31d88b4d91"),
        "name" : "Purple Haven Lavender Kittens",
        "price" : 14610,
        "size" : 9,
        "style" : "Kitten Heels",
        "onSale" : false
}
{
        "_id" : ObjectId("616714c6461c6d31d88b4d92"),
        "name" : "Marlo Wood’s Lite Moccasins",
        "price" : 8880,
        "size" : 9,
        "style" : "Moccasins",
        "onSale" : false
}
{
        "_id" : ObjectId("616714c6461c6d31d88b4d93"),
        "name" : "Myrna's D'Orsays",
        "price" : 9760,
        "size" : 7,
        "style" : "D'Orsay",
        "__v" : 0,
        "onSale" : false
}
{
        "_id" : ObjectId("616716ad33e9f410d4cdfded"),
        "name" : "Choco-Lita YMX 60",
        "price" : 3400,
        "size" : 9,
        "style" : "Lita",
        "__v" : 0,
        "onSale" : false
}
{
        "_id" : ObjectId("61671f63e97d0a62702d57b5"),
        "name" : "Starvis Thigh Highs Mango Yellow",
        "price" : 10580,
        "size" : 8,
        "style" : "Thigh High Boots",
        "onSale" : false
}

Great! We have our newly added onSale field on every document. The function has not replaced any old values or fields. With this, whenever you add a new value, you will see the newly added field. This way we have learnt to update eon document to change many others using the $addFields pipeline.

Partially Update Document to Change Many Using $set Pipeline

We will now use the $set pipeline to partially update document to change many in MonoDb.

> db.shoes.updateMany({} , { $set: { freeDelivery : false } } )

{ "acknowledged" : true, "matchedCount" : 6, "modifiedCount" : 6 }
  • Let us now check our documents:
> db.shoes.find({}).pretty()
{
        "_id" : ObjectId("616714c6461c6d31d88b4d90"),
        "name" : "Dmitri Vu's Highs",
        "price" : 5300,
        "size" : 8,
        "style" : "Chelsea Boots",
        "__v" : 0,
        "onSale" : false,
        "freeDelivery" : false
}
{
        "_id" : ObjectId("616714c6461c6d31d88b4d91"),
        "name" : "Purple Haven Lavender Kittens",
        "price" : 14610,
        "size" : 9,
        "style" : "Kitten Heels",
        "onSale" : false,
        "freeDelivery" : false
}
{
        "_id" : ObjectId("616714c6461c6d31d88b4d92"),
        "name" : "Marlo Wood’s Lite Moccasins",
        "price" : 8880,
        "size" : 9,
        "style" : "Moccasins",
        "onSale" : false,
        "freeDelivery" : false
}
{
        "_id" : ObjectId("616714c6461c6d31d88b4d93"),
        "name" : "Myrna's D'Orsays",
        "price" : 9760,
        "size" : 7,
        "style" : "D'Orsay",
        "__v" : 0,
        "onSale" : false,
        "freeDelivery" : false
}
{
        "_id" : ObjectId("616716ad33e9f410d4cdfded"),
        "name" : "Choco-Lita YMX 60",
        "price" : 3400,
        "size" : 9,
        "style" : "Lita",
        "__v" : 0,
        "onSale" : false,
        "freeDelivery" : false
}
{
        "_id" : ObjectId("61671f63e97d0a62702d57b5"),
        "name" : "Starvis Thigh Highs Mango Yellow",
        "price" : 10580,
        "size" : 8,
        "style" : "Thigh High Boots",
        "onSale" : false,
        "freeDelivery" : false
}

This way, we have successfully learned to partially update document to change many documents in MongoDB.

Read More: MongoDB $exists Operator: Ultimate Guide for Beginners

Conclusion

Learn to partially update document to change many or all documents inside a collection in MongoDB.

Noteworthy References

SO Answer

Set – MongoDB

AddFields – MongoDB

Aneesha S
Aneesha S
Articles: 172