
- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to query MongoDB with “like”?
You can easily query MongoDB with “like”:
db.yourCollectionName.find({"yourFieldName" : /.*yourMatchingValue.*/}).pretty();
To understand the above syntax, let us create a collection with some documents. Here, we have a collection with the name ‘employee’. The query is as follows:
> db.employee.insert({"EmployeeName":"John","EmployeeSalary":450000}); WriteResult({ "nInserted" : 1 }) > db.employee.insert({"EmployeeName":"Carol","EmployeeSalary":150000}); WriteResult({ "nInserted" : 1 }) > db.employee.insert({"EmployeeName":"James","EmployeeSalary":550000}); WriteResult({ "nInserted" : 1 }) > db.employee.insert({"EmployeeName":"Jace","EmployeeSalary":670000}); WriteResult({ "nInserted" : 1 }) > db.employee.insert({"EmployeeName":"Larry","EmployeeSalary":1000000}); WriteResult({ "nInserted" : 1 })
Now you can display all documents from a collection using find() method. The query is as follows:
> db.employee.find().pretty();
The following is the output:
{ "_id" : ObjectId("5c6c0b2e68174aae23f5ef59"), "EmployeeName" : "John", "EmployeeSalary" : 450000 } { "_id" : ObjectId("5c6c0b3b68174aae23f5ef5a"), "EmployeeName" : "Carol", "EmployeeSalary" : 150000 } { "_id" : ObjectId("5c6c0b4768174aae23f5ef5b"), "EmployeeName" : "James", "EmployeeSalary" : 550000 } { "_id" : ObjectId("5c6c0b8f68174aae23f5ef5c"), "EmployeeName" : "Jace", "EmployeeSalary" : 670000 } { "_id" : ObjectId("5c6c0b9e68174aae23f5ef5d"), "EmployeeName" : "Larry", "EmployeeSalary" : 1000000 }
Here is the query in MongoDB with “like” operator:
> db.employee.find({"EmployeeName":/.*J.*/}).pretty();
The following is the output:
{ "_id" : ObjectId("5c6c0b2e68174aae23f5ef59"), "EmployeeName" : "John", "EmployeeSalary" : 450000 } { "_id" : ObjectId("5c6c0b4768174aae23f5ef5b"), "EmployeeName" : "James", "EmployeeSalary" : 550000 } { "_id" : ObjectId("5c6c0b8f68174aae23f5ef5c"), "EmployeeName" : "Jace", "EmployeeSalary" : 670000 }
- Related Questions & Answers
- How to query MongoDB similar to “like” ?
- “Toggle” query in MongoDB?
- MongoDB query to search for string like “@email” in the field values
- Query MongoDB with “like” implementation on name and email field beginning with a specific letter?
- MongoDB: How to query a collection named “version”?
- Can we use “LIKE concat()” in a MySQL query?
- What is the equivalent of SQL “like” in MongoDB?
- MongoDB Query for boolean field as “not true”
- How to use “not in” query with C# LINQ?
- MySQL query to convert a string like “1h 15 min” into 75 minutes?
- MongoDB regular expression to fetch record with specific name “John”, instead of “john”
- How to use MySQL LIKE clause to fetch multiple values beginning with “Joh”
- How to efficiently perform “distinct” with multiple keys in MongoDB?
- MongoDB query to fetch only the “Name” field based on roles?
- How to work with “!=” or “not equals” in MySQL WHERE?
Advertisements