Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Web Technology by (4k points)

I'd like to find a function within the MongoDB the shell that will let me see how many items are in a particular query.

For instance, I want to do something akin to:

 db.collection.find({category: "Cupcakes"}).stats()

and see count, file size, that sort of thing.

In MongoJS and other front-end implementations of MongoDb a query returns an object that you can perform a .length method on, like:

 db.collection.find({category: "Cupcakes"}, function(err, records){
       console.log(records.length); // Shows how many records are in the search field
 });

Anyway to do something similar in the shell itself? Would be insanely useful but I can't find any docs or mention anywhere of this.

1 Answer

0 votes
by (8.7k points)

To overcome the above problem statement, you can use Count() as:

db.collection.find({category: "Cupcakes"}).count()

Also, you can achieve it by the  below command:

db.collection.count({category: "Cupcakes"})  

Want to learn more, Explore the  MongoDB tutorial by Intellipaat. 

Browse Categories

...