MongoDB Stats for a particular search

2019-08-19 04:26发布

I'd like to find a function within the MongoDB 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
 });

Any way 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条回答
Emotional °昔
2楼-- · 2019-08-19 05:05

This function is count(). All you need is to do: db.collection.find({category: "Cupcakes"}).count().

Or as pointed by Stennie you can also use db.collection.count({category: "Cupcakes"})

查看更多
登录 后发表回答