if/else condition in MongoDB


$cond operator has the keyword "if", "then" and "else", using this operator we can perform if/else in MongoDB query.

See the following example:


Use Test



db.Users.insert(
{
    "fName" : "Dilip",
    "lName" : "Singh",
    "address" : "Noida",
    "totalOrder" : 150,
    "createdDate" : "2014/07/07"
})
db.Users.insert(
{
    "fName" : "Vipul",
    "lName" : "Bhatt",
    "address" : "Delhi",
    "totalOrder" : 50,
    "createdDate" : "2013/07/07"
})
db.Users.insert(
{
    "fName" : "Brijesh",
    "lName" : "Kumar",
    "address" : "Gorakhpur",
    "totalOrder" : 70,
    "createdDate" : "2012/07/07"
})
db.Users.insert(
{
    "fName" : "Raj",
    "lName" : "Kumar",
    "address" : "Bokaro",
    "totalOrder" : 40,
    "createdDate" : "2010/07/07"
})

db.Users.find()


Output


/* 0 */
{
    "_id" : ObjectId("55dac0b31c949abf34d5daed"),
    "fName" : "Dilip",
    "lName" : "Singh",
    "address" : "Noida",
    "totalOrder" : 150,
    "createdDate" : "2014/07/07"
}

/* 1 */
{
    "_id" : ObjectId("55dac0c21c949abf34d5daf1"),
    "fName" : "Vipul",
    "lName" : "Bhatt",
    "address" : "Delhi",
    "totalOrder" : 50,
    "createdDate" : "2013/07/07"
}

/* 2 */
{
    "_id" : ObjectId("55dac0d11c949abf34d5daf2"),
    "fName" : "Brijesh",
    "lName" : "Kumar",
    "address" : "Gorakhpur",
    "totalOrder" : 70,
    "createdDate" : "2012/07/07"
}

/* 3 */
{
    "_id" : ObjectId("55dac0e31c949abf34d5daf3"),
    "fName" : "Raj",
    "lName" : "Kumar",
    "address" : "Bokaro",
    "totalOrder" : 40,
    "createdDate" : "2010/07/07"
}


Now we apply query if the user has totalOrder more than 100 so his User Type will be  Gold else Silver. See the following query.


db.Users.aggregate(
[{
    $project :
    {
        fName:1,
        totalOrder:1,
        UserType:{
            $cond:{
                    if:{$gte:["$totalOrder",100]},
                    then:"gold",
                    else:"Silver"
                 }
                }
    }
}])


Output


/* 0 */
{
    "result" : [
        {
            "_id" : ObjectId("55dac0b31c949abf34d5daed"),
            "fName" : "Dilip",
            "totalOrder" : 150,
            "UserType" : "gold"
        },
        {
            "_id" : ObjectId("55dac0c21c949abf34d5daf1"),
            "fName" : "Vipul",
            "totalOrder" : 50,
            "UserType" : "Silver"
        },
        {
            "_id" : ObjectId("55dac0d11c949abf34d5daf2"),
            "fName" : "Brijesh",
            "totalOrder" : 70,
            "UserType" : "Silver"
        },
        {
            "_id" : ObjectId("55dac0e31c949abf34d5daf3"),
            "fName" : "Raj",
            "totalOrder" : 40,
            "UserType" : "Silver"
        }
    ],
    "ok" : 1
}



$skip (Aggregation) in MongoDB


This operator uses to skip first to n documents and remaining will be passed in the next pipeline. n= skip number.

Syntax:


{$skip:<positive integer>}


See the following example


use Test



db.Inventory.insert(
{
"Title":"MongoDB Tutorial by codefari.com",
"ISBN":"1990987666765",
"AccessCountry":["India","China"],
})
db.Inventory.insert(
{
"Title":"SQL Server tutorial by codefari.com",
"ISBN":"9909876765476",
"AccessCountry":["USA"],
})
db.Inventory.insert(
{
"Title":"C# tutorial by codefari.com",
"ISBN":"8898877676544",
"AccessCountry":["UK"],
})
db.Inventory.find()


output


/* 0 */
{
    "_id" : ObjectId("5655b294583c040b3341b96e"),
    "Title" : "MongoDB Tutorial by codefari.com",
    "ISBN" : "1990987666765",
    "AccessCountry" : [
        "India",
        "China"
    ]
}

/* 1 */
{
    "_id" : ObjectId("5655b294583c040b3341b96f"),
    "Title" : "SQL Server tutorial by codefari.com",
    "ISBN" : "9909876765476",
    "AccessCountry" : [
        "USA"
    ]
}

/* 2 */
{
    "_id" : ObjectId("5655b294583c040b3341b970"),
    "Title" : "C# tutorial by codefari.com",
    "ISBN" : "8898877676544",
    "AccessCountry" : [
        "UK"
    ]
}


Now if we want to skip 2 records then we will use $skip



db.Inventory.aggregate(
    {$skip:2}
)


Output


/* 0 */
{
    "result" : [
        {
            "_id" : ObjectId("5655b294583c040b3341b970"),
            "Title" : "C# tutorial by codefari.com",
            "ISBN" : "8898877676544",
            "AccessCountry" : [
                "UK"
            ]
        }
    ],
    "ok" : 1
}


Related Posts

What is the Use of isNaN Function in JavaScript? A Comprehensive Explanation for Effective Input Validation

In the world of JavaScript, input validation is a critical aspect of ensuring that user-provided data is processed correctly. One indispensa...