Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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 }Advertisements