Mongodb if else statement

Mongodb if else statement

According to MongoDb docs, $cond "evaluates a Boolean expression to return one of the two specified return expressions".
Syntax:

{$cond: { if: <expression>,
                     then: <true>,
                    else: <false>
       }
}

OR

{$cond: [<expression>, <true>, <false>] }


Example for MongoDb if else statement

db.customers.aggregate(
   [{
         $project:
           {
             C_Id: 1,
             C_Type:
               {
                 $cond: {
                                    if: { $gte: [ "$C_Id", "IT "] },
                                                then: "N_Customer",
                                                else: "VIP_Customer"
                                       }
               }
           }
    }]
)

For more detail, go to

ANIL SINGH

Hey! I'm Anil Singh. I author this blog. I'm Active Blogger, Programmer. I love learning new technologies, programming, blogging and participating the forum discussions more...
My Blogs - http://www.code-sample.com and http://www.code-sample.xyz
My Books - Google Amazon and Flipkart Book Store!

You Might Also Like
Post a Comment
www.code-sample.com/. Powered by Blogger.
ASK Questions
SQL Server NodeJs TypeScript JavaScript Angular SQL Server My Book