Comparison Operators: MongoDB

In this video tutorial we shall illustrate the use of comparison operators in MongoDB.

Comparison Operators
$all
$in
$nin – not in
$ne – not equal to
$gt – greater than
$gte – greater than or equal to
$lt – less than
$lte – less than or equal to

red-apple-green-apple-comparison

JavaScript file
load.js – in path: C:/test/load.js

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
db.person.insert({
name   : 'Satish',
age    : 25,
skills : ['nodejs', 'mongoDB', 'HTML5']
});
 
db.person.insert({
name   : 'Kiran',
age    : 27,
skills : ['PHP', 'mySQL', 'HTML5']
});
 
db.person.insert({
name   : 'Sunitha',
age    : 24,
skills : ['html', 'ASP']
});
 
db.person.insert({
name   : 'Jyothi',
age    : 23,
skills : ['html', 'ASP']
});
 
db.person.insert({
name   : 'Varsha',
age    : 30,
skills : ['.NET', 'Java']
});
 
db.person.insert({
name   : 'Amogh',
age    : 29,
skills : ['C#', 'ASP']
});

This JavaScript file contains some simple data, to be inserted into MongoDB server.
It contains, persons name, age and skills(in array form)
person is the collection name, we’re creating.

From our previous day video tutorial we already know, how to, Load Data From External JavaScript File: MongoDB.

load script to new database

1
2
3
4
5
6
7
8
9
C:\mongodb>cd bin
 
C:\mongodb\bin>mongo 127.0.0.1/satish C:/temp/load.js
MongoDB shell version: 2.4.3
connecting to: 127.0.0.1/satish
 
C:\mongodb\bin>mongo
MongoDB shell version: 2.4.3
connecting to: test

Once these data/documents are loaded into new database, we start operating on this data using comparison operator.

Documents In person Collection

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
> db.person.find().forEach(printjson)
{
        "_id" : ObjectId("518c76ba05ea2a1d2b3b33a0"),
        "name" : "Satish",
        "age" : 25,
        "skills" : [
                "nodejs",
                "mongoDB",
                "HTML5"
        ]
}
{
        "_id" : ObjectId("518c76ba05ea2a1d2b3b33a1"),
        "name" : "Kiran",
        "age" : 27,
        "skills" : [
                "PHP",
                "mySQL",
                "HTML5"
        ]
}
{
        "_id" : ObjectId("518c76ba05ea2a1d2b3b33a2"),
        "name" : "Sunitha",
        "age" : 24,
        "skills" : [
                "html",
                "ASP"
        ]
}
{
        "_id" : ObjectId("518c76ba05ea2a1d2b3b33a3"),
        "name" : "Jyothi",
        "age" : 23,
        "skills" : [
                "html",
                "ASP"
        ]
}
{
        "_id" : ObjectId("518c76ba05ea2a1d2b3b33a4"),
        "name" : "Varsha",
        "age" : 30,
        "skills" : [
                ".NET",
                "Java"
        ]
}
{
        "_id" : ObjectId("518c76ba05ea2a1d2b3b33a5"),
        "name" : "Amogh",
        "age" : 29,
        "skills" : [
                "C#",
                "ASP"
        ]
}

Comparison Operators: MongoDB


[youtube https://www.youtube.com/watch?v=937PSHo7F00]

YouTube Link: https://www.youtube.com/watch?v=937PSHo7F00 [Watch the Video In Full Screen.]



Switch to new database, ‘satish’

1
2
3
4
5
6
7
> show dbs
admin   0.203125GB
company 0.203125GB
local   0.078125GB
satish  0.203125GB
> use satish
switched to db satish

$all operator

1
2
3
4
> db.person.find({skills: { $all: ['html', 'css'] }}, {name: 1, _id: 0});
> db.person.find({skills: { $all: ['html', 'ASP'] }}, {name: 1, _id: 0});
{ "name" : "Sunitha" }
{ "name" : "Jyothi" }

Matches arrays that contain all elements specified in the query.

$in operator

1
2
3
4
5
6
> db.person.find({skills: { $in: ['html', 'css'] }}, {name: 1, _id: 0});
{ "name" : "Sunitha" }
{ "name" : "Jyothi" }
> db.person.find({skills: { $in: ['java', 'css'] }}, {name: 1, _id: 0});
> db.person.find({skills: { $in: ['Java', 'css'] }}, {name: 1, _id: 0});
{ "name" : "Varsha" }

Matches any of the values that exist in an array specified in the query.

$nin operator: not in

1
2
3
4
5
6
7
8
9
> db.person.find({skills: { $nin: ['Java', 'css'] }}, {name: 1, _id: 0});
{ "name" : "Satish" }
{ "name" : "Kiran" }
{ "name" : "Sunitha" }
{ "name" : "Jyothi" }
{ "name" : "Amogh" }
> db.person.find({skills: { $nin: ['Java', 'ASP'] }}, {name: 1, _id: 0});
{ "name" : "Satish" }
{ "name" : "Kiran" }

Matches values that do not exist in an array specified to the query.

$gt operator

1
2
3
4
> db.person.find({age: { $gt: 25 }}, {name: 1, _id: 0});
{ "name" : "Kiran" }
{ "name" : "Varsha" }
{ "name" : "Amogh" }

Matches values that are greater than the value specified in the query.

$gte operator

1
2
3
4
5
> db.person.find({age: { $gte: 25 }}, {name: 1, _id: 0});
{ "name" : "Satish" }
{ "name" : "Kiran" }
{ "name" : "Varsha" }
{ "name" : "Amogh" }

Matches values that are equal to or greater than the value specified in the query.

Combining multiple operators: $gt, $gte and $lt operator

1
2
3
4
5
6
7
8
> db.person.find({age: { $gte: 25, $lt: 29 }}, {name: 1, _id: 0});
{ "name" : "Satish" }
{ "name" : "Kiran" }
> db.person.find({age: { $gt: 25, $lt: 29 }}, {name: 1, _id: 0});
{ "name" : "Kiran" }
> db.person.find({age: { $gt: 25, $lte: 29 }}, {name: 1, _id: 0});
{ "name" : "Kiran" }
{ "name" : "Amogh" }

$lt – less than – Matches vales that are less than the value specified in the query.
$lte – less than or equal to – Matches values that are less than or equal to the value specified in the query.

$ne operator

1
2
3
4
5
6
> db.person.find({age: {$ne: 25}}, {name: 1, _id: 0});
{ "name" : "Kiran" }
{ "name" : "Sunitha" }
{ "name" : "Jyothi" }
{ "name" : "Varsha" }
{ "name" : "Amogh" }

Matches all values that are not equal to the value specified in the query.

Note:
Operators are useful when we do not know the exact value to be extracted or to fetch a range of values.
All mongoDB operators begin with $ sign.

Conditional Logic For Decision Making: jQuery

In this video tutorial we shall see how to make our applications smarter with artificial intelligence using conditional logic for decision making, using jQuery.

Here we take 2 numbers from the user and using jQuery, we find the result of division of those two numbers. If the user tries to divide by zero, then we alert the user with a message, that “Division by zero is not allowed”. Thus making our application smarter and get the feel of artificial intelligence!

HTML code
index.html

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<html>
<head><title>Conditional Logic For Decision Making: jQuery</title></head>
<body>
 
<form>
No1: <input type="text" id="no1" /><br />
No2: <input type="text" id="no2" />
<button>Division</button>
</form>
 
<span></span>
 
<script src="script/jquery-1.8.1.min.js" type="text/javascript"></script>
<script src="script/my_script.js" type="text/javascript"></script>
</body>
</html>

Here we have a form, with 2 input box and a button.
There is a span tag to display the appropriate messages.

jQuery code
my_script.js

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
$(document).ready( function() {
 
 $("button").click( function() {
     var no1 = $("#no1").val();
 var no2 = $("#no2").val();
 
 if( no2 == 0 )
   $("span").html("Division by zero not allowed!");
 else if( no2 == 1 )
   $("span").html("Division of "+no1+" and "+no2+" is "+(no1/no2));
 else
 
 $("form").submit( function() { return false; });
 });
});

Here we fetch and assign the values entered by the user to local variables.
using conditional operator, if else, we see if the second number entered by the user is zero.
If its zero, we show “Division by zero not allowed!” message, else we show the result of division.

We also need to make the form submit return false, so that the form doesn’t try to redirect to some other page once the user clicks on the submit button.

Video Tutorial: Conditional Logic For Decision Making: jQuery


[youtube https://www.youtube.com/watch?v=JMXbyAUMmEk]

YouTube Link: https://www.youtube.com/watch?v=JMXbyAUMmEk [Watch the Video In Full Screen.]



Comparison Operators
1. Equality: a == b;
2. Inequality: a != b;
3. Exactly Equal: a === b;
4. Greater than: a > b;
5. Greater than or equal to: a >= b;
6. Less than: a < b; 7. Less than or equal to: a < = b; Logical Operators: Negation: !a OR: a || b; AND: a && b;

TruthTable

Negation: True if a is false or doesn’t exist in the DOM.
OR: True if a is true or b is true, or if both are true, but not if both are false.
AND: True if a is true and b is true, but not if one or the other is false.

Ternary Operator
Ex: a > b ? if_true_code : else_this_code;

If a is greater than b, then if_true_code will be executed, else else_this_code will be executed.

Also see Ternary Operator in C for understanding the logic.
Biggest of Two Numbers Using Ternary Operator: C++.
Biggest of 3 Numbers Using Ternary Operator: C++.

Note: If there is only 1 statement inside the if and/or elseif and/or else, we need not put the code inside the brackets. If there are multiple statements then we MUST put the code inside the brackets. Also we can have any number of elseif’s in a if else block, but only 1 if and only 1 else part in it. We could have any number of if-elseif-else blocks in a program.

Also note that, we can have if without elseif and else. But we can not have elseif and else without if!