
- 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 query with case insensitive search?
For case, insensitive search, use regex in find() method. Following is the syntax −
db.demo572.find({"yourFieldName" : { '$regex':/^yourValue$/i}});
To understand the above syntax, let us create a collection with documents −
> db.demo572.insertOne({"CountryName":"US"});{ "acknowledged" : true, "insertedId" : ObjectId("5e915f0e581e9acd78b427f1") } > db.demo572.insertOne({"CountryName":"UK"});{ "acknowledged" : true, "insertedId" : ObjectId("5e915f17581e9acd78b427f2") } > db.demo572.insertOne({"CountryName":"Us"});{ "acknowledged" : true, "insertedId" : ObjectId("5e915f1b581e9acd78b427f3") } > db.demo572.insertOne({"CountryName":"AUS"});{ "acknowledged" : true, "insertedId" : ObjectId("5e915f20581e9acd78b427f4") } > db.demo572.insertOne({"CountryName":"us"});{ "acknowledged" : true, "insertedId" : ObjectId("5e915f25581e9acd78b427f5") }
Display all documents from a collection with the help of find() method −
> db.demo572.find();
This will produce the following output −
{ "_id" : ObjectId("5e915f0e581e9acd78b427f1"), "CountryName" : "US" } { "_id" : ObjectId("5e915f17581e9acd78b427f2"), "CountryName" : "UK" } { "_id" : ObjectId("5e915f1b581e9acd78b427f3"), "CountryName" : "Us" } { "_id" : ObjectId("5e915f20581e9acd78b427f4"), "CountryName" : "AUS" } { "_id" : ObjectId("5e915f25581e9acd78b427f5"), "CountryName" : "us" }
Following is the query for case insensitive search −
> db.demo572.find({"CountryName" : { '$regex':/^US$/i}});
This will produce the following output −
{ "_id" : ObjectId("5e915f0e581e9acd78b427f1"), "CountryName" : "US" } { "_id" : ObjectId("5e915f1b581e9acd78b427f3"), "CountryName" : "Us" } { "_id" : ObjectId("5e915f25581e9acd78b427f5"), "CountryName" : "us" }
- Related Questions & Answers
- MongoDB query for specific case insensitive search
- Case insensitive search in Mongo?
- How to make a case-insensitive query in MongoDB?
- MongoDB $regex operator i or I for case insensitive search
- Is it possible to make a case-insensitive query in MongoDB?
- How to perform case-insensitive search in Oracle?
- How to achieve case sensitive uniqueness and case insensitive search in MySQL?
- How do I make case-insensitive queries on MongoDB?
- MySQL case-insensitive DISTINCT?
- Case-insensitive Dictionary in C#
- JavaScript Count characters case insensitive
- How can I search (case-insensitive) in a column using LIKE wildcard?
- How to perform Case Insensitive matching with JavaScript RegExp?
- How to use case-insensitive switch-case in JavaScript?
- Case-insensitive string comparison in C++
Advertisements