Mongodb Simple Update Document Example

by Didin J. on Dec 19, 2016 Mongodb Simple Update Document Example

MongoDB Simple Update Document Example using built in MongoDB functions.

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, options)

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

mongo

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

use exampledb

Update single data.

db.city.update({_id:ObjectId("584a13d5b65761be678d4dd4")}, {"citiName" : "Jakarta Selatan", "provName" : "DKI Jakarta"}, {upsert:true})

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

To update single field or specific fields just use $set function.

db.city.update({_id:ObjectId("584a13d5b65761be678d4dd4")}, {$set: {"citiName":"Jakarta Pusat"}})

This will update a specific field that defined by $set function.

For updating multiple rows just use "multi:true" in options section and don't specify a query.

db.city.update({}, {$set: {"country":"Indonesia"}}, {multi:true})

Above command update all documents and set new field "country" with value "Indonesia".

That it's a simple update document in the MongoDB.

Thanks.

The following resources might be useful for you:

Loading…