

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
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 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 } }
- Related Questions & Answers
- MongoDB regular expression to match a specific record?
- What makes John Donne a unique poet?
- MongoDB: Find name similar to Regular Expression input?
- Randomizing unique data with MongoDB and placing values for emailid with word John in the beginning
- Use ObjectId under findOne() to fetch a specific record in MongoDB?
- Text search in MongoDB with Regular Expression
- MongoDB aggregation to fetch documents with specific field value?
- Fetch a specific document in MongoDB with array elements
- Fetch specific field values in MongoDB
- Fetch specific multiple documents in MongoDB
- Fetch a specific column value (name) in MySQL
- MongoDB query to display documents with a specific name irrespective of case
- MongoDB query to get record beginning with specific element from an array?
- Fetch a specific record using a single MySQL query with AND & OR operator
- How to filter some fields in objects and fetch a specific subject name value in MongoDB?
Advertisements