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
MongoDB regex to display records whose first five letters are uppercase?
Let us first create a collection with documents −
> db.upperCaseFiveLetterDemo.insertOne({"StudentFullName":"JOHN Smith"});
{
"acknowledged" : true,
"insertedId" : ObjectId("5cd7edef1a844af18acdffb2")
}
> db.upperCaseFiveLetterDemo.insertOne({"StudentFullName":"SAM Williams"});
{
"acknowledged" : true,
"insertedId" : ObjectId("5cd7ee011a844af18acdffb3")
}
> db.upperCaseFiveLetterDemo.insertOne({"StudentFullName":"CAROL Taylor"});
{
"acknowledged" : true,
"insertedId" : ObjectId("5cd7ee101a844af18acdffb4")
}
> db.upperCaseFiveLetterDemo.insertOne({"StudentFullName":"Bob Taylor"});
{
"acknowledged" : true,
"insertedId" : ObjectId("5cd7ee351a844af18acdffb5")
}
> db.upperCaseFiveLetterDemo.insertOne({"StudentFullName":"DAVID Miller"});
{
"acknowledged" : true,
"insertedId" : ObjectId("5cd7ee451a844af18acdffb6")
}
Following is the query to display all documents from a collection with the help of find() method −
> db.upperCaseFiveLetterDemo.find().pretty();
This will produce the following output −
{
"_id" : ObjectId("5cd7edef1a844af18acdffb2"),
"StudentFullName" : "JOHN Smith"
}
{
"_id" : ObjectId("5cd7ee011a844af18acdffb3"),
"StudentFullName" : "SAM Williams"
}
{
"_id" : ObjectId("5cd7ee101a844af18acdffb4"),
"StudentFullName" : "CAROL Taylor"
}
{
"_id" : ObjectId("5cd7ee351a844af18acdffb5"),
"StudentFullName" : "Bob Taylor"
}
{
"_id" : ObjectId("5cd7ee451a844af18acdffb6"),
"StudentFullName" : "DAVID Miller"
}
Following is the query to get the count of the records whose first five letters are in uppercase −
> db.upperCaseFiveLetterDemo.find({StudentFullName : {$regex : /[A-Z]{5}/ } }).count();
This will produce the following output −
2
There are two documents with first five letters uppercase. Let us now display those documents −
> db.upperCaseFiveLetterDemo.find({StudentFullName : {$regex : /[A-Z]{5}/ } });
This will produce the following output −
{ "_id" : ObjectId("5cd7ee101a844af18acdffb4"), "StudentFullName" : "CAROL Taylor" }
{ "_id" : ObjectId("5cd7ee451a844af18acdffb6"), "StudentFullName" : "DAVID Miller" }Advertisements