mongodb update command

MongoDB update command | Complete tutorial in 2022

MongoDB is an incredible No-SQL database, a document-based database, and is used by most companies to store data for a more extended period. Mongodb has many commands available, using which you can easily modify and fetch the documents. This blog will learn how to use the Mongodb update command.

Mongodb update command

With the help MongoDB update command, we can either update multiple documents or a single document. The syntax for the update command is as follows:

db.collection.updateOne(filter, update, options)
db.collection.updateMany(filter, update, options)

where

Filter: The selection criteria for the update. 

Update: The modification to be done

options: Extra option to be applied while updating documents.

Now let’s proceed and see some of the examples of update and update many commands.

MongoDB update command example

In this session, we will be using the update command to update the above document we have inserted into the show’s collection.

Mongodb Data for demo

To demonstrate the usage of the mongodb update command, we will be using the below data.

[
  {
    "title": "The Last Student Returns",
    "meta": { "rating": 9.5, "aired": 2018 },
    "visitors": 400,
    "expectedVisitors": 300,
    "genre": ["thriller", "drama", "action"],
    "location": [
      { 
          "city": "new york",
          "frequency": 3
      },
      {
          "city": "houston",
          "frequency": 5
      }
      ]
  },
  {
    "title": "Supercharged Teaching",
    "meta": {"rating": 9.3, "aired": 2016 },
    "visitors": 700,
    "expectedVisitors": 300,
    "genre": ["thriller", "action"],
    "location": [
      { 
          "city": "bangalore",
          "frequency": 5
      },
      {
          "city": "mumbai",
          "frequency": 2
      }
      ]
  },
  {
    "title": "Teach me if you can",
    "meta": {"rating": 8.5, "aired": 2014 },
    "visitors": 200,
    "expectedVisitors": 250,
    "genre": ["action", "thriller"],
    "location": [
      { 
          "city": "houston",
          "frequency": 6
      },
      {
          "city": "mumbai",
          "frequency": 1
      }
      ]

  },
  {
    "title": "terminator",
    "meta": {"rating": 8.5, "aired": 2014 },
    "visitors": 300,
    "expectedVisitors": 200,
    "genre": ["action", "scify"],
    "location": [
      { 
          "city": "new york",
          "frequency": 4
      },
      {
          "city": "bangalore",
          "frequency": 6
      }
      ]
  }
]

Insert the above data in mongodb shows collection and verify if the data gets appropriately inserted.

> db.shows.insertMany(l)
{
	"acknowledged" : true,
	"insertedIds" : [
		ObjectId("610519295cddf7c92e7e9134"),
		ObjectId("610519295cddf7c92e7e9135"),
		ObjectId("610519295cddf7c92e7e9136"),
		ObjectId("610519295cddf7c92e7e9137")
	]
}
>
> db.shows.find()
{ "_id" : ObjectId("610519295cddf7c92e7e9134"), "title" : "The Last Student Returns", "meta" : { "rating" : 9.5, "aired" : 2018 }, "visitors" : 400, "expectedVisitors" : 300, "genre" : [ "thriller", "drama", "action" ], "location" : [ { "city" : "new york", "frequency" : 3 }, { "city" : "houston", "frequency" : 5 } ] }
{ "_id" : ObjectId("610519295cddf7c92e7e9135"), "title" : "Supercharged Teaching", "meta" : { "rating" : 9.3, "aired" : 2016 }, "visitors" : 700, "expectedVisitors" : 300, "genre" : [ "thriller", "action" ], "location" : [ { "city" : "bangalore", "frequency" : 5 }, { "city" : "mumbai", "frequency" : 2 } ] }
{ "_id" : ObjectId("610519295cddf7c92e7e9136"), "title" : "Teach me if you can", "meta" : { "rating" : 8.5, "aired" : 2014 }, "visitors" : 200, "expectedVisitors" : 250, "genre" : [ "action", "thriller" ], "location" : [ { "city" : "houston", "frequency" : 6 }, { "city" : "mumbai", "frequency" : 1 } ] }
{ "_id" : ObjectId("610519295cddf7c92e7e9137"), "title" : "terminator", "meta" : { "rating" : 8.5, "aired" : 2014 }, "visitors" : 300, "expectedVisitors" : 200, "genre" : [ "action", "scify" ], "location" : [ { "city" : "new york", "frequency" : 4 }, { "city" : "bangalore", "frequency" : 6 } ] }

MongoDB update one

As discussed above update, one command is used to update a single document in the collection. The syntax of the update one command follows the below pattern

db.collection.updateOne(filter, update, options)

Let’s use the updateOne command to update the “visitors”: 750 where “title”: “Supercharged Teaching”.

Before updating the document, it is highly recommended to filter it first. Type the below command to filter the document where“title”: “Supercharged Teaching”.

> db.shows.find({"title": "Supercharged Teaching"}).pretty()
{
	"_id" : ObjectId("610519295cddf7c92e7e9135"),
	"title" : "Supercharged Teaching",
	"meta" : {
		"rating" : 9.3,
		"aired" : 2016
	},
	"visitors" : 700,
	"expectedVisitors" : 300,
	"genre" : [
		"thriller",
		"action"
	],
	"location" : [
		{
			"city" : "bangalore",
			"frequency" : 5
		},
		{
			"city" : "mumbai",
			"frequency" : 2
		}
	]
}

Now, type the below command to update the frequency

> db.shows.updateOne({"title": "Supercharged Teaching"}, {$set: {"visitors": 750}})
{ "acknowledged" : true, "matchedCount" : 1, "modifiedCount" : 1 }

Verify if the document gets updated

> db.shows.find({"title": "Supercharged Teaching"}).pretty()
{
	"_id" : ObjectId("610519295cddf7c92e7e9135"),
	"title" : "Supercharged Teaching",
	"meta" : {
		"rating" : 9.3,
		"aired" : 2016
	},
	"visitors" : 750,
	"expectedVisitors" : 300,
	"genre" : [
		"thriller",
		"action"
	],
	"location" : [
		{
			"city" : "bangalore",
			"frequency" : 5
		},
		{
			"city" : "mumbai",
			"frequency" : 2
		}
	]
}

MongoDB update many

With the MongoDB updateMany command, we can update multiple documents at one go in mongodb. The MongoDB update many commands following the below pattern.

db.collection.updateMany(filter, update, options)

Let’s update the shows document and add a new field “high_visitors”: true where visitors >300

Before updating the document, let’s filter out the document first using the below command

> db.shows.find({"visitors": {$gt:300}}).pretty()
{
	"_id" : ObjectId("610519295cddf7c92e7e9134"),
	"title" : "The Last Student Returns",
	"meta" : {
		"rating" : 9.5,
		"aired" : 2018
	},
	"visitors" : 400,
	"expectedVisitors" : 300,
	"genre" : [
		"thriller",
		"drama",
		"action"
	],
	"location" : [
		{
			"city" : "new york",
			"frequency" : 3
		},
		{
			"city" : "houston",
			"frequency" : 5
		}
	]
}
{
	"_id" : ObjectId("610519295cddf7c92e7e9135"),
	"title" : "Supercharged Teaching",
	"meta" : {
		"rating" : 9.3,
		"aired" : 2016
	},
	"visitors" : 750,
	"expectedVisitors" : 300,
	"genre" : [
		"thriller",
		"action"
	],
	"location" : [
		{
			"city" : "bangalore",
			"frequency" : 5
		},
		{
			"city" : "mumbai",
			"frequency" : 2
		}
	]
}

Superb, now run the below command to update the documents.

 db.shows.updateMany({"visitors": {$gt:300}},{$set: {"high_visitors": true}})
{ "acknowledged" : true, "matchedCount" : 2, "modifiedCount" : 2 }

Verify if the document gets updated

> db.shows.find({"visitors": {$gt:300}}).pretty()
{
	"_id" : ObjectId("610519295cddf7c92e7e9134"),
	"title" : "The Last Student Returns",
	"meta" : {
		"rating" : 9.5,
		"aired" : 2018
	},
	"visitors" : 400,
	"expectedVisitors" : 300,
	"genre" : [
		"thriller",
		"drama",
		"action"
	],
	"location" : [
		{
			"city" : "new york",
			"frequency" : 3
		},
		{
			"city" : "houston",
			"frequency" : 5
		}
	],
	"high_visitors" : true
}
{
	"_id" : ObjectId("610519295cddf7c92e7e9135"),
	"title" : "Supercharged Teaching",
	"meta" : {
		"rating" : 9.3,
		"aired" : 2016
	},
	"visitors" : 750,
	"expectedVisitors" : 300,
	"genre" : [
		"thriller",
		"action"
	],
	"location" : [
		{
			"city" : "bangalore",
			"frequency" : 5
		},
		{
			"city" : "mumbai",
			"frequency" : 2
		}
	],
	"high_visitors" : true
}

MongoDB update all

In this session, we will understand how to update all the documents in one go in the MongoDB collection. The syntax to update all documents in mongodb follows the below pattern.

db.collection.updateMany({}, update, options)

Since we need to update all the documents so we need to pass {} in the filter condition to specify that we want all data to be updated.

Let’s use the updateMany command to add a new field low_visitors”: true in all the documents.

db.shows.updateMany({},{$set: {"low_visitors": true}})
{ "acknowledged" : true, "matchedCount" : 4, "modifiedCount" : 4 }

Verify that the low_visitors field gets added in all the documents.

db.shows.find()
{ "_id" : ObjectId("61cddac45ac17e32a0d6e366"), "title" : "The Last Student Returns", "meta" : { "rating" : 9.5, "aired" : 2018 }, "visitors" : 400, "expectedVisitors" : 300, "genre" : [ "thriller", "drama", "action" ], "location" : [ { "city" : "new york", "frequency" : 3 }, { "city" : "houston", "frequency" : 5 } ], "high_visitors" : true, "low_visitors" : true }
{ "_id" : ObjectId("61cddac45ac17e32a0d6e367"), "title" : "Supercharged Teaching", "meta" : { "rating" : 9.3, "aired" : 2016 }, "visitors" : 750, "expectedVisitors" : 300, "genre" : [ "thriller", "action" ], "location" : [ { "city" : "bangalore", "frequency" : 5 }, { "city" : "mumbai", "frequency" : 2 } ], "high_visitors" : true, "low_visitors" : true }
{ "_id" : ObjectId("61cddac45ac17e32a0d6e368"), "title" : "Teach me if you can", "meta" : { "rating" : 8.5, "aired" : 2014 }, "visitors" : 200, "expectedVisitors" : 250, "genre" : [ "action", "thriller" ], "location" : [ { "city" : "houston", "frequency" : 6 }, { "city" : "mumbai", "frequency" : 1 } ], "low_visitors" : true }
{ "_id" : ObjectId("61cddac45ac17e32a0d6e369"), "title" : "terminator", "meta" : { "rating" : 8.5, "aired" : 2014 }, "visitors" : 300, "expectedVisitors" : 200, "genre" : [ "action", "scify" ], "location" : [ { "city" : "new york", "frequency" : 4 }, { "city" : "bangalore", "frequency" : 6 } ], "low_visitors" : true }

Conclusion

I hope you like this tutorial about the MongoDB update command. We started with introducing the mongoDB update command, and finally, we used the updateOne and updateMany commands to update one and multiple documents in the show’s collection.

Please let me know if you face any issues while following this tutorial. I will be more than happy to hear from you.

More to read?

MongoDB commands you should be aware of

MongoDB with python | pymongo

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top