- 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
Count the documents with a field value beginning with 13
To count the documents, use $count. For values beginning with 13, use $regex. You can use $regex. Let us create a collection with documents −
> db.demo570.insertOne({Information:{Value:"13675"}});{ "acknowledged" : true, "insertedId" : ObjectId("5e90959b39cfeaaf0b97b583") } > db.demo570.insertOne({Information:{Value:"14135"}});{ "acknowledged" : true, "insertedId" : ObjectId("5e9095a739cfeaaf0b97b584") } > db.demo570.insertOne({Information:{Value:"15113"}});{ "acknowledged" : true, "insertedId" : ObjectId("5e9095b639cfeaaf0b97b585") } > db.demo570.insertOne({Information:{Value:"13141"}});{ "acknowledged" : true, "insertedId" : ObjectId("5e9095c139cfeaaf0b97b586") }
Display all documents from a collection with the help of find() method −
> db.demo570.find();
This will produce the following output −
{ "_id" : ObjectId("5e90959b39cfeaaf0b97b583"), "Information" : { "Value" : "13675" } } { "_id" : ObjectId("5e9095a739cfeaaf0b97b584"), "Information" : { "Value" : "14135" } } { "_id" : ObjectId("5e9095b639cfeaaf0b97b585"), "Information" : { "Value" : "15113" } } { "_id" : ObjectId("5e9095c139cfeaaf0b97b586"), "Information" : { "Value" : "13141" } }
Following is the query for faster performance that will use an index −
> db.demo570.count({'Information.Value': { '$regex' : '^13' }});
This will produce the following output −
2
- Related Articles
- MongoDB aggregation to fetch documents with specific field value?
- Find documents with arrays not containing a document with a particular field value in MongoDB?
- Padding the beginning of a MySQL INT field with zeroes?
- Match MongoDB documents with field value greater than a specific number and fetch them?
- MongoDB query to fetch a specific document rom documents with field value set using NumberInt()?
- How can I count the documents in an array based on the value of a specific field?
- Query MongoDB with “like” implementation on name and email field beginning with a specific letter?
- Return the field with highest count in MySQL
- Get a count of total documents with MongoDB while using limit?
- Get count of array elements from a specific field in MongoDB documents?
- MongoDB query to update each field of documents in collection with a formula?
- Find a value in lowercase from a MongoDB collection with documents
- How to get the maximum value from a column with alphanumeric strings beginning with specific characters in MYSQL?
- How do I find all documents with a field that is NaN in MongoDB?
- Set MongoDB compound index with a fixed value field

Advertisements