
- 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
How to use $regex in MongoDB?
Following is the syntax to use $regex in MongoDB −
db.yourCollectionName.find({yourFieldName: { $regex: yourValue}});
Let us first create a collection with documents −
> db.regularExpressionDemo.insertOne({"UserName":"John"}); { "acknowledged" : true, "insertedId" : ObjectId("5cdffc25bf3115999ed51210") } > db.regularExpressionDemo.insertOne({"UserName":"JOHN"}); { "acknowledged" : true, "insertedId" : ObjectId("5cdffc2ebf3115999ed51211") } > db.regularExpressionDemo.insertOne({"UserName":"john"}); { "acknowledged" : true, "insertedId" : ObjectId("5cdffc35bf3115999ed51212") } > db.regularExpressionDemo.insertOne({"UserName":"JoHn"}); { "acknowledged" : true, "insertedId" : ObjectId("5cdffc3ebf3115999ed51213") }
Following is the query to display all documents from a collection with the help of find() method −
> db.regularExpressionDemo.find();
This will produce the following output −
{ "_id" : ObjectId("5cdffc25bf3115999ed51210"), "UserName" : "John" } { "_id" : ObjectId("5cdffc2ebf3115999ed51211"), "UserName" : "JOHN" } { "_id" : ObjectId("5cdffc35bf3115999ed51212"), "UserName" : "john" } { "_id" : ObjectId("5cdffc3ebf3115999ed51213"), "UserName" : "JoHn" }
Following is the query to use $regex −
> db.regularExpressionDemo.find({'UserName': { $regex: 'JOHN'}});
This will produce the following output −
{ "_id" : ObjectId("5cdffc2ebf3115999ed51211"), "UserName" : "JOHN" }
Let us now match all the cases. Following is the query −
> db.regularExpressionDemo.find({'UserName': { $regex: 'JOHN', $options: 'i' }});
This will produce the following output −
{ "_id" : ObjectId("5cdffc25bf3115999ed51210"), "UserName" : "John" } { "_id" : ObjectId("5cdffc2ebf3115999ed51211"), "UserName" : "JOHN" } { "_id" : ObjectId("5cdffc35bf3115999ed51212"), "UserName" : "john" } { "_id" : ObjectId("5cdffc3ebf3115999ed51213"), "UserName" : "JoHn" }
- Related Articles
- Is it possible to use MongoDB field value as pattern in $regex?
- How to use regex in selenium locators?
- Is it possible to use MongoDB field value as a pattern in $regex?
- How to use R in Java-8 regex.
- Set regex in MongoDB $in?
- Using regex in MongoDB findOne()
- Regex to ignore a specific character in MongoDB?
- How to use $type in MongoDB?
- How to use collMod in MongoDB runCommand()?
- How to use deleteOne() function in MongoDB?
- How to use save() correctly in MongoDB?
- How do I use capturing groups in Java Regex?
- MongoDB Regex Search on Integer Value?
- Using a regex with text search in MongoDB
- How to Use Go With MongoDB?

Advertisements