
- 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
Case insensitive search in Mongo?
You can restrict case insensitive search in MongoDB with the help of '$regex'. The syntax is as follows −
db.yourCollectionName.find({"yourFieldName" : { '$regex':'^yourValue$'}});
You can use another regex. The syntax is as follows −
db.yourCollectionName.find({"Name" : { '$regex':/^yourValue$/i}});
To understand the concept, let us create a collection with the document. The query to create a collection with a document is as follows −
> db.caseInsesitiveDemo.insertOne({"Name":"John"}); { "acknowledged" : true, "insertedId" : ObjectId("5c8bd66293c80e3f23815e83") } > db.caseInsesitiveDemo.insertOne({"Name":"Johnson"}); { "acknowledged" : true, "insertedId" : ObjectId("5c8bd66693c80e3f23815e84") } > db.caseInsesitiveDemo.insertOne({"Name":"Johny"}); { "acknowledged" : true, "insertedId" : ObjectId("5c8bd66a93c80e3f23815e85") }
Display all documents from a collection with the help of find() method. The query is as follows −
> db.caseInsesitiveDemo.find().pretty();
The following is the output −
{ "_id" : ObjectId("5c8bd66293c80e3f23815e83"), "Name" : "John" } { "_id" : ObjectId("5c8bd66693c80e3f23815e84"), "Name" : "Johnson" } { "_id" : ObjectId("5c8bd66a93c80e3f23815e85"), "Name" : "Johny" }
If you use below the type of regex then the list documents would be visible. The query is as follows −
> db.caseInsesitiveDemo.find({"Name" : { '$regex' : 'John' }});
The following is the output −
{ "_id" : ObjectId("5c8bd66293c80e3f23815e83"), "Name" : "John" } { "_id" : ObjectId("5c8bd66693c80e3f23815e84"), "Name" : "Johnson" } { "_id" : ObjectId("5c8bd66a93c80e3f23815e85"), "Name" : "Johny" }
Case 1 − If you want to restrict all those documents from displaying, use the first query −
> db.caseInsesitiveDemo.find({"Name" : { '$regex':'^John$'}});
The following is the output −
{ "_id" : ObjectId("5c8bd66293c80e3f23815e83"), "Name" : "John" }
Look at the above sample output, only ‘John’ is displaying..
Case 2 − If you want to restrict all those documents from displaying, use the second query.
The query is as follows −
> db.caseInsesitiveDemo.find({"Name" : { '$regex':/^John$/i}});
The following is the output −
{ "_id" : ObjectId("5c8bd66293c80e3f23815e83"), "Name" : "John" }
- Related Articles
- MongoDB query with case insensitive search?
- How to perform case-insensitive search in Oracle?
- MongoDB query for specific case insensitive search
- How to achieve case sensitive uniqueness and case insensitive search in MySQL?
- MongoDB $regex operator i or I for case insensitive search
- How can I search (case-insensitive) in a column using LIKE wildcard?
- Case-insensitive Dictionary in C#
- MySQL case-insensitive DISTINCT?
- Is Python case-sensitive or case-insensitive?
- Case-insensitive string comparison in C++
- How to use case-insensitive switch-case in JavaScript?
- JavaScript Count characters case insensitive
- Perform case insensitive SELECT using MySQL IN()?
- JavaScript Check for case insensitive values?
- Case-insensitive string replacement using Python Program
