Insert the Records into the Specific Collection
We need to create the collection first to demonstrate the LIKE operator functionality in MongoDB. For this, we create the “employee” collection and add the records in that collection using the insertMany() function. Consider the subsequent query of inserting the documents.
{ "id": 1, name:"Mia Gill", "Dept": "Manager" , "status": "Senior"},
{ "id": 2, name:"Charlottes David", "Dept": "Developer", "status": "Junior"},
{ "id": 3, name:"Anna Gilbert", "Dept": "UI/UX Designer", "status": "Intern"},
{ "id": 4, name:"James Bill", "Dept": "Marketing Designer" , "status":
"Senior"},
{ "id": 5, name:"Andy Johns", "Dept": "Team Lead": "Scala" , "status":
"Senior Manager"},
])
Example 1: Get the Document that Begins with the Specified Case-Insensitive Value
In any case, the $regex operator is used firstly to match the pattern whose string begins with the specific string. Consider the implementation of the $regex operator query in the following:
Here, we query the “employee” collection that is created in the previous section. We employ the find() method to search for the document that matches the provided criteria.
For the criteria, we call the $regex operator to perform a regular expression search on the “name” field. There, we assign the regular expression “/avs/i” that finds the “avs” substring in a case-insensitive manner as the “i” flag is being used.
Two records are retrieved from the “employee” collection where the string has the value of “Jo” in any case.
Example 2: Get the Document that Begins with the Specified Letter
In the previous example, the string is used as the regular expression to be matched within the document. Now, we use the character that should start with the specific character in the document.
Here, we call out the find() query again over the same “employee” collection to search for the specific document that matches the specified criteria. Inside the find() query, we input the “Dept” field to which the $regex operator is provided. The $regex operator here takes the “/^M/” expression where the “^” character denotes that the string must begin with the provided letter “M”.
The output represents the records where the “Dept” field contains the values which begin with the character “M”.
Example 3: Get the Document that Ends with the Specified String
Similarly, the $regex expression can also be used to match the string that ends with the provided character which is accomplished in this example.
Here, we set up the $regex operator against the “name” field of the “employee” collection. The find() query holds the criteria of the $regex operation where the expression is passed as “/s$/”. The “$” character that is used after the letter “s” indicates that the string should end with that letter. So, the “find” method only searches for that string that ends with the specific letter “s”.
The result is displayed in the following where it only fetches the document whose “name” field contains the string which ends up with the character “s”:
Example 4: Get the Document that Contains the Specified Value
To get the record where the regular expression matches the specific value anywhere in the string, we enclose the expression with the “/.*” symbol.
Here, we provide the regular expression to the “name” field of the documents without using the $regex operator. The regular “/.*A.*/” expression is for strings that contain the letter “A” anywhere in the “name” field. The letter “A” that is used in between the “.*” character represents that there can be any range of characters before and after the letter “A”.
When the query is executed, it displays the records where the character “A” is found in the “name” field of the documents.
Example 5: Get the Document that Contains the Specified String
Next is the case where we need the string to be matched with the regular expression, either the document begins with that string or only has that string within the specific field.
Here, the regular expression that is specified to the $regex operator is “/^Senior/” to the field status of the “employee” collection. This expression shows that the status field should start with the “Senior” string. Then, the find() method looks for particular documents only and returns them to the output.
There, we get three records in the output whose status field begins with the “Senior” string in the document.
Example 6: Get the Document that Ends with the Specified String
Moreover, the exact word regardless of case-insensitive can also be matched by providing the regular expression. For this, the regular expression must be enclosed with the anchor “\b” which we use in this example.
Here, we use the $regex operator to perform a regular expression search on the “status” field. The regular expression “/\bStudent\b/i” searches for the exact “Designer” word in a case-insensitive manner as the “i” flag is called over it. We use the “\b” character as the boundary anchor which ensures that the “Designer” is matched as a whole word and not as part of a longer word.
The output has all the documents where the field contains the word “Designer” as a whole word, regardless of the case.
Example 7: Get the Document that Contains Multiple Values
However, the regular expression can also be passed with the multiple values for the pattern matching that we deployed in this example. We use the pipe “|” symbol which acts as an OR operator that indicates that the regular expression looks for either of the specified fields.
Here, we define the search criteria of the $regex operator for the “name” field of the “employee” collection. We assign the “/Mia|James” expression for the $regex operator which looks for either the “Mia” or “James” substring in the “name” field. If both exist in the document, both strings are retrieved in the output.
The records are retrieved for the output which satisfies the criteria of the $regex operator.
Conclusion
We demonstrated the LIKE operator functionality in MongoDB via the $regex operator. The $regex operator is used all over the examples for the pattern matching inside the specified collection. It is important to note that the regular expressions should only be used rarely because they can be resource-intensive, especially for extensive collections.