- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
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
Is it possible to make a case-insensitive query in MongoDB?
Yes, you can use regexp to make a case-insensitive query in MongoDB. The syntax is as follows:
db.yourCollectionName.find({"yourFieldName":/^yourvalue$/i});
To understand the above syntax, let us create a collection with some documents. The query to create a collection with documents is as follows:
> db.caseInsensitiveDemo.insertOne({"Name":"John"}); { "acknowledged" : true, "insertedId" : ObjectId("5c6d7a67f2db199c1278e7ef") } > db.caseInsensitiveDemo.insertOne({"Name":"JOHN"}); { "acknowledged" : true, "insertedId" : ObjectId("5c6d7ad6f2db199c1278e7f0") }
Display all documents from a collection with the help of find(). The query is as follows:
> db.caseInsensitiveDemo.find();
The following is the output:
{ "_id" : ObjectId("5c6d7a67f2db199c1278e7ef"), "Name" : "John" } { "_id" : ObjectId("5c6d7ad6f2db199c1278e7f0"), "Name" : "JOHN" }
Here is the query to make case insensitive query:
> db.caseInsensitiveDemo.find({"Name":/^john$/i});
The following is the output:
{ "_id" : ObjectId("5c6d7a67f2db199c1278e7ef"), "Name" : "John" } { "_id" : ObjectId("5c6d7ad6f2db199c1278e7f0"), "Name" : "JOHN" }
- Related Articles
- How to make a case-insensitive query in MongoDB?
- MongoDB query with case insensitive search?
- MongoDB query for specific case insensitive search
- How do I make case-insensitive queries on MongoDB?
- Is it possible to cast in a MongoDB Query?
- How to make jQuery attribute selector case insensitive?
- Is it possible to return a list of specific values from a query in MongoDB?
- Is it possible to return a list of specific values from a MongoDB query?
- How do I make my string comparison case insensitive?
- Is it possible to make an insert or an update in the same MySQL query?
- How do we make my string comparison case insensitive in java?
- Is it possible to calculate a correlation in a MySQL query?
- Is it possible to achieve a slice chain in MongoDB?
- MongoDB $regex operator i or I for case insensitive search
- How to use case-insensitive switch-case in JavaScript?

Advertisements