![Trending Articles on Technical and Non Technical topics](/images/trending_categories.jpeg)
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- 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 fetch record with specific name “John”, instead of “john”
For searching a specific word, use /searchWord/ with regex. Let us create a collection with documents −
> db.demo221.insertOne({"Details":{"StudentName":"Chris","StudentAge":21}}); { "acknowledged" : true, "insertedId" : ObjectId("5e3ee15d03d395bdc213472b") } > db.demo221.insertOne({"Details":{"StudentName":"John","StudentAge":20}}); { "acknowledged" : true, "insertedId" : ObjectId("5e3ee16503d395bdc213472c") } > db.demo221.insertOne({"Details":{"StudentName":"Bob","StudentAge":22}}); { "acknowledged" : true, "insertedId" : ObjectId("5e3ee16b03d395bdc213472d") } > db.demo221.insertOne({"Details":{"StudentName":"john","StudentAge":24}}); { "acknowledged" : true, "insertedId" : ObjectId("5e3ee17303d395bdc213472e") }
Display all documents from a collection with the help of find() method −
> db.demo221.find();
This will produce the following output −
{ "_id" : ObjectId("5e3ee15d03d395bdc213472b"), "Details" : { "StudentName" : "Chris", "StudentAge" : 21 } } { "_id" : ObjectId("5e3ee16503d395bdc213472c"), "Details" : { "StudentName" : "John", "StudentAge" : 20 } } { "_id" : ObjectId("5e3ee16b03d395bdc213472d"), "Details" : { "StudentName" : "Bob", "StudentAge" : 22 } } { "_id" : ObjectId("5e3ee17303d395bdc213472e"), "Details" : { "StudentName" : "john", "StudentAge" : 24 } }
Following is the query to fetch a specific record with name “John” −
> db.demo221.find({"Details.StudentName":/John/});
This will produce the following output −
{ "_id" : ObjectId("5e3ee16503d395bdc213472c"), "Details" : { "StudentName" : "John", "StudentAge" : 20 } }
Advertisements