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.
We can update the array by simply using update() method. But here, we need to remember all the elements of the array as well as the new element to be inserted into the array. Thus, this method is somewhat tedious.
We could insert an element into the array by making use of $set operator. Here we need to know the position where the new element needs to be inserted. In mongoDB, array index starts from zero.
using $push operator we can insert an element to the right hand side of the array. Here we simply specify the key and the value/element to be inserted.
$addToSet operator adds specified element to the array, if its not already present in the array. If the element is already present in the array, then it doesn’t add it once again.
This way, we could eliminate the field Product. But this is a tedious process – we need to remember all the fields inorder to achieve this. It gets difficult when we have many fields in our document. To solve this problem, we have $unset operator, where we only need to know the field name which we want to remove.