What’s the difference between equal to v/s strictly equal to and not equal to v/s strictly not equal to in any programming language?
This seems to be confusing at first, but if we execute a small example things get very clear about the working of these operators.
== is called equal to operator. != is called not-equal to operator. === is called strictly equal to operator. !== is called strictly not-equal to operator.
In this video tutorial we shall see the differences between == v/s === and != v/s !===
Lets assign integer 1 to variable a and string ‘1’ to variable b.
Equal-to v/s Strictly Equal-to
$a == $b
true
$a === $b
false
$a == $b
true
$a === $b
false
== operator doesn’t consider the type of variable strictly, thus returns true even though we are comparing a string with an integer. But === operator considers the type of the variable strictly and treats integer and string as two different things and hence returns false.
Not Equal-to v/s Strictly Not Equal-to
$a != $b
false
$a !== $b
true
$a != $b
false
$a !== $b
true
Similarly, as != operator doesn’t take variable type into strict consideration it thinks $a and $b as equals, hence returns false, but !== returns true as it reads integer and string variables as different.
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
JavaScript file load.js – in path: C:/test/load.js
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']
});
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.
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
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.
$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.