
- 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
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" }
- Related Articles
- Display MongoDB records by ObjectId?
- JavaScript regex program to display name to be only numbers, letters and underscore.
- MongoDB query to skip first 5 records and display only the last 5 of them
- MongoDB query to retrieve records from a collection named with letters and numbers
- Display the undefined and exact MongoDB document records
- How to convert lowercase letters in string to uppercase in Python?
- How to find capital letters with Regex in MySQL?
- How to convert all uppercase letters in string to lowercase in Python?
- MongoDB query to group records and display a specific value with dot notation
- How to ORDER BY DESC and display the first 3 records in MySQL?
- Display records by first fixing the first two values in a column and then using DISTINCT to display other values in MySQL
- How to use $regex in MongoDB?
- Styling First-Letters with CSS ::first-letter
- For the Hosts Locale how to convert a string to uppercase letters in JavaScript?
- MySQL query to count records that begin with specific letters

Advertisements