mongodb update query

The following two tabs change content below.
Prasad Kharkar is a java enthusiast and always keen to explore and learn java technologies. He is SCJP,OCPWCD, OCEJPAD and aspires to be java architect.

Latest posts by Prasad Kharkar (see all)

We have seen insert and find queries previously, this article will deal with mongodb update query. Just like sql statements, mongodb update query is also used to update documents with certain conditions.

Mongodb update query:

Syntax for mongodb update query is like below

it takes three parameters,

  • query criteria which is similar to WHERE clause in sql
  • update operations which are the fields to be updated
  • options which specify upsert, multi and writeConcern properties.

Here we will see some basic of mongodb update query. Consider following code.

Note the structure of query as depicted in diagram

mongodb update query
mongodb update query

when you run this, you will see something like below.

i.e. mongodb update query returns and object of type WriteResult which stores information about the number of documents matched, upserted and modified, we will see about them later in detail.

Now this query will update only one document as we have not specified the multi attribute to true. To update all documents matching certain criteria, we need to specify multi=true. here is the update one

setting multi=true for mongodb update query will update all the documents with matching criteria. When you run this, you can confirm with WriteResult object as below.

 

 

Share Button

Prasad Kharkar

Prasad Kharkar is a java enthusiast and always keen to explore and learn java technologies. He is SCJP,OCPWCD, OCEJPAD and aspires to be java architect.

Leave a Reply

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