How to use IF ELSE in MongoDB

You are here : Home / MongoDB Tutorial



Contents of page >

  • 1) First let's create insert documents in collection in MongoDB
  • 2) Write query which prints firstName and salary of employee >
  • If salary >= 2000 then print "high".
  • Else         print "low".


1) First let's create insert documents in collection in MongoDB

1.1) Let's create new collection and insert document in it before finding >
db.employee.insert({id : 1,  firstName:"ank", salary : 1000 })
db.employee.insert({id : 2,  firstName:"ank", salary : 2000 })
db.employee.insert({id : 3,  firstName:"sag", salary : 3000 })
db.employee.insert({id : 4,  firstName:"sag", salary : 4000 })
db.employee.insert({id : 5,  firstName:"neh", salary : 5000 })
db.employee.insert({id : 6,  firstName:"neh", salary : 5000 })
Above will create collection (or table) (if collection already exists it will insert documents in it).

1.2) Let’s query all documents of collection using find() method>
> db.employee.find()
Output>
{ "_id" : ObjectId("588205d01e509423fa0faa6d"), "id" : 1, "firstName" : "ank", "salary" : 1000 }
{ "_id" : ObjectId("588205d01e509423fa0faa6e"), "id" : 2, "firstName" : "ank", "salary" : 2000 }
{ "_id" : ObjectId("588205d01e509423fa0faa6f"), "id" : 3, "firstName" : "sag", "salary" : 3000 }
{ "_id" : ObjectId("588205d01e509423fa0faa70"), "id" : 4, "firstName" : "sag", "salary" : 4000 }
{ "_id" : ObjectId("588205d01e509423fa0faa71"), "id" : 5, "firstName" : "neh", "salary" : 5000 }
{ "_id" : ObjectId("588205d01e509423fa0faa72"), "id" : 6, "firstName" : "neh", "salary" : 5000 }


2) Write query which prints firstName and salary of employee >
  • If salary >= 2000 then print "high".
  • Else         print "low".

We will use $cond operator.
db.employee.aggregate(
  [
     {
        $project:
          {
            firstName : 1,
            salaryIsHighOrLow :
              {
                $cond: { if: { $gte: [ "$salary", 2000 ] }, then: "high" , else: "low" }
              }
          }
     }
  ]
)
Output>
{ "_id" : ObjectId("588205d01e509423fa0faa6d"), "firstName" : "ank", "salaryIsHighOrLow" : "low" }
{ "_id" : ObjectId("588205d01e509423fa0faa6e"), "firstName" : "ank", "salaryIsHighOrLow" : "high" }
{ "_id" : ObjectId("588205d01e509423fa0faa6f"), "firstName" : "sag", "salaryIsHighOrLow" : "high" }
{ "_id" : ObjectId("588205d01e509423fa0faa70"), "firstName" : "sag", "salaryIsHighOrLow" : "high" }
{ "_id" : ObjectId("588205d01e509423fa0faa71"), "firstName" : "neh", "salaryIsHighOrLow" : "high" }
{ "_id" : ObjectId("588205d01e509423fa0faa72"), "firstName" : "neh", "salaryIsHighOrLow" : "high" }

In above query >
firstName : 1 > Is used to display firstName field in output.


Summary -
So in this MongoDB tutorial we learned How to use IF ELSE in MongoDB.



Having any doubt? or you liked the tutorial! Please comment in below section.
Please express your love by liking JavaMadeSoEasy.com (JMSE) on facebook, following on google+ or Twitter. You may join our fbGroup or linkedInGroup as well.



RELATED LINKS>

What is MongoDB - A quick introduction to database


eEdit
Must read for you :