- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
What should be used to implement MySQL LIKE statement in MongoDB?
To get MySQL LIKE statement, use the REGEX in MongoDB. Let us first create a collection with documents −
> db.likeInMongoDBDemo.insertOne({"Name" : "Sam"}); { "acknowledged" : true, "insertedId" : ObjectId("5cd6922857806ebf1256f123") } > db.likeInMongoDBDemo.insertOne({"Name" : "John" }); { "acknowledged" : true, "insertedId" : ObjectId("5cd6923157806ebf1256f124") } > db.likeInMongoDBDemo.insertOne({"Name" : "Scott"}); { "acknowledged" : true, "insertedId" : ObjectId("5cd6924557806ebf1256f125") } > db.likeInMongoDBDemo.insertOne({"Name" : "Sean"}); { "acknowledged" : true, "insertedId" : ObjectId("5cd6924f57806ebf1256f126") } > db.likeInMongoDBDemo.insertOne({"Name" : "Samuel"}); { "acknowledged" : true, "insertedId" : ObjectId("5cd6925857806ebf1256f127") }
Following is the query to display all documents from a collection with the help of find() method −
> db.likeInMongoDBDemo.find().pretty();
This will produce the following output −
{ "_id" : ObjectId("5cd6922857806ebf1256f123"), "Name" : "Sam" } { "_id" : ObjectId("5cd6923157806ebf1256f124"), "Name" : "John" } { "_id" : ObjectId("5cd6924557806ebf1256f125"), "Name" : "Scott" } { "_id" : ObjectId("5cd6924f57806ebf1256f126"), "Name" : "Sean" } { "_id" : ObjectId("5cd6925857806ebf1256f127"), "Name" : "Samuel" }
Following is the query to perform REGEX to match the MySQL LIKE statement in MongoDB −
> db.likeInMongoDBDemo.find({"Name":{$regex:"Sam"}});
This will produce the following output −
{ "_id" : ObjectId("5cd6922857806ebf1256f123"), "Name" : "Sam" } { "_id" : ObjectId("5cd6925857806ebf1256f127"), "Name" : "Samuel" }
- Related Articles
- Why should fossil fuels like coal and petroleum be used judiciously?
- How Can MySQL CASE statement be used in stored procedure?
- Which MySQL Datatype should be used for storing BloodType?
- Explain the scenario in which CURSOR should be used over a standalone SELECT statement?
- How MySQL IF statement can be used in a stored procedure?
- How MySQL WHILE loop statement can be used in stored procedure?
- How MySQL REPEAT loop statement can be used in stored procedure?
- How Can MySQL LOOP statement be used in a stored procedure?
- What are the different wildcard characters that can be used with MySQL LIKE operator?
- How can column data be used within MySQL CASE statement?
- What is the meaning of “SELECT” statement in MySQL and how can it be used?
- How MySQL IF ELSE statement can be used in a stored procedure?
- MySQL isn’t inserting binary data properly? Which datatype should be used?
- How to implement WHILE LOOP with IF STATEMENT MySQL?
- Implement multiple LIKE operators in a single MySQL query

Advertisements