Understanding MongoDB Modifiers

Recently I’ve been working on a project where I needed to use lots of modifiers. So in this post you will learn about $inc, $set, $push, $addToSet and $each modifiers.

$inc

$inc, a modifier which can be used to increment a particular existing key or to create a new key if it doesn’t exist then increment the value of the key. For example, we have a database collection called sites. Each time someone visits to a particular site we will increment the pageView key.

db.sites.update({ "slug": "codemacaw" }, {
  "$inc": { "pageView": 1 }
});

To see the document,

db.sites.find({ "slug": "codemacaw" });
{
  "slug": "codemacaw",
  "url": "https://www.codemacaw.com",
  "pageView": 1
}

If we again execute or a new user visits our site,

db.sites.update({ "slug": "codemacaw" }, {
  "$inc": { "pageView": 1 }
});

Then the document will be,

db.sites.find({ "slug": "codemacaw" });
{
  "slug": "codemacaw",
  "url": "https://www.codemacaw.com",
  "pageView": 2
}

$set

$set, a modifier which can be used to set value of a key, it will be created if the key doesn’t exist then $set will set the value of that key. For example, suppose you have a collection users if a particular user updates a particular key you can assume the key is age,

db.users.update({ "_id": ObjectId("58959598byg59595sdwca") }, {
   "$set": { "age": 35 }
});

Now the document will have an age key,

db.users.findById({ "_id": ObjectId("58959598byg59595sdwca") });
{
  "_id": ObjectId("58959598byg59595sdwca"),
  "name": "John Doe",
  "username": "doe",
  "age": 35
}

If the user decides to remove the age key, you can use inverse of $set called $unset,

db.users.update({ "_id": ObjectId("58959598byg59595sdwca") }, {
   "$unset": { "age": 1 }
});

Now the document will be,

{
  "_id: ObjectId("58959598byg59595sdwca"),
  "name": "John Doe",
  "username": "doe"
}

$push

$push, a modifier which can be used to add elements to the end of an array if it does exist or creates a new array if it doesn’t exist then add elements to the end of that array. For example, we have a collection called posts,

db.posts.find({ slug: "firstPost" });
{
  "title": "Our First Post",
  "slug": "firstPost",
  "content": "lorem ipsum..."
}

Let’s add comment using $push,

db.posts.update({ "slug": "firstPost" }, {
  "$push": { "comments":  
    { "name": "John Doe", "username": "doe", "comment": "Great post" }
  }
});    

Now the document will be,

{
  "title": "Our First Post",
  "slug": "firstPost",
  "content": "lorem ipsum...",
  "comments": [
    {
      "name": "John Doe",
      "username": "doe",
      "comment": "Great post"
    }
  ]
}

We can add another comment,

db.posts.update({ "slug": "firstPost" }, {
  "$push": { "comments":  
    { "name": "Mark", "username": "mark", "comment": "Good post" }
  }
});
{
  "title": "Our First Post",
  "slug": "firstPost",
  "content": "lorem ipsum...",
  "comments": [
    {
      "name": "John Doe",
      "username": "doe",
      "comment": "Great post"
    },
    {
      "name": "Mark",
      "username": "mark",
      "comment": "Good post"
    }
  ]
}

$addToSet

$addToSet, a modifier which can be used to add elements into an array but prevent duplication. For example, you have a collection called users,

db.users.findById({ "_id": ObjectId("58959598byg59595sdwca") });
{
  "_id": ObjectId("58959598byg59595sdwca"),
  "name": "John Doe",
  "username": "doe",
  "age": 35,
  "cell_number": [
    "123456789",
    "987654321"
  ]
}

Let’s add a new cell_number using $addToSet modifier,

db.users.update({ "_id": ObjectId("58959598byg59595sdwca") }, {
  "$addToSet": {
    "cell_number": "123456789"
  }
});

As 123456789 is duplicate so $addToSet won’t add into cell_number key, so the document will be the same,

{
  "_id": ObjectId("58959598byg59595sdwca"),
  "name": "John Doe",
  "username": "doe",
  "age": 35,
  "cell_number": [
    "123456789",
    "987654321"
  ]
}

If you add a new unique cell_number,

db.users.update({ "_id": ObjectId("58959598byg59595sdwca") }, {
  "$addToSet": {
    "cell_number": "112233445"
  }
});
db.users.findById({ "_id": ObjectId("58959598byg59595sdwca") });
{
  "_id": ObjectId("58959598byg59595sdwca"),
  "name": "John Doe",
  "username": "doe",
  "age": 35,
  "cell_number": [
    "123456789",
    "987654321",
    "112233445"
  ]
}

$each

$each, a modifier which can be used to add multiple elements with $push, $addToSet etc. For example, consider the previous example of $addToSet modifier, we can add an array of numbers into cell_number,

db.users.update({ "_id": ObjectId("58959598byg59595sdwca") }, {
  "$addToSet": {
    "cell_number": { 
      "$each": ["123456789", "225533665", "654321111"]
    }
  }
});
db.users.findById({ "_id": ObjectId("58959598byg59595sdwca") });
{
  "_id": ObjectId("58959598byg59595sdwca"),
  "name": "John Doe",
  "username": "doe",
  "age": 35,
  "cell_number": [
    "123456789",
    "987654321",
    "112233445",
    "225533665",
    "654321111"
  ]
}

2 thoughts on “Understanding MongoDB Modifiers”

Leave a Comment

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