Find a value in lowercase from a MongoDB collection with documents



To find a value in lowercase, use the toLowerCase() method in MongoDB. Use the method in find() to find the value in lowercase.

Let us create a collection with documents −

> db.demo172.insertOne({"SubjectName":"MySQL"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e3838ce9e4f06af551997e1") } > db.demo172.insertOne({"SubjectName":"mongodb"}); {     "acknowledged" : true,    "insertedId" : ObjectId("5e3838d69e4f06af551997e2") } > db.demo172.insertOne({"SubjectName":"MongoDB"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e3838db9e4f06af551997e3") }

Display all documents from a collection with the help of find() method −

> db.demo172.find();

This will produce the following output −

{ "_id" : ObjectId("5e3838ce9e4f06af551997e1"), "SubjectName" : "MySQL" } { "_id" : ObjectId("5e3838d69e4f06af551997e2"), "SubjectName" : "mongodb" } { "_id" : ObjectId("5e3838db9e4f06af551997e3"), "SubjectName" : "MongoDB" }

Following is the query to find a value in lowercase −

> db.demo172.find({"SubjectName":"MONGODB".toLowerCase()});

This will produce the following output −

{ "_id" : ObjectId("5e3838d69e4f06af551997e2"), "SubjectName" : "mongodb" }
Updated on: 2020-03-27T06:45:06+05:30

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements