
- 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
Using a regex with text search in MongoDB
To filter records using Regular Expression in MongoDB, use $regex. Let us first create a collection with documents −
> db.demo19.insertOne({"Values":"4321GH"}); { "acknowledged" : true, "insertedId" : ObjectId("5e1389b955d0fc6657d21f0f") } > db.demo19.insertOne({"Values":"12321_Carol"}); { "acknowledged" : true, "insertedId" : ObjectId("5e1389c755d0fc6657d21f10") } > db.demo19.insertOne({"Values":"8765Mike"}); { "acknowledged" : true, "insertedId" : ObjectId("5e1389d355d0fc6657d21f11") }
Following is the query to display all documents from a collection with the help of find() method −
> db.demo19.find();
This will produce the following output −
{ "_id" : ObjectId("5e1389b955d0fc6657d21f0f"), "Values" : "4321GH" } { "_id" : ObjectId("5e1389c755d0fc6657d21f10"), "Values" : "12321_Carol" } { "_id" : ObjectId("5e1389d355d0fc6657d21f11"), "Values" : "8765Mike" }
Here is the query to use regex with text search in MongoDB −
> db.demo19.find({Values: {$regex: /4321|8765/, $options: 'i'}});
This will produce the following output −
{ "_id" : ObjectId("5e1389b955d0fc6657d21f0f"), "Values" : "4321GH" } { "_id" : ObjectId("5e1389d355d0fc6657d21f11"), "Values" : "8765Mike" }
- Related Articles
- Text search in MongoDB with Regular Expression
- MongoDB Regex Search on Integer Value?
- Implement Text search in MongoDB
- Using regex in MongoDB findOne()
- Perform MongoDB full text search
- Create an index for text search in MongoDB
- MongoDB $regex operator i or I for case insensitive search
- How to Split Text Using Regex in Golang?
- Avoid MongoDB performance issues while using regex
- Significance of regex match() and regex search() function in Python
- Search a string with special characters in a MongoDB document?
- Search a value in an object with number keys with MongoDB
- Set regex in MongoDB $in?
- Adding default search text to search box in HTML with JavaScript?
- MongoDB query with case insensitive search?

Advertisements