
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
Find Name Similar to Regular Expression in MongoDB
Send the name with $regex in MongoDB to find a name similar to the input. Let us create a collection with documents −
> db.demo514.insertOne({"Information":{"FullName":"John Doe"}});{ "acknowledged" : true, "insertedId" : ObjectId("5e885116987b6e0e9d18f58c") } > db.demo514.insertOne({"Information":{"FullName":"John Smith"}});{ "acknowledged" : true, "insertedId" : ObjectId("5e88515e987b6e0e9d18f58d") } > db.demo514.insertOne({"Information":{"FullName":"john doe"}});{ "acknowledged" : true, "insertedId" : ObjectId("5e885169987b6e0e9d18f58e") } > db.demo514.insertOne({"Information":{"FullName":"Chris Brown"}});{ "acknowledged" : true, "insertedId" : ObjectId("5e88516f987b6e0e9d18f58f") }
Display all documents from a collection with the help of find() method −
> db.demo514.find();
This will produce the following output −
{ "_id" : ObjectId("5e885116987b6e0e9d18f58c"), "Information" : { "FullName" : "John Doe" } } { "_id" : ObjectId("5e88515e987b6e0e9d18f58d"), "Information" : { "FullName" : "John Smith" } } { "_id" : ObjectId("5e885169987b6e0e9d18f58e"), "Information" : { "FullName" : "john doe" } } { "_id" : ObjectId("5e88516f987b6e0e9d18f58f"), "Information" : { "FullName" : "Chris Brown" } }
Following is the query to find name similar to input −
> db.demo514.find({'Information.FullName': {$regex: "John Doe", $options: 'i'}});
This will produce the following output −
{ "_id" : ObjectId("5e885116987b6e0e9d18f58c"), "Information" : { "FullName" : "John Doe" } } { "_id" : ObjectId("5e885169987b6e0e9d18f58e"), "Information" : { "FullName" : "john doe" } }
Advertisements