
- MongoDB Tutorial
- MongoDB - Home
- MongoDB - Overview
- MongoDB - Advantages
- MongoDB - Environment
- MongoDB - Data Modeling
- MongoDB - Create Database
- MongoDB - Drop Database
- MongoDB - Create Collection
- MongoDB - Drop Collection
- MongoDB - Data Types
- MongoDB - Insert Document
- MongoDB - Query Document
- MongoDB - Update Document
- MongoDB - Delete Document
- MongoDB - Projection
- MongoDB - Limiting Records
- MongoDB - Sorting Records
- MongoDB - Indexing
- MongoDB - Aggregation
- MongoDB - Replication
- MongoDB - Sharding
- MongoDB - Create Backup
- MongoDB - Deployment
- MongoDB - Java
- MongoDB - PHP
- Advanced MongoDB
- MongoDB - Relationships
- MongoDB - Database References
- MongoDB - Covered Queries
- MongoDB - Analyzing Queries
- MongoDB - Atomic Operations
- MongoDB - Advanced Indexing
- MongoDB - Indexing Limitations
- MongoDB - ObjectId
- MongoDB - Map Reduce
- MongoDB - Text Search
- MongoDB - Regular Expression
- Working with Rockmongo
- MongoDB - GridFS
- MongoDB - Capped Collections
- Auto-Increment Sequence
- MongoDB Useful Resources
- MongoDB - Questions and Answers
- MongoDB - Quick Guide
- MongoDB - Useful Resources
- MongoDB - Discussion
What is the equivalent of SQL “like” in MongoDB?
You can use “$regex” operator to implement the equivalent of SQL ‘like’ in MongoDB. To implement it, let us create a collection with a document. The query to create a collection with a document is as follows −
> db.sqlLikeDemo.insertOne({"UserName":"John Smith","UserAge":24}); { "acknowledged" : true, "insertedId" : ObjectId("5c912e124afe5c1d2279d6a5") } > db.sqlLikeDemo.insertOne({"UserName":"John Doe","UserAge":21}); { "acknowledged" : true, "insertedId" : ObjectId("5c912e264afe5c1d2279d6a6") } > db.sqlLikeDemo.insertOne({"UserName":"Chris Williams","UserAge":22}); { "acknowledged" : true, "insertedId" : ObjectId("5c912e404afe5c1d2279d6a7") } > db.sqlLikeDemo.insertOne({"UserName":"Robert Taylor","UserAge":26}); { "acknowledged" : true, "insertedId" : ObjectId("5c912e4d4afe5c1d2279d6a8") } > db.sqlLikeDemo.insertOne({"UserName":"John Brown","UserAge":27}); { "acknowledged" : true, "insertedId" : ObjectId("5c912e6c4afe5c1d2279d6a9") } > db.sqlLikeDemo.insertOne({"UserName":"Mike Brown","UserAge":23}); { "acknowledged" : true, "insertedId" : ObjectId("5c912e794afe5c1d2279d6aa") } > db.sqlLikeDemo.insertOne({"UserName":"Larry Smith","UserAge":24}); { "acknowledged" : true, "insertedId" : ObjectId("5c912e8c4afe5c1d2279d6ab") }
Display all documents from a collection with the help of find() method. The query is as follows −
> db.sqlLikeDemo.find().pretty();
The following is the output −
{ "_id" : ObjectId("5c912e124afe5c1d2279d6a5"), "UserName" : "John Smith", "UserAge" : 24 } { "_id" : ObjectId("5c912e264afe5c1d2279d6a6"), "UserName" : "John Doe", "UserAge" : 21 } { "_id" : ObjectId("5c912e404afe5c1d2279d6a7"), "UserName" : "Chris Williams", "UserAge" : 22 } { "_id" : ObjectId("5c912e4d4afe5c1d2279d6a8"), "UserName" : "Robert Taylor", "UserAge" : 26 } { "_id" : ObjectId("5c912e6c4afe5c1d2279d6a9"), "UserName" : "John Brown", "UserAge" : 27 } { "_id" : ObjectId("5c912e794afe5c1d2279d6aa"), "UserName" : "Mike Brown", "UserAge" : 23 } { "_id" : ObjectId("5c912e8c4afe5c1d2279d6ab"), "UserName" : "Larry Smith", "UserAge" : 24 }
Here is the query that works like SQL ‘like’ clause i.e. the record with “UserName” John −
> db.sqlLikeDemo.find({"UserName":{"$regex": "John"}}).pretty();
The following is the output −
{ "_id" : ObjectId("5c912e124afe5c1d2279d6a5"), "UserName" : "John Smith", "UserAge" : 24 } { "_id" : ObjectId("5c912e264afe5c1d2279d6a6"), "UserName" : "John Doe", "UserAge" : 21 } { "_id" : ObjectId("5c912e6c4afe5c1d2279d6a9"), "UserName" : "John Brown", "UserAge" : 27 }
- Related Articles
- What is the C# Equivalent of SQL Server DataTypes?
- The equivalent of SQL Server function SCOPE_IDENTITY() in MySQL?
- Equivalent of SQL Server IDENTITY Column in MySQL?
- How to properly use 'exist' function in MongoDB like in SQL?\n
- MongoDB equivalent of WHERE IN(1,2,…)?
- MySQL LIMIT clause equivalent for SQL SERVER?
- Explain the use of sql LIKE operator using MySQL in Python?
- What is the equivalent of EXCEPT in MySQL?
- What is the C++ equivalent of sprintf?
- What is Python equivalent of the ! operator?
- What is the equivalent of C# namespace in Java?
- MongoDB equivalent of SELECT field AS `anothername`?
- What is the use of update command in SQL?
- What is SQL?
- What is the equivalent of a VB module in C#?

Advertisements