find( 函数用于在集合中搜索文档,类似于 SQL 数据库中的 SELECT 语句。它接受一个搜索条件字符串 (SearchConditionStr) 作为参数,用于指定应从数据库返回的文档。SearchConditionStr)execute() 函数触发 find() 操作的实际执行。
SearchConditionStr 可以采用以下形式之一
-
如果未指定 SearchConditionStr,则
find()操作将返回集合中的所有文档。// Get a collection var myColl = session.getSchema("world_x").getCollection("countryinfo"); // To return all documents in world_x: myColl.find().execute(); -
SearchConditionStr 最常见的形式是
JSON-path [ operator { value | JSON-path} ]以下是 SearchConditionStr 不同部分的一些解释
-
JSON-path:JSON 路径标识 JSON 文档中的元素;有关详细信息,请参阅 JSON 路径语法。以下是 JSON 路径语法的简要概述-
JSON 路径以范围开头:在 MySQL 的 JSON 文档实现中,路径的范围始终是被操作的文档,表示为
$,这始终是隐式假定的,因此在大多数情况下可以省略;例如,路径$.geography.Region等效于geography.Region。注意在某些情况下,
$不能省略;例如当使用
**通配符时(例如,find("$**.b1");请参阅下文关于通配符的讨论),如果省略了
$,则 JSON 路径仅包含一个文字字符串(例如,find("$.'country_name'")用于查找所有具有country name字段的文档)。
在范围之后,路径由一个或多个路径段组成。路径段从 JSON 树的一层向下到下一层,并且连续的路径用句点 (
.) 分隔。例如:myColl.find("geography.Continent = 'Africa'")查找geography字段下的Continent字段的值为Africa的所有文档。-
数组中的元素由
[表示,其中N]N是数组索引,它必须是非负整数。myColl.add({ name:'John', favorNums: [1, 3, 5, 7, 9] }).execute(); myColl.find("favorNums[0] = 1").execute(); //Returns the document just added }
-
-
通配符
*和**可以在 JSON 路径中使用,如下所示表示成员object.*object下所有成员的值。例如,在示例world_x模式中的countryinfo集合中,geography.*表示geography对象下的所有成员,myColl.find("'Africa' in geography.*")返回在geography下的任何成员中具有值Africa的所有文档。-
表示数组中所有元素的值。例如array[*]myColl.add({ name:'John', favorNums: [1, 3, 5, 7, 9] }).execute(); myColl.add({ name:'Jane', favorNums: [2, 4, 6, 8, 10] }).execute(); myColl.find("1 in favorNums[*]").execute(); //Returns the first document added above myColl.find("2 in favorNums[*]").execute(); //Returns the second document added above } -
[表示文档prefix]**suffixprefix下以suffix结尾的所有路径,无论路径的深度如何。以下示例说明了如何使用 ** 返回不同的结果mysql-js> myColl.find().execute(); { "a": "bar", "b": { "b1": 6, "b2": 7, "b3": { "b1": 99, "b2": 98, "b3": { "b1": 999, "b2": 998 } } }, "_id": "000061313aa10000000000000001" } { "a": "baz", "b": { "b1": 1, "b2": 7 }, "_id": "000061313aa10000000000000002" } { "a": "bbr", "c": 37, "_id": "0000613247ed0000000000000001" } 3 documents in set (0.0007 sec) mysql-js> myColl.find("$**.b2").execute(); { "a": "bar", "b": { "b1": 6, "b2": 7, "b3": { "b1": 99, "b2": 98, "b3": { "b1": 999, "b2": 998 } } }, "_id": "000061313aa10000000000000001" } { "a": "baz", "b": { "b1": 1, "b2": 7 }, "_id": "000061313aa10000000000000002" } 2 documents in set, 1 warning (0.0008 sec) ... mysql-js> myColl.find("$**.b3**.b2").execute(); { "a": "bar", "b": { "b1": 6, "b2": 7, "b3": { "b1": 99, "b2": 98, "b3": { "b1": 999, "b2": 998 } } }, "_id": "000061313aa10000000000000001" } 1 document in set, 1 warning (0.0011 sec) ...使用
**通配符时,适用以下要求prefix应为$或本身为文档的元素。suffix应为路径段,并且始终是必需的(也就是说,路径表达式不能以**结尾)。路径表达式不能包含序列
***。
-
value是要与JSON-path上的元素进行比较的值。与 MySQLWHERE子句一样,%和_通配符可以在value中与LIKE运算符一起使用。例如myColl.find("Name LIKE 'Austra%'") myColl.find("geography.Continent LIKE 'Asi_'") -
operator:以下运算符可以在 SearchConditionStr 中使用:OR (||)、AND (&&)、XOR、IS、NOT、BETWEEN、IN、LIKE、OVERLAPS、!=、<>、>、>=、<、<=、&、|、<<、>>、+、-、*、/、~和%。以下是一些使用运算符的示例myColl.find("Name = 'Australia'") myColl.find("demographics.Population >= 1000000" ) myColl.find("demographics.LifeExpectancy BETWEEN 50 AND 60") myColl.find("government.HeadOfState = 'Elizabeth II' AND geography.Region = 'Caribbean'")如果没有提供运算符和后续 JSON 路径,则
find()将返回提供的 JSON 路径指向某些非空元素的所有文档。例如myColl.find("demographics.Population" ).execute();返回所有具有
demographics.Population元素的文档{ "GNP": 828, "_id": "00005de917d80000000000000000", "Code": "ABW", "Name": "Aruba", "IndepYear": null, "geography": { "Region": "Caribbean", "Continent": "North America", "SurfaceArea": 193 }, "government": { "HeadOfState": "Beatrix", "GovernmentForm": "Nonmetropolitan Territory of The Netherlands" }, "demographics": { "Population": 103000, "LifeExpectancy": 78.4000015258789 } } { "GNP": 5976, "_id": "00005de917d80000000000000001", ... 232 documents in set, 1 warning (0.0013 sec) Warning (code 3986): Evaluating a JSON value in SQL boolean context does an implicit comparison against JSON integer 0; if this is not what you want, consider converting JSON to an SQL numeric type with JSON_VALUE RETURNING在 SearchConditionStr 中使用
IN运算符来检查通配符覆盖的所有成员中的值mysql-js> myColl.find("$**.b1").execute(); { "a": "bar", "b": { "b1": 6, "b2": 7, "b3": { "b1": 99, "b2": 98, "b3": { "b1": 999, "b2": 998 } } }, "_id": "000061313aa10000000000000001" } { "a": "baz", "b": { "b1": 1, "b2": 7 }, "_id": "000061313aa10000000000000002" } 2 documents in set, 1 warning (0.0012 sec) ... mysql-js> myColl.find("99 IN $**.b1").execute(); { "a": "bar", "b": { "b1": 6, "b2": 7, "b3": { "b1": 99, "b2": 98, "b3": { "b1": 999, "b2": 998 } } }, "_id": "000061313aa10000000000000001" } 1 document in set (0.0016 sec) ...OVERLAPS运算符比较两个 JSON 片段,如果这两个片段在任何键值对或数组元素中具有任何共同值,则返回 true (1)。例如mysql-js> myColl.find("list").execute(); { "_id": "1", "list": [ 1, 4 ] } { "_id": "2", "list": [ 4, 7 ] } 2 documents in set, 1 warning (0.0010 sec) mysql-js> myColl.find("[1,2,3] OVERLAPS $.list") { "_id": "1", "list": [ 1, 4 ] } 1 document in set (0.0006 sec)
-
可以将 fields()、sort() 和 limit() 等几种方法链接到 find() 函数以进一步细化结果。例如
myColl.find("Name LIKE 'Austra%'").fields("Code")
myColl.find("geography.Continent LIKE 'A%'").limit(10)
还支持使用 bind() 进行参数绑定。以下示例说明了如何将 bind() 与 find() 一起使用
// Use the collection 'my_collection'
var myColl = db.getCollection('my_collection');
// Find a single document that has a field 'name' that starts with 'L'
var docs = myColl.find('name like :param').
limit(1).bind('param', 'L%').execute();
print(docs.fetchOne());
// Get all documents with a field 'name' that starts with 'L'
docs = myColl.find('name like :param').
bind('param','L%').execute();
var myDoc;
while (myDoc = docs.fetchOne()) {
print(myDoc);
}
有关 EBNF 中 find() 语法的详细信息,另请参阅 CollectionFindFunction。