Mongodb Simple Update Document Example

by Didin J., updated on Aug 10, 2019 Mongodb Simple Update Document Example

MongoDB Simple Update Document Example using built in MongoDB functions to modify documents in a collection

MongoDB Simple Update Document Example using built-in MongoDB functions to modify documents in a collection. This is a little tutorial or reference about populating data or collections using MongoDB update. One of the most important in the MongoDB operation is to update the Document or collection.

Jumps to the examples:

Today I will show you an example of MongoDB Simple Update Document. Function structure of update document just like this.

db.collection.update(query, update operator, options)

That means:

  • db - Database operation prefix
  • collection - a collection that will be update
  • update - a MongoDB operation to update a collection
  • query - determine which record that will be update
  • update - a set of operators for update the collection
  • option - optional operator that support the update operator

Don't waste your time, just go to this example. Start your MongoDB update example.


Open your database, mine is "exampledb" (this DB created in previous MongoDB tutorial).

use exampledb

Update Single Data

This operation is a simple or basic operation of the update operator. Its implement above update operator pattern.{_id:ObjectId("584a13d5b65761be678d4dd4")}, {"citiName" : "Jakarta Selatan", "provName" : "DKI Jakarta"}, {upsert:true})

Above command update a single document base on id, "upsert: true" option use to check if there is no matching _id it will create a new document.

MongoDB Update a Specific Fields

To update a single field or specific fields just use the $set operator.{_id:ObjectId("584a13d5b65761be678d4dd4")}, {$set: {"citiName":"Jakarta Pusat"}})

This will update a specific field of "citiName" by value "Jakarta Pusat" that defined by $set operator.

Update Multiple Rows or Records

For updating multiple rows or records just use "multi:true" in the options section and don't specify a query using empty bracket "{}".{}, {$set: {"country":"Indonesia"}}, {multi:true})

Above command update, all documents and set a new field "country" with the same value "Indonesia" for all city collection.

Incremental Value Update

MongoDB update operation has incremental value update using $inc operator. Its separate operation from the $set operator.
   { _id: 123 },
     $inc: { population: 100 },

That operation will increase the population field value of city collection by 100 based on the row or record with an ID 123.

Remove Fields from All or Specific Collection Records

MongoDB update operator also can remove fields from all or specific collection records. { _id: 123 }, { $unset: { cityCode: 1 } } )

That update operation will remove the "cityCode" field from the city collection what has an ID 123. To remove all collection records change the query to empty bracket "{}".

Replace All Fields

Mongo update operation can replace all fields at a single operation.
   { cityCode: "BDG" },
     citiName: "Bandung",
     population: 2000000,
     province: "West Java",
     area: "2345KM2"

That Mongo update operation will replace all fields value of city collection except the ID value.

That it's a simple Mongo update document or documents of the collection.

That just the basic. If you need more deep learning about MongoDB or related you can take the following cheap course: