
- 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
MongoDB regular expression to match a specific record?
Let us first create a collection with documents −
> dbworkingOfRegularExpressionDemoinsertOne({ "StudentDetails" : { "StudentName" : "John" },"StudentAge":21 }); { "acknowledged" : true, "insertedId" : ObjectId("5cf227acb64a577be5a2bc07") } > dbworkingOfRegularExpressionDemoinsertOne({ "StudentDetails" : { "StudentName" : "JOHN" },"StudentAge":19 }); { "acknowledged" : true, "insertedId" : ObjectId("5cf227b8b64a577be5a2bc08") } > dbworkingOfRegularExpressionDemoinsertOne({ "StudentDetails" : { "StudentName" : "Carol" },"StudentAge":20 }); { "acknowledged" : true, "insertedId" : ObjectId("5cf227c2b64a577be5a2bc09") }
Following is the query to display all documents from a collection with the help of find() method −
> dbworkingOfRegularExpressionDemofind();
This will produce the following document −
{ "_id" : ObjectId("5cf227acb64a577be5a2bc07"), "StudentDetails" : { "StudentName" : "John" }, "StudentAge" : 21 } { "_id" : ObjectId("5cf227b8b64a577be5a2bc08"), "StudentDetails" : { "StudentName" : "JOHN" }, "StudentAge" : 19 } { "_id" : ObjectId("5cf227c2b64a577be5a2bc09"), "StudentDetails" : { "StudentName" : "Carol" }, "StudentAge" : 20 } Following is the regular expression to get the document with StudentName JOHN: > dbworkingOfRegularExpressionDemofind({'StudentDetailsStudentName': /JOHN/});
This will produce the following document −
{ "_id" : ObjectId("5cf227b8b64a577be5a2bc08"), "StudentDetails" : { "StudentName" : "JOHN" }, "StudentAge" : 19 }
- Related Articles
- MongoDB regular expression to fetch record with specific name “John”, instead of “john”
- How to match a regular expression against a string?
- PHP – Match regular expression using mb_ereg_match()
- Regular expression to match numbers only in JavaScript?
- How to match parentheses in Python regular expression?
- How to match a word in python using Regular Expression?
- How to match a whitespace in python using Regular Expression?
- Explain Python regular expression search vs match
- How to match digits using Java Regular Expression (RegEx)
- How to query an Array[String] for a Regular Expression match?
- How to match a nonwhitespace character in python using Regular Expression?
- How to match a single character in python using Regular Expression?
- How to use regular expression in Java to pattern match?
- How to write a Python regular expression to match multiple words anywhere?
- MongoDB query to find a specific city record from a collection

Advertisements