当前位置:嗨网首页>书籍在线阅读

02-计算文档数

  
选择背景色: 黄橙 洋红 淡粉 水蓝 草绿 白色 选择字体: 宋体 黑体 微软雅黑 楷体 选择字体大小: 恢复默认

7.1 计算文档数

使用count()来获悉MongoDB shell Cursor表示多少个文档 在本节中,您将对示例数据集执行各种find()操作,并对它们返回的Cursor对象调用方法count()。这里将首先演示count(),再加深您对方法find()的查询结构的认识。 程序清单7.1所示的示例执行多种find()操作,并输出在数据库中找到的与查询参数匹配的文档数。 请执行下面的步骤,编写这个在数据库words中查找文档并显示找到了多少个文档的脚本。 1.确保启动了MongoDB服务器。 2.确保运行了生成数据库words的脚本文件code/hour05/generate_words.js。 3.在文件夹code/hour07中新建一个文件,并将其命名为find_count.js。 4.在这个文件中输入程序清单7.1所示的代码。这些代码连接到数据库words,获取表示集合word_stats的Collection对象,使用find()和各种查询运算符来检索单词,并显示每个find()操作返回的Cursor对象表示多少个单词。 5.将这个文件存盘。 6.打开一个控制台窗口,并切换到目录code/hour07。 7.执行下面的命令来运行这个脚本,它将显示对集合word_stats执行的各种查询返回的单词数。程序清单7.2显示了这个脚本的输出。 程序清单7.1 find_count.js:使用MongoDB shell在集合中查找文档并计算找到的文档数 程序清单7.2 find_count.js-output:使用MongoDB shell查找文档并计算找到的文档数的输出

访问MongoDB中的文档集时,您可能想在取回文档前获悉有多少个文档。在MongoDB服务器和客户端计算文档数的开销很小,因为不需要实际传输文档。

对find()返回的文档集执行操作时,也应明了将要处理的文档有多少,在大型环境中尤其如此。有时候,您只想知道文档数。例如,如果您要获悉应用程序中配置了多少用户,只需计算集合users中有多少个文档。

Cursor对象的方法count()指出它表示多少个文档。例如,下面的代码使用方法find()获取一个Cursor对象,再使用方法count()获取文档数:

cursor = wordsColl.find({first: {$in: ['a', 'b', 'c']}});
itemCount = cursor.count();

itemCount的值为与find()查询匹配的单词数。

▼ Try It Yourself

mongo find_count.js
01 mongo = new Mongo("localhost");
02 wordsDB = mongo.getDB("words");
03 wordsColl = wordsDB.getCollection("word_stats");
04 cursor = wordsColl.find({first: {$in: ['a', 'b', 'c']}});
05 print("Words starting with a, b or c: ", cursor.count());
06 cursor = wordsColl.find({size:{$gt: 12}});
07 print("Words longer than 12 characters: ", cursor.count());
08 cursor = wordsColl.find({size:{$mod: [2,0]}});
09 print("Words with even Lengths: ", cursor.count());
10 cursor = wordsColl.find({letters:{$size: 12}});
11 print("Words with 12 Distinct characters: ", cursor.count());
12 cursor = wordsColl.find({$and:
13                                  [{first:{
14                                      $in: ['a', 'e', 'i', 'o', 'o']}},
15                                  {last:{
16                                      $in: ['a', 'e', 'i', 'o', 'o']}}]});
17 print("Words that start and end with a vowel: ", cursor.count());
18 cursor = wordsColl.find({"stats.vowels":{$gt: 6}});
19 print("Words containing 7 or more vowels: ", cursor.count());
20 cursor = wordsColl.find({letters:{$all: ['a','e','i','o','u']}});
21 print("Words with all 5 vowels: ", cursor.count());
22 cursor = wordsColl.find({otherChars: {$exists: true}});
23 print("Words with non-alphabet characters: ", cursor.count());
24 cursor = wordsColl.find({charsets:{
25                                 $elemMatch:{
26                                    $and:[{type: 'other'},
27                                           {chars: {$size: 2}}]}}});
28 print("Words with 2 non-alphabet characters: ", cursor.count());
Words starting with a, b or c: 1098
Words longer than 12 characters: 65
Words with even Lengths: 2587
Words with 12 Distinct characters: 3
Words that start and end with a vowel: 258
Words containing 7 or more vowels: 4
Words with all 5 vowels: 16
Words with non-alphabet characters: 31
Words with 2 non-alphabet characters: 2