13-根据子文档数组中的字段查找文档
6.4.8 根据子文档数组中的字段查找文档
使用MongoDB shell查找特定的文档 在本节中,您将编写一个MongoDB shell脚本,在其中使用前面讨论的查询运算符来检索特定的文档。您应该熟悉前面介绍的大多数查询运算符。 在程序清单6.5中,第1~11行实现了一个简单的JavaScript显示函数,用于以方便查看的方式显示结果。 请执行如下步骤,编写这个在数据库words中查找特定文档的脚本。 1.确保启动了MongoDB服务器。 2.确保运行了生成数据库words的脚本文件code/hour05/generate_words.js。 3.在文件夹code/hour06中新建一个文件,并将其命名为find_specific.js。 4.在这个文件中输入程序清单程序清单6.5所示的代码。这些代码连接到数据库words,获取表示集合word_stats的Collection对象,并使用find()和各种查询运算符来检索并显示一系列单词。 5.将这个文件存盘。 6.打开一个控制台窗口,并切换到目录code/hour06。 7.执行下面的命令来运行这个脚本,它将使用各种查询运算符来查询集合word_stats以访问特定的文档。程序清单6.6显示了这个脚本的输出。 程序清单6.5 find_specific.js:使用MongoDB shell查找并访问集合中的特定文档 程序清单6.6 find_specific.js-output:使用MongoDB shell查找并访问集合中特定文档的输出 ▲
在MongoDB文档模型中,一种比较棘手的查询是,根据数组字段中的子文档来查找文档。在这种情况下,文档包含一个子文档数组,而您要根据子文档中的字段来查询文档。
要根据数组字段中的子文档进行查询,可使用运算符$elemMatch。这个运算符让您能够根据数组中的子文档进行查询。
为演示运算符$elemMatch,最简单的方式是使用数据库words中的子文档数组字段charsets。字段charsets是一个文档数组,其结构如下:
{
type: <string>,
chars: <array>
}
下面的查询匹配这样的文档,即其字段charsets包含这样的文档:字段type为other,而数组字段chars的长度为2:
{charsets:{$elemMatch: {$and: [{type: 'other'},{chars: {$size: 2}}] } }}
▼ Try It Yourself
mongo find_specific.js
01 function displayWords(msg, cursor, pretty){
02 print("\n"+msg);
03 words = cursor.map(function(word){
04 return word.word;
05 });
06 wordStr = JSON.stringify(words);
07 if (wordStr.length > 65){
08 wordStr = wordStr.slice(0, 50) + "...";
09 }
10 print(wordStr);
11 }
12 mongo = new Mongo("localhost");
13 wordsDB = mongo.getDB("words");
14 wordsColl = wordsDB.getCollection("word_stats");
15 cursor = wordsColl.find({first: {$in: ['a', 'b', 'c'] } });
16 displayWords("Words starting with a, b or c: ", cursor);
17 cursor = wordsColl.find({size:{$gt: 12}});
18 displayWords("Words longer than 12 characters: ", cursor);
19 cursor = wordsColl.find({size:{$mod: [2,0] } });
20 displayWords("Words with even Lengths: ", cursor);
21 cursor = wordsColl.find({letters:{$size: 12}});
22 displayWords("Words with 12 Distinct characters: ", cursor);
23 cursor = wordsColl.find({$and:
24 [{first:{
25 $in: ['a', 'e', 'i', 'o', 'o'] } },
26 {last:{
27 $in: ['a', 'e', 'i', 'o', 'o'] } }]});
28 displayWords("Words that start and end with a vowel: ", cursor);
29 cursor = wordsColl.find({"stats.vowels":{$gt: 6}});
30 displayWords("Words containing 7 or more vowels: ", cursor);
31 cursor = wordsColl.find({letters:{$all: ['a','e','i','o','u'] } });
32 displayWords("Words with all 5 vowels: ", cursor);
33 cursor = wordsColl.find({otherChars: {$exists: true}});
34 displayWords("Words with non-alphabet characters: ", cursor);
35 cursor = wordsColl.find({charsets:{
36 $elemMatch:{
37 $and:[{type: 'other'},
38 {chars: {$size: 2}}] } }});
39 displayWords("Words with 2 non-alphabet characters: ", cursor);
Words starting with a, b or c:
["be","and","a","can't","at","but","by","as","can"...
Words longer than 12 characters:
["international","administration","environmental",...
Words with even Lengths:
["be","of","in","to","have","to","it","that","he",...
Words with 12 Distinct characters:
["uncomfortable","accomplishment","considerably"]
Words that start and end with a vowel:
["a","i","one","into","also","one","area","eye","i...
Words containing 7 or more vowels:
["identification","questionnaire","organizational"...
Words with all 5 vowels:
["education","educational","regulation","evaluatio...
Words with non-alphabet characters:
["don't","won't","can't","shouldn't","e-mail","lon...
Words with 2 non-alphabet characters:
["two-third's","middle-class'"]