MongoDB - Update Documents

MongoDB

mongodb logo - tutorial - dyclassroom

In this MongoDB tutorial we will learn to update documents.

Login to your MongoDB server and insert the following documents.

For this tutorial I will insert the documents in the subscription collection.

> db.subscription.insertMany([
  {
    "firstname": "John",
    "lastname": "Doe",
    "uid": 1,
    "accountstatus": "ACTIVE",
    "plan": "TRIAL15DAYS",
    "modifiedAt": null,
    "createdAt": new Date()
  },
  {
    "firstname": "Jane",
    "lastname": "Doe",
    "uid": 2,
    "accountstatus": "ACTIVE",
    "plan": "PLAN30DAYS",
    "modifiedAt": null,
    "createdAt": new Date()
  },
  {
    "firstname": "Jim",
    "lastname": "Doe",
    "uid": 3,
    "accountstatus": "SUSPENDED",
    "plan": "PLAN60DAYS",
    "modifiedAt": null,
    "createdAt": new Date()
  },
  {
    "firstname": "Alice",
    "lastname": "Doe",
    "uid": 4,
    "accountstatus": "ACTIVE",
    "plan": "PLAN1YEAR",
    "modifiedAt": null,
    "createdAt": new Date()
  }
])

Note! We are using new Date() to set the createdAt to current date time.

Update one document

To update a single document we use updateOne method.

Syntax

db.collectionName.updateOne(filter, update)

Where, filter is the condition to get the required document and update is the part of the document we want to update.

In the following example we are updating the account status of user having uid equal to 3 from SUSPENDED to ACTIVE.

> db.subscription.updateOne(
  { "uid": 3 },
  {
    $set: { "accountstatus": "ACTIVE" },
    $currentDate: { "modifiedAt": true }
  }
)

We use $set operator to update the value of accountstatus.

We use $currentDate operator to set the modifiedAt to current date.

If we now fetch the above document we will get the following.

> db.subscription.find({ "uid": 3 }).pretty()

{
  "_id" : ObjectId("5d7715d5b385796f53d4c8ff"),
  "firstname" : "Jim",
  "lastname" : "Doe",
  "uid" : 3,
  "accountstatus" : "ACTIVE",
  "plan" : "PLAN60DAYS",
  "modifiedAt" : ISODate("2019-09-10T03:18:42.406Z"),
  "createdAt" : ISODate("2019-09-10T03:17:41.130Z")
}

Insert new document if not exisits using updateOne method

We can use the updateOne method to update an existing document and also insert a new document if it does not exists.

Syntax

db.collectionName.updateOne(filter, update, option)

In the option set the upsert to true. This will help to insert new document if it doesn't exists.

In the following example we are inserting a new document in the subscription collection using the updateOne method.

> db.subscription.updateOne(
  { "uid": 5 },
  {
    $set: {
      "firstname": "Bob",
      "lastname": "Doe",
      "uid": 5,
      "accountstatus": "ACTIVE",
      "plan": "PLAN1YEAR",
      "modifiedAt": null,
      "createdAt": new Date()
    }
  },
  {
    upsert: true
  }
)

If we now fetch the newly inserted document we will get the following result.

> db.subscription.find({ "uid": 5 }).pretty()


{
  "_id" : ObjectId("5d771bc30a7168b744863bc6"),
  "uid" : 5,
  "accountstatus" : "ACTIVE",
  "createdAt" : ISODate("2019-09-10T03:42:59.876Z"),
  "firstname" : "Bob",
  "lastname" : "Doe",
  "modifiedAt" : null,
  "plan" : "PLAN1YEAR"
}

Update multiple documents

To update multiple documents we use the updateMany method.

In the following example we are updating the plan to PLAN60DAYS for uid 2, 3 and 4.

> db.subscription.updateMany(
  { "uid": { $in: [2, 3, 4] } },
  {
    $set: { "plan": "PLAN60DAYS" },
    $currentDate: { "modifiedAt": true }
  }
)

Learn to filter document using $in and other operators in the MongoDB - Query Document tutorial.

Replace one document

We use the replaceOne method to replace the entire content of a document except the _id.

In the following example we are replacing the content of document having uid equal to 5.

> db.subscription.replaceOne(
  { "uid": 5 },
  {
    "firstname": "Bob",
    "lastname": "Doe",
    "uid": 5,
    "accountstatus": "ACTIVE",
    "plan": "PLAN90DAYS",
    "modifiedAt": new Date(),
    "createdAt": new Date()
  }
)

We can also use replaceOne method to insert new document if it does not exists by setting the upsert option to true.

> db.subscription.replaceOne(
  { "uid": 6 },
  {
    "firstname": "Billy",
    "lastname": "Doe",
    "uid": 6,
    "accountstatus": "ACTIVE",
    "plan": "PLAN90DAYS",
    "modifiedAt": new Date(),
    "createdAt": new Date()
  },
  {
    upsert: true
  }
)

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT