- 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
MongoDB 'count()' is very slow. How do we work around with it?
You can use ensureIndex() to boost the performance of count() method in MongoDB. To understand the concept, let us create a collection with the document. The query to create a collection with a document is as follows −
> db.countPerformanceDemo.insertOne({"StudentName":"John","StudentCountryName":"US"}); { "acknowledged" : true, "insertedId" : ObjectId("5c8ebcf82f684a30fbdfd55f") } > db.countPerformanceDemo.insertOne({"StudentName":"Mike","StudentCountryName":"UK"}); { "acknowledged" : true, "insertedId" : ObjectId("5c8ebd042f684a30fbdfd560") } > db.countPerformanceDemo.insertOne({"StudentName":"David","StudentCountryName":"AUS"}); { "acknowledged" : true, "insertedId" : ObjectId("5c8ebd112f684a30fbdfd561") } > db.countPerformanceDemo.insertOne({"StudentName":"Carol","StudentCountryName":"US"}); { "acknowledged" : true, "insertedId" : ObjectId("5c8ebd1a2f684a30fbdfd562") } > db.countPerformanceDemo.insertOne({"StudentName":"Bob","StudentCountryName":"UK"}); { "acknowledged" : true, "insertedId" : ObjectId("5c8ebd212f684a30fbdfd563") } > db.countPerformanceDemo.insertOne({"StudentName":"David","StudentCountryName":"UK"}); { "acknowledged" : true, "insertedId" : ObjectId("5c8ebd9a2f684a30fbdfd564") } > db.countPerformanceDemo.insertOne({"StudentName":"David","StudentCountryName":"US"}); { "acknowledged" : true, "insertedId" : ObjectId("5c8ebd9e2f684a30fbdfd565") }
Display all documents from a collection with the help of find() method. The query is as follows −
> db.countPerformanceDemo.find().pretty();
The following is the output −
{ "_id" : ObjectId("5c8ebcf82f684a30fbdfd55f"), "StudentName" : "John", "StudentCountryName" : "US" } { "_id" : ObjectId("5c8ebd042f684a30fbdfd560"), "StudentName" : "Mike", "StudentCountryName" : "UK" } { "_id" : ObjectId("5c8ebd112f684a30fbdfd561"), "StudentName" : "David", "StudentCountryName" : "AUS" } { "_id" : ObjectId("5c8ebd1a2f684a30fbdfd562"), "StudentName" : "Carol", "StudentCountryName" : "US" } { "_id" : ObjectId("5c8ebd212f684a30fbdfd563"), "StudentName" : "Bob", "StudentCountryName" : "UK" } { "_id" : ObjectId("5c8ebd9a2f684a30fbdfd564"), "StudentName" : "David", "StudentCountryName" : "UK" } { "_id" : ObjectId("5c8ebd9e2f684a30fbdfd565"), "StudentName" : "David", "StudentCountryName" : "US" }
Here is the query to get the high-performance form of count() −
> db.countPerformanceDemo.ensureIndex({"StudentName":1}); { "createdCollectionAutomatically" : false, "numIndexesBefore" : 1, "numIndexesAfter" : 2, "ok" : 1 }
Now, implement count() method. It counts the records with StudentName “David”:
> db.countPerformanceDemo.find({"StudentName":"David"}).count();
The following is the output −
3
- Related Articles
- MySQL Count Distinct values process is very slow. How to fasten it?
- How and why does 'z'['toUpperCase']() in JavaScript work?
- MongoDB query with an 'or' condition?
- How does JavaScript 'return' statement work?
- C++ code to get updated string with same 'a' and 'b' count
- How does $('iframe').ready() method work in jQuery?
- How do I change a MongoDB user's password?
- How can we follow "Don't think about work at home"?
- What does 'is' operator do in Python?
- How does Python's super() work with multiple inheritance?
- Is it possible to combine 'DISTINCT' and 'COUNT' queries, so that I can see how many times each distinct value appears in a MySQL table column?
- Replace '*' with '^' with Java Regular Expressions
- Update 'a' record with 'b' and 'b' with 'a' in a MySQL column (swap) with only 'a' and 'b' values?
- How can I use 'Not Like' operator in MongoDB?
- What does 'is not' operator do in Python?

Advertisements