![]() |
Querying data in MongoDB often requires pattern–matching operations similar to SQL’s “LIKE” operator. While MongoDB doesn’t have a direct “LIKE” operator, it offers the $regex operator and regular expressions for achieving similar functionality. In this article, We will learn about How to Query MongoDB with “like” with the help of $regex and regular expression in detail along with the implementations and so on. How to Query MongoDB with “like”?To query MongoDB with a “like” operator equivalent, we can use the $regex operator along with regular expressions. However, MongoDB offers several methods to query MongoDB with “like” are defined below:
Syntax:db.collection.find({ "field": { $regex: /pattern/i } });
Let’s set up an Environment: To understand How to query MongoDB with “like” we need a collection and some documents on which we will perform various operations and queries. Here we will consider a collection called users which contains information like name, email, username, bio, and city of the Employees in various documents. > db.users.insertMany([
{
"name": "John Doe",
"email": "[email protected]",
"username": "johndoe",
"bio": "Software Engineer with a passion for coding",
"city": "New York"
},
{
"name": "Alice Smith",
"email": "[email protected]",
"username": "alicesmith",
"bio": "Web Developer interested in UX design",
"city": "San Francisco"
},
{
"name": "francis",
"email": "[email protected]",
"username": "francis",
"bio": "data scientist",
"city": "Madagascar"
}
]);
Output: ![]() user collections created 1. Using the $regex OperatorIn MongoDB, the $regex operator is a powerful tool for performing pattern matching operations within queries. It allows for flexible searching based on regular expressions, enabling refined search capabilities similar to SQL’s “LIKE” operator. Example 1Let’s Retrieve documents from the “users” collection where the “name” field contains the case-insensitive string “john”. // Querying the documents where name contains with John
db.users.find({ "name": { $regex: /john/i } });
Output: ![]() Querying MongoDB with “like” Explanation: In the above output, we quered all the documents where name contains with “john”. Example 2Let’s Find all documents in the “users” collection where the “username” field contains the case-insensitive string “alice” // Querying the documents where username contains alice
db.users.find({ "username": { $regex: /alice/i } });
Output: ![]() Querying MongoDB with “like” Explanation: In the above output, we quered all the documents where username contains “alice”. 2. Using Regular ExpressionsIn MongoDB, Regular Expressions (regex) offer a powerful tool for querying and searching data based on specific patterns within fields. Regular expressions allow for flexible and dynamic searches, akin to SQL’s “LIKE” operator. Example 1:Let’s Retrieve documents from the “users” collection where the “bio” field contains the case-insensitive substring “data” // Find documents where the bio contains "data" (case insensitive)
db.users.find({ bio: { $regex: /data/, $options: 'i' } })
Output: ![]() Querying MongoDB with “like” Explanation: In the above output, we quered all the documents where username contains “alice”. Example 2Let’s Retrieve documents from the “users” collection where the “email” field contains the substring “john” using a multi-line matching pattern. // Find documents where the email contains "john"
db.users.find({ email: { $regex: /john/, $options: 'm' } })
Output: ![]() Querying MongoDB with “like” Explanation: In the above output, we quered all the documents where the email contains “john”. ConclusionOverall, Querying MongoDB with “like” operators is a powerful feature provided by MongoDB’s $regex operator and regular expressions. With the help of these features, you can perform flexible and dynamic searches within your MongoDB collections, enhancing your data querying capabilities. |
Reffered: https://www.geeksforgeeks.org
Databases |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 13 |