When accessing document sets in MongoDB, you might want to get a count first and then decide whether to retrieve a set of documents. There are several reasons to count specific document sets. Performing a count is much less intensive on the MongoDB side than retrieving documents using find()
and other methods, which cause temporary objects such as Cursor
objects to be created and maintained by the server.
When performing operations on the resulting set of documents from a find()
, you should be aware of how many documents you are going to be dealing with, especially in larger environments. Sometimes all you want is a count. For example, if you need to know how many users are configured in your application, you could just count the number of documents in the users
collection.
The count()
method on the Collection
object allows you to get a simple count of documents that match the query
object criteria. The count()
method is formatted exactly the same way as the find()
method, as shown below, and it performs the query
and options
parameters in exactly the same manner:
count([query], [options], callback)
If no query
value is specified, count()
returns a count of all the documents in the database. The callback function accepts an error as the first argument and the count, as an integer, as the second.
Listing 15.2 uses the count()
method with exactly the same queries performed with find()
in Listing 15.1. The output in Figure 15.2 shows that instead of a Cursor
object, a simple integer is returned and displayed.
01 var MongoClient = require('mongodb').MongoClient;
02 MongoClient.connect("mongodb://localhost/", function(err, db) {
03 var myDB = db.db("words");
04 myDB.collection("word_stats", countItems);
05 setTimeout(function(){
06 db.close();
07 }, 3000);
08 });
09 function countItems(err, words){
10 words.count({first:{$in: ['a', 'b', 'c']}}, function(err, count){
11 console.log("Words starting with a, b or c: " + count);
12 });
13 words.count({size:{$gt: 12}}, function(err, count){
14 console.log("Words longer than 12 characters: " + count);
15 });
16 words.count({size:{$mod: [2,0]}}, function(err, count){
17 console.log("Words with even Lengths: " + count);
18 });
19 words.count({letters:{$size: 12}}, function(err, count){
20 console.log("Words with 12 Distinct characters: " + count);
21 });
22 words.count({$and: [{first:{$in: ['a', 'e', 'i', 'o', 'u']}},
23 {last:{$in: ['a', 'e', 'i', 'o', 'u']}}]},
24 function(err, count){
25 console.log("Words that start and end with a vowel: " + count);
26 });
27 words.count({"stats.vowels":{$gt:6}}, function(err, count){
28 console.log("Words containing 7 or more vowels: " + count);
29 });
30 words.count({letters:{$all: ['a','e','i','o','u']}},
31 function(err, count){
32 console.log("Words with all 5 vowels: " + count);
33 });
34 words.count({otherChars: {$exists:true}}, function(err, count){
35 console.log("Words with non-alphabet characters: " + count);
36 });
37 words.count({charsets:{$elemMatch:{$and:[{type:'other'},
38 {chars:{$size:2}}]}}},
39 function(err, count){
40 console.log("Words with 2 non-alphabet characters: " + count);
41 });
42 }