Friday 20 February 2015

Conditional operators


Conditional operators are used to perform operations like >, <, >=, <= etc.

Operator
Description
$gt
Matches values that are greater than the value specified in the query.
$gte
Matches values that are greater than or equal to the value specified in the query
$in
Matches any of the values that exist in an array specified in the query.
$lt
Matches values that are less than the value specified in the query.
$lte
Matches values that are less than or equal to the value specified in the query.
$ne
Matches all values that are not equal to the value specified in the query.
$nin
Matches values that do not exist in an array specified to the query.

For all examples use below data.

db.employee.insert(
 [
    {
        "_id": 1,
        "firstName": "Joel",
        "lastName": "chelli",
        "salary": 25000,
        "hobbies": [
            "watching movies",
            "playing games"
        ]
    },
    {
        "_id": 2,
        "firstName": "Ananad",
        "lastName": "Bandaru",
        "salary": 200000,
        "hobbies": [
            "car drivinig",
            "watching movies"
        ]
    },
    {
        "_id": 3,
        "firstName": "Gopi",
        "lastName": "Battu",
        "salary": 65000,
        "hobbies": [
            "climbing hills",
            "dancing",
            "singing"
        ]
    },
    {
        "_id": 4,
        "firstName": "Ritwik",
        "lastName": "Mohenthy",
        "salary": 50000,
        "hobbies": [
            "watching movies",
            "watching movies",
            "watching movies"
        ]
    },
    {
        "_id": 5,
        "firstName": "RamaKrishna",
        "lastName": "Gurram",
        "salary": 150000,
        "hobbies": [
            "hackning sites",
            "Reading books",
            "listening music"
        ]
    },
    {
        "_id": 6,
        "firstName": "BalaKrishna",
        "lastName": "Gurram",
        "salary": 80000,
        "hobbies": [
            "roaming around",
            "eating food"
        ]
    }
]
 )

> db.employee.find()
{ "_id" : 1, "firstName" : "Joel", "lastName" : "chelli", "salary" : 25000, "hobbies" : [ "watching movies", "playing games" ] }
{ "_id" : 2, "firstName" : "Ananad", "lastName" : "Bandaru", "salary" : "200000", "hobbies" : [ "car drivinig", "watching movies" ] }
{ "_id" : 3, "firstName" : "Gopi", "lastName" : "Battu", "salary" : "65000", "hobbies" : [ "climbing hills", "dancing", "singing" ] }
{ "_id" : 4, "firstName" : "Ritwik", "lastName" : "Mohenthy", "salary" : "50000", "hobbies" : [ "watching movies", "watching movies", "watching movies" ] }
{ "_id" : 5, "firstName" : "RamaKrishna", "lastName" : "Gurram", "salary" : "150000", "hobbies" : [ "hackning sites", "Reading books", "listening music" ] }
{ "_id" : 6, "firstName" : "BalaKrishna", "lastName" : "Gurram", "salary" : "80000", "hobbies" : [ "roaming around", "eating food" ] }


1.Get all the employee documents where salary is > 50000.
> db.employee.find({salary : {$gt : 50000}})
{ "_id" : 2, "firstName" : "Ananad", "lastName" : "Bandaru", "salary" : 200000, "hobbies" : [ "car drivinig", "watching movies" ] }
{ "_id" : 3, "firstName" : "Gopi", "lastName" : "Battu", "salary" : 65000, "hobbies" : [ "climbing hills", "dancing", "singing" ] }
{ "_id" : 5, "firstName" : "RamaKrishna", "lastName" : "Gurram", "salary" : 150000, "hobbies" : [ "hackning sites", "Reading books", "listening music" ] }
{ "_id" : 6, "firstName" : "BalaKrishna", "lastName" : "Gurram", "salary" : 80000, "hobbies" : [ "roaming around", "eating food" ] }


2. Get all the employee documents where salary is > 50000 and lastName is Gurram.
> db.employee.find({"salary" : {$gt : 50000}, "lastName" : "Gurram"})
{ "_id" : 5, "firstName" : "RamaKrishna", "lastName" : "Gurram", "salary" : 150000, "hobbies" : [ "hackning sites", "Reading books", "listening music" ] }
{ "_id" : 6, "firstName" : "BalaKrishna", "lastName" : "Gurram", "salary" : 80000, "hobbies" : [ "roaming around", "eating food" ] }


3. Get all the employee documents where salary is >= 50000 and less than 90000.
> db.employee.find({"salary" : {$gte : 50000, $lt : 90000}})
{ "_id" : 3, "firstName" : "Gopi", "lastName" : "Battu", "salary" : 65000, "hobbies" : [ "climbing hills", "dancing", "singing" ] }
{ "_id" : 4, "firstName" : "Ritwik", "lastName" : "Mohenthy", "salary" : 50000, "hobbies" : [ "watching movies", "watching movies", "watching movies" ] }
{ "_id" : 6, "firstName" : "BalaKrishna", "lastName" : "Gurram", "salary" : 80000, "hobbies" : [ "roaming around", "eating food" ] }

4. Get all the employe documents, where hobbies contain “watching movies”.
> db.employee.find({"hobbies" : {$in :["watching movies"]}})
{ "_id" : 1, "firstName" : "Joel", "lastName" : "chelli", "salary" : 25000, "hobbies" : [ "watching movies", "playing games" ] }
{ "_id" : 2, "firstName" : "Ananad", "lastName" : "Bandaru", "salary" : 200000, "hobbies" : [ "car drivinig", "watching movies" ] }
{ "_id" : 4, "firstName" : "Ritwik", "lastName" : "Mohenthy", "salary" : 50000, "hobbies" : [ "watching movies", "watching movies", "watching movies" ] }


5. Get all the employe documents, where hobbies don’t contain “watching movies”.

> db.employee.find({"hobbies" : {$nin :["watching movies"]}})
{ "_id" : 3, "firstName" : "Gopi", "lastName" : "Battu", "salary" : 65000, "hobbies" : [ "climbing hills", "dancing", "singing" ] }
{ "_id" : 5, "firstName" : "RamaKrishna", "lastName" : "Gurram", "salary" : 150000, "hobbies" : [ "hackning sites", "Reading books", "listening music" ] }
{ "_id" : 6, "firstName" : "BalaKrishna", "lastName" : "Gurram", "salary" : 80000, "hobbies" : [ "roaming around", "eating food" ] }


6. Get all the employe documents, where hobbies contain “watching movies” or “singing”.

> db.employee.find({"hobbies" : {$in :["watching movies","singing"]}})
{ "_id" : 1, "firstName" : "Joel", "lastName" : "chelli", "salary" : 25000, "hobbies" : [ "watching movies", "playing games" ] }
{ "_id" : 2, "firstName" : "Ananad", "lastName" : "Bandaru", "salary" : 200000, "hobbies" : [ "car drivinig", "watching movies" ] }
{ "_id" : 3, "firstName" : "Gopi", "lastName" : "Battu", "salary" : 65000, "hobbies" : [ "climbing hills", "dancing", "singing" ] }
{ "_id" : 4, "firstName" : "Ritwik", "lastName" : "Mohenthy", "salary" : 50000, "hobbies" : [ "watching movies", "watching movies", "watching movies" ] }




Prevoius                                                 Next                                                 Home

No comments:

Post a Comment