As you can see, $and works/outputs results only when all the conditions in the array is met. i.e., in above command, the name must have small letter “e” in it and must be lexicographically greater than capital letter “C”.
In set theory, the union (denoted by ∪) of a collection of sets is the set of all distinct elements in the collection. It is one of the fundamental operations through which sets can be combined and related to each other.
Union of two sets The union of two sets A and B is the collection of points which are in A or in B or in both A and B. In symbols,
For example, if A = {1, 3, 5, 7} and B = {1, 2, 4, 6} then A ∪ B = {1, 2, 3, 4, 5, 6, 7}.
$or is a prefix operator. It takes array as it’s value. The array can contain any number of objects for the union. Mongo Shell fetches all the documents which matches any of the documents which the individual objects inside the array points to.
Observe the documents – it has some names which are in alphabetical order. Last document has an extra field called age. And another odd entry is a document with name as 25.
If $exists is true, it retrieves documents which has the specified field. If $exists is false, then it retrieves all the documents which do not have the specified field.
$regex: “e” retrieves all the documents in which small letter “e” is present. $regex: “^E” retrieves all the documents which has it’s name beginning letter as “E”. $regex: “h$” retrieves all the documents which has it’s name ending letter as “h”. $regex: “^[A-Z]” retrieves all the documents which has it’s name starting with characters “A” to “Z”. $regex: “^[A-E]” retrieves all the documents which has it’s name starting with characters “A” to “E”.
Note: There is more to regular expression, this video is just an introduction. We’ll cover more about regular expressions in a separate video of it’s own.
Today we shall see how we can compare strings using comparison operators like, $ne – not equal to $gt – greater than $gte – greater than or equal to $lt – less than $lte – less than or equal to
(In our video we have clearly illustrated the use of $lt and $gt. Your task is to understand it and try other comparison operators, and you can share your results in the comment section below.)
But in both cases the entry with name as 25 doesn’t appear. This concludes that, in MongoDB while we compare string/character using comparison operator, the comparison occurs only between strings and character and not with other datatypes.
Note: Lexicographical order: In mathematics, the lexicographic or lexicographical order is a generalization of the way the alphabetical order of words is based on the alphabetical order of their component letters.