MongoDB - Java

MongoDB - PHP

MongoDB - Advanced

MongoDB - Useful Resources

MongoDB - Update Document



MongoDB's update() and save() methods are used to update document into a collection. The update() method updates the values in the existing document while the save() method replaces the existing document with the document passed in save() method.

MongoDB Update() Method

The update() method updates the values in the existing document.

Syntax

The basic syntax of update() method is as follows −

>db.COLLECTION_NAME.update(SELECTION_CRITERIA, UPDATED_DATA)

Example

Consider the mycol collection has the following data.

[
  {
    _id: ObjectId('6964b8da25c4ea6b911e2629'),
    title: 'MongoDB Overview',
    description: 'MongoDB is no SQL database',
    by: 'tutorials point',
    url: 'http://www.tutorialspoint.com',
    tags: [ 'mongodb', 'database', 'NoSQL' ],
    likes: 100
  },
  {
    _id: ObjectId('6964b8da25c4ea6b911e262a'),
    title: 'NoSQL Database',
    description: "NoSQL database doesn't have tables",
    by: 'tutorials point',
    url: 'http://www.tutorialspoint.com',
    tags: [ 'mongodb', 'database', 'NoSQL' ],
    likes: 20,
    comments: [
      {
        user: 'user1',
        message: 'My first comment',
        dateCreated: ISODate('2013-12-09T21:05:00.000Z'),
        like: 0
      }
    ]
  }
]

Following example will set the new title 'New MongoDB Tutorial' of the documents whose title is 'MongoDB Overview'.

sampleDB>db.mycol.update({'title':'MongoDB Overview'},{$set:{'title':'New MongoDB Tutorial'}})
{
  acknowledged: true,
  insertedId: null,
  matchedCount: 1,
  modifiedCount: 1,
  upsertedCount: 0
}
sampleDB>db.mycol.find()
[
  {
    _id: ObjectId('6964b8da25c4ea6b911e2629'),
    title: 'New MongoDB Tutorial',
    description: 'MongoDB is no SQL database',
    by: 'tutorials point',
    url: 'http://www.tutorialspoint.com',
    tags: [ 'mongodb', 'database', 'NoSQL' ],
    likes: 100
  },
  {
    _id: ObjectId('6964b8da25c4ea6b911e262a'),
    title: 'NoSQL Database',
    description: "NoSQL database doesn't have tables",
    by: 'tutorials point',
    url: 'http://www.tutorialspoint.com',
    tags: [ 'mongodb', 'database', 'NoSQL' ],
    likes: 20,
    comments: [
      {
        user: 'user1',
        message: 'My first comment',
        dateCreated: ISODate('2013-12-09T21:05:00.000Z'),
        like: 0
      }
    ]
  }
]
sampleDB>

By default, MongoDB will update only a single document. To update multiple documents, you need to set a parameter 'multi' to true.

>db.mycol.update({'title':'MongoDB Overview'},{$set:{'title':'New MongoDB Tutorial'}},{multi:true})

MongoDB findOneAndUpdate() method

The findOneAndUpdate() method updates the values in the existing document.

Syntax

The basic syntax of findOneAndUpdate() method is as follows −

>db.COLLECTION_NAME.findOneAndUpdate(SELECTIOIN_CRITERIA, UPDATED_DATA)

Example

Assume we have created a collection named empDetails and inserted three documents in it as shown below −

sampleDB> db.empDetails.insertMany(
...     [
...             {
...                     First_Name: "Radhika",
...                     Last_Name: "Sharma",
...                     Age: "26",
...                     e_mail: "radhika_sharma.123@gmail.com",
...                     phone: "9000012345"
...             },
...             {
...                     First_Name: "Rachel",
...                     Last_Name: "Christopher",
...                     Age: "27",
...                     e_mail: "Rachel_Christopher.123@gmail.com",
...                     phone: "9000054321"
...             },
...             {
...                     First_Name: "Fathima",
...                     Last_Name: "Sheik",
...                     Age: "24",
...                     e_mail: "Fathima_Sheik.123@gmail.com",
...                     phone: "9000054321"
...             }
...     ]
... )
{
  acknowledged: true,
  insertedIds: {
    '0': ObjectId('696512c025c4ea6b911e262e'),
    '1': ObjectId('696512c025c4ea6b911e262f'),
    '2': ObjectId('696512c025c4ea6b911e2630')
  }
}
sampleDB>

Following example updates the age and email values of the document with name 'Radhika'.

sampleDB> db.empDetails.findOneAndUpdate(
...     {First_Name: 'Radhika'},
...     { $set: { Age: '30',e_mail: 'radhika_newemail@gmail.com'}}
... )
{
  _id: ObjectId('6964ba7325c4ea6b911e262b'),
  First_Name: 'Radhika',
  Last_Name: 'Sharma',
  Age: '26',
  e_mail: 'radhika_sharma.123@gmail.com',
  phone: '9000012345'
}
sampleDB>

MongoDB updateOne() method

This methods updates a single document which matches the given filter.

Syntax

The basic syntax of updateOne() method is as follows −

>db.COLLECTION_NAME.updateOne(<filter>, <update>)

Example

sampleDB> db.empDetails.updateOne(
...     {First_Name: 'Radhika'},
...     { $set: { Age: '30',e_mail: 'radhika_newemail@gmail.com'}}
... )
{
  acknowledged: true,
  insertedId: null,
  matchedCount: 1,
  modifiedCount: 0,
  upsertedCount: 0
}
sampleDB>

MongoDB updateMany() method

The updateMany() method updates all the documents that matches the given filter.

Syntax

The basic syntax of updateMany() method is as follows −

>db.COLLECTION_NAME.update(<filter>, <update>)

Example

sampleDB> db.empDetails.updateMany(
...     {Age:{ $gt: "25" }},
...     { $set: { Age: '00'}}
... )
{
  acknowledged: true,
  insertedId: null,
  matchedCount: 4,
  modifiedCount: 4,
  upsertedCount: 0
}
sampleDB>

You can see the updated values if you retrieve the contents of the document using the find method as shown below −

sampleDB> db.empDetails.find()
[
  {
    _id: ObjectId('6964ba7325c4ea6b911e262b'),
    First_Name: 'Radhika',
    Last_Name: 'Sharma',
    Age: '00',
    e_mail: 'radhika_newemail@gmail.com',
    phone: '9000012345'
  },
  {
    _id: ObjectId('6964ba7325c4ea6b911e262c'),
    First_Name: 'Rachel',
    Last_Name: 'Christopher',
    Age: '00',
    e_mail: 'Rachel_Christopher.123@gmail.com',
    phone: '9000054321'
  },
  {
    _id: ObjectId('6964ba7325c4ea6b911e262d'),
    First_Name: 'Fathima',
    Last_Name: 'Sheik',
    Age: '24',
    e_mail: 'Fathima_Sheik.123@gmail.com',
    phone: '9000054321'
  },
  {
    _id: ObjectId('696512c025c4ea6b911e262e'),
    First_Name: 'Radhika',
    Last_Name: 'Sharma',
    Age: '00',
    e_mail: 'radhika_sharma.123@gmail.com',
    phone: '9000012345'
  },
  {
    _id: ObjectId('696512c025c4ea6b911e262f'),
    First_Name: 'Rachel',
    Last_Name: 'Christopher',
    Age: '00',
    e_mail: 'Rachel_Christopher.123@gmail.com',
    phone: '9000054321'
  },
  {
    _id: ObjectId('696512c025c4ea6b911e2630'),
    First_Name: 'Fathima',
    Last_Name: 'Sheik',
    Age: '24',
    e_mail: 'Fathima_Sheik.123@gmail.com',
    phone: '9000054321'
  }
]
sampleDB>
Advertisements