MongoDB - SPLessons

MongoDB Operators

Chapter 17

SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

MongoDB Operators

MongoDB Operators

shape Description

The MongoDB Operators is defined as a character that is used to store characters values basically as a part of MongoDB database. MongoDB Operators are utilized to implement conditions in Mongodb database and provides services and provide different number of conjunctions in an announcement. MongoDB Operators can be classified into the following types:

Set Operators

shape Description

In set operator a client wants to add a field to the document while another client is trying to increment. So the client will create a new record by himself such as db.a.find with only one field and add them accordingly. For that the client have to use update command instead of saving a new record. And whatever the changes occurred will be saved within the update command.

shape Examples

By viewing the below example, the concept of set operator can be understand easily.

MongoDB shell version:2.6.1
connecting to: test
>use test
switched to db test
>show collections
names
system.indexes
>db.names.find();
>db.a.save({_id:1,x:10});
>db.a.find()
{"_id" :1,"x":10}
>db.a.update({_id:1},{$set:{y:3}})
>db.a.update({_id:1),($inc:{x:1}})
>db.a.find()
{"_id":1,"x":11,"y":3}

Unset Operators

shape Description

The unset command takes the field name and an arbitrary value. The single quotes there have no meaning other than holding place. It might have well said ‘0’ and doesn’t matter and the end result is ‘1’ which is removed from the field.

shape Examples

By viewing the below example, the concept of unset operator can be understand easily.

MongoDB shell version:2.6.1
connecting to: test
>use test
switched to db test
>show collections
names
system.indexes
>db.a.find()
{"_id" :1,"x":11,"y":3}
>db.a.update({_id:1},{$unset:{y:''}})
>db.a.update({_id:1},{$unset:{y:0}})
>db.a.find()
{"_id":1,"x":11}

Rename Operators

shape Description

By using rename operator, one can change the record name from older name to new name.

shape Examples

By viewing the below example, the concept of Rename operator can be understand easily.

MongoDB shell version:2.6.1
connecting to: test
>use test
switched to db test
>show collections
names
system.indexes
>db.a.find()
db.a.save({_id:1,Name:'James'})
>db.a.find()
{"_id":1,"Name":"James"}
>db.a.update({_id:1},{$rename:{'name':'name'}})
>db.find()
{"Name":"James","_id":1}

Push Operators

shape Description

In push operator, consider an array operation consisting of a document containing an ID with some values inside the document. By using update command one can push the item into an array. And the item is pushed inside and again push multiple items. To prevent that use addToSet operator, which means add this element only if it doesn’t already exist in the array.

shape Examples

By viewing the below example, the concept of push operator can be understand easily.

MongoDB shell version:2.6.1
connecting to: test
>use test
switched to db test
>show collections
names
system.indexes
>db.a.find()
>db.a.save({_id:1})
>db.a.find()
{"_id":1}
>db.a.update({_id:1},{$push:{things:'one'}})
>db.a.find()
{"_id":1,"things":["one"]}
>db.a.update({_id:1},{$push:{things:'two'}})
>db.a.update({_id:1},{$push:{things:'three'}})
>db.a.find()
{"_id":1,"things":["one","two","three"}})
>db.a.update({_id:1},{$push:{things:'three'}})
>db.a.find()
{"_id":1,"things":["one","two","three","three"}})
>db.a.update({_id:1},{$push:{things:'four'}})
>db.a.find()
{"_id":1,"things":["one","two","three","three","four"]
}
>db.a.update({_id:1},{$addTOSet:{things:'four'}})
>db.a.find()
{"_id":1,"things":["one","two","three","three","four"]
}

Pull Operators

shape Description

The pull operator removes all the instances of an element from an array.

shape Examples

By viewing the below example, the concept of pull operator can be understand easily.

MongoDB shell version:2.6.1
connecting to: test
>use test
switched to db test
>show collections
names
system.indexes
>db.a.find()
>db.a.save({_id:1})
>db.a.find()
{"_id":1}
>db.a.update({_id:1},{$push:{things:'one'}})
>db.a.find()
{"_id":1,"things":["one"]}
>db.a.update({_id:1},{$push:{things:'two'}})
>db.a.update({_id:1},{$push:{things:'three'}})
>db.a.find()
{"_id":1,"things":["one","two","three"}})
>db.a.update({_id:1},{$push:{things:'three'}})
>db.a.find()
{"_id":1,"things":["one","two","three","three"}})
>db.a.update({_id:1},{$push:{things:'four'}})
>db.a.find()
{"_id":1,"things":["one","two","three","three","four"]
}
>db.a.update({_id:1},{$addTOSet:{things:'four'}})
>db.a.find()
{"_id":1,"things":["one","two","three","three","four"]
}
>db.a.update({_id:1},{$pull:{things:'three'}})
>db.a.find()
{"_id":1,"things":["one","two","four"]}
>db.a.update({_id:1},{$addToSet:{things:'three'}})
>db.a.update({_id:1},{$addToSet:{things:'three'}})
>db.a.update({_id:1},{$addToSet:{things:'three'}})
>db.a.find()
{"_id":1,"things":["one","two","four","three"]

Pop Operators

shape Description

Pop operator will remove the last element in an array.

shape Examples

By viewing the below example, the concept of pop operator can be understand easily.

MongoDB shell version:2.6.1
connecting to: test
>use test
switched to db test
>show collections
names
system.indexes
>db.a.find()
>db.a.find()
{"_id":1,"things":["one","two","three"]}
>db.a.update({_id:1},{$pop:{things:1}})
>db.a.find()
{"_id":1,"things":["one","two"]}
>db.a.update({_id:1},{$pop:{things:-1}})
>db.a.find()
{"_id":1,"things":["two"]}
>db.a.update({_id:1},{$pop:{things:-1}})
>db.a.update({_id:1},{$pop:{things:-1}})
>db.a.update({_id:1},{$pop:{things:-1}})
>db.a.find()
{"_id":1,"things":[]}

Summary

shape Key Points

  • MongoDB Operators – Defined as a character that is used to store character values.
  • Set Operator – Used to add multiple fields in the document.
  • Unset Operator – Takes all the fields names and an arbitrary values.
  • Rename Operator – Rename from older name to new name.
  • Push Operator – It will push an item inside an array.
  • Pull Operator – Removes all the instances of an element from an array.
  • Pop operator – Removes the last element in an array.