mongoDB – Update a document in database

As in SQL We use update command for updating a record,  We do the same in No SQL databases. DB. We use update command. This approach is known as wholesale update of a document.

Suppose we have a document in users collection.

Following is Syntax of update.

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

Query is like where clause in SQL, it will specify which document to select.  and second array will be the values to update. This will replace every field in document with the update value except _id field.

 

This query will update document where name =”Mike” with  name= “Thompson” and  salary = 5000. Document will be in this form

Consider another example

if we run this query

Then document will become as

 

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.