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

06-限制返回的字段

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

7.3.2 限制返回的字段

使用projection参数减少MongoDB shell Cursor对象表示的文档中的字段数 在本节中,您将使用方法find()的projection参数来排除字段或指定要包含的字段,从而减少服务器返回的数据量。程序清单7.7所示的示例执行各种find()操作,并使用projection参数来减少返回的文档包含的字段数。 方法displayWords()设置单词的输出格式,并使用方法map()迭代游标。请注意,显示的字段随projection参数而异。 请执行如下步骤来编写这个脚本,对find()操作从数据库words中返回的字段进行限制。 1.确保启动了MongoDB服务器。 2.确保运行了生成数据库words的脚本文件code/hour05/generate_words.js。 3.在文件夹code/hour07中新建一个文件,并将其命名为find_fields.js。 4.在这个文件中输入程序清单7.7所示的代码。这些代码连接到数据库words,获取表示集合word_stats的Collection对象,再调用find()并使用带projection参数来控制要返回文档的哪些字段。 5.将这个文件存盘。 6.打开一个控制台窗口,并切换到目录code/hour07。 7.执行下面的命令以运行这个脚本,它对集合word_stats执行各种查询,并对返回的文档包含的字段进行限制。程序清单7.8显示这个脚本的输出。 程序清单7.7 find_fields.js:在MongoDB shell中对Cursor对象返回的文档包含的字段进行限制 程序清单7.8 find_fields.js-output:在MongoDB shell中对Cursor对象返回的文档包含的字段进行限制的输出

为限制文档检索时返回的数据量,另一种极有效的方式是限制要返回的字段。文档可能有很多字段在有些情况下很有用,但在其他情况下没用。从MongoDB服务器检索文档时,需考虑应包含哪些字段,并只请求必要的字段。

要对find()操作从服务器返回的字段进行限制,可在find()操作中使用projection参数。projection参数是一个将字段名用作属性的JavaScript对象,让您能够包含或排除字段:将属性值设置为0/false表示排除;将属性值设置为1/true表示包含。然而,在同一个表达式中,不能同时指定包含和排除。

例如,返回first字段为t的文档时,要排除字段stats、value和comments,可使用下面的projection参数:

find({first:"t"}, {stats:false, value:false, comments:false});

仅包含所需的字段通常更容易。例如,如果只想返回first字段为t的文档的word和size字段,可使用下面的代码:

find({first:"t"}, {word:1, size:1 });

▼ Try It Yourself

mongo find_fields.js
01 function displayWords(cursor){
02    words = cursor.forEach(function(word){
03       print(JSON.stringify(word, null, 2));
04    });
05 }
06 mongo = new Mongo("localhost");
07 wordsDB = mongo.getDB("words");
08 wordsColl = wordsDB.getCollection("word_stats");
09 cursor = wordsColl.find({first:'p'});
10 print("Full Word:");
11 displayWords(cursor.limit(1));
12 cursor = wordsColl.find({first:'p'}, {word:1});
13 print("Only the word field:");
14 displayWords(cursor.limit(1));
15 cursor = wordsColl.find({first:'p'}, {word:1,size:1,stats:1});
16 print("Only the word, size and stats fields:");
17 displayWords(cursor.limit(1));
18 cursor = wordsColl.find({first:'p'}, {word:1,first:1,last:1});
19 print("Only the word, first and last fields:");
20 displayWords(cursor.limit(1));
21 cursor = wordsColl.find({first:'p'}, {charsets:false, stats:false});
22 print("Excluding charsets and stats:");
23 displayWords(cursor.limit(1));
Full Word:
{
  "_id": {
    "str": "52d87454483398c8f24292b7"
  },
  "word": "people",
  "first": "p",
  "last": "e",
  "size": 6,
  "letters": ["p", "e", "o", "l" ],
  "stats": {
    "vowels": 3,
    "consonants": 3
  },
  "charsets": [
    {
       "type": "consonants",
       "chars": [ "p", "l" ]
    },
    {
       "type": "vowels",
       "chars": [ "e", "o" ]
    }
  ]
}
Only the word field:
{
  "_id": {
    "str": "52d87454483398c8f24292b7"
  },
  "word": "people"
}
Only the word, size and stats fields:
{
  "_id": {
    "str": "52d87454483398c8f24292b7"
  },
  "word": "people",
  "size": 6,
  "stats": {
    "vowels": 3,
    "consonants": 3
  }
}
Only the word, first and last fields:
{
  "_id": {
    "str": "52d87454483398c8f24292b7"
  },
  "word": "people",
  "first": "p",
  "last": "e"
}
Excluding charsets and stats:
{
  "_id": {
    "str": "52d87454483398c8f24292b7"
  },
  "word": "people",
  "first": "p",
  "last": "e",
  "size": 6,
  "letters": [ "p", "e", "o", "l" ]
}