定义
语法
range 通过以下语法实现:
1 { 2 "$search": { 3 "index": <index name>, // optional, defaults to "default" 4 "range": { 5 "path": "<field-to-search>", 6 "gt | gte": <value-to-search>, 7 "lt | lte": <value-to-search>, 8 "score": <score-options>, 9 "doesNotAffect": "<facet-to-exclude>" | [<array-of-facets>] 10 } 11 } 12 }
选项
range 使用以下词条来构造查询:
字段 | 类型 | 说明 | 必要性 |
|---|---|---|---|
| BSON 日期、数字、字符串或 objectId | no | |
| BSON 日期、数字、字符串或 objectId | no | |
| 字符串或字符串数组 | 要搜索的一个或多个带索引字段。 | 是 |
| 对象 | 修改分配给匹配搜索结果的分数。您可以使用以下选项修改默认分数:
查询大量值时,如果大量中的更多值与查询匹配, MongoDB Search 会分配更高的分数。 有关在查询中使用 | no |
| 字符串或字符串数组 | 要从基于此查询的计数重新计算中排除的分面(Facet)或分面(Facet)列表。值必须是 | no |
示例
The following examples use the collection in the sample data. If you loaded the sample data on your cluster, you can create the indexes using the index definitions in the examples below and run the example queries on your cluster.
数字示例
The following examples use indexes on numeric fields in the sample data and run range queries against the indexed fields.
对于本示例,您可以使用静态或动态映射为 sample_mflix.movies 集合中的 runtime 字段建立索引。该查询使用 gte 和 lte 字段来定义要搜索的数字范围。
样本索引
下列名为 default 的索引定义为 movies 集合中的所有字段编制索引。
1 { 2 "mappings": { 3 "dynamic": true 4 } 5 }
下列名为 default 的索引定义仅对 movies 集合中的 runtime 字段编制索引。
1 { 2 "mappings": { 3 "dynamic": false, 4 "fields": { 5 "runtime": { 6 "type": "number" 7 } 8 } 9 } 10 }
样本查询
以下查询搜索运行时间大于或等于 2 且小于或等于 3 的电影。它包括 $limit 阶段,用于将输出限制为 5 个结果,以及 $project 阶段,用于排除 title 和 runtime 之外的所有字段。
1 db.movies.aggregate([ 2 { 3 "$search": { 4 "range": { 5 "path": "runtime", 6 "gte": 2, 7 "lte": 3 8 } 9 } 10 }, 11 { 12 "$limit": 5 13 }, 14 { 15 "$project": { 16 "_id": 0, 17 "title": 1, 18 "runtime": 1 19 } 20 } 21 ])
{ "runtime" : 3, "title" : "Dots" } { "runtime" : 3, "title" : "Sisyphus" } { "runtime" : 3, "title" : "The Fly" } { "runtime" : 2, "title" : "Andrè and Wally B." } { "runtime" : 2, "title" : "Luxo Jr." }
对于本示例,您可以使用静态或动态映射为 sample_mflix.movies 集合中的 runtime 字段建立索引。该查询使用 lte 字段,搜索所有小于或等于给定值的值。
样本索引
以下名为default的索引定义为movies集合中的runtime字段编制索引。
1 { 2 "mappings": { 3 "dynamic": true 4 } 5 }
1 { 2 "mappings": { 3 "dynamic": false, 4 "fields": { 5 "runtime": { 6 "type": "number" 7 } 8 } 9 } 10 }
样本查询
以下查询搜索运行时间小于或等于2的所有电影。 它包括一个用于将输出限制为5结果的$limit阶段和用于执行以下操作的$project阶段:
排除
title和runtime之外的所有字段添加字段
score
1 db.movies.aggregate([ 2 { 3 "$search": { 4 "range": { 5 "path": "runtime", 6 "lte": 2 7 } 8 } 9 }, 10 { 11 "$limit": 5 12 }, 13 { 14 "$project": { 15 "_id": 0, 16 "title": 1, 17 "runtime": 1, 18 score: { $meta: "searchScore" } 19 } 20 } 21 ])
{ "runtime" : 1, "title" : "Blacksmith Scene", "score" : 1 } { "runtime" : 2, "title" : "Andrè and Wally B.", "score" : 1 } { "runtime" : 2, "title" : "Luxo Jr.", "score" : 1 } { "runtime" : 1, "title" : "The Kiss", "score" : 1 } { "runtime" : 1, "title" : "Dickson Experimental Sound Film", "score" : 1 }
以下示例为sample_analytics.customers集合中所有动态可索引字段(包括具有数值数组的字段)动态编制索引。 示例查询使用 gte 和 lte 字段在Atlas Search中查找数值数组中给定值之间的所有值。
样本索引
以下名为default的索引定义动态索引所有字段,包括accounts字段,它是一个数值数组。
{ "mappings": { "dynamic": true } }
样本查询
以下查询搜索250000和300000之间的客户帐户。 它包括一个$limit阶段,用于将输出限制为5结果;以及一个$project阶段,用于排除除accounts和name之外的所有字段。
1 db.customers.aggregate([ 2 { 3 "$search": { 4 "range": { 5 "path": "accounts", 6 "gt": 250000, 7 "lt": 400000 8 } 9 } 10 }, 11 { 12 "$limit": 5 13 }, 14 { 15 "$project": { 16 "_id": 0, 17 "accounts": 1, 18 "name": 1 19 } 20 } 21 ])
1 [ 2 { 3 name: 'Elizabeth Ray', 4 accounts: [ 371138, 324287, 276528, 332179, 422649, 387979 ] 5 }, 6 { 7 name: 'Katherine David', 8 accounts: [ 462501, 228290, 968786, 515844, 377292 ] 9 }, 10 { 11 name: 'Brad Cardenas', 12 accounts: [ 721914, 817222, 973067, 260799, 87389 ] 13 }, 14 { 15 name: 'Gary Nichols', 16 accounts: [ 385397, 337979, 325377, 440243, 586395, 86702 ] 17 }, 18 { name: 'Jennifer Lawrence', accounts: [ 344885, 839927, 853542 ] } 19 ]
日期示例
以下示例使用 range 运算符查询sample_mflix.movies 集合中的日期字段。对于本示例,您可以使用静态或动态映射为集合中名为 released 的 date 类型字段建立索引。
样本索引
以下名为 default 的索引定义为 movies 集合中的所有动态可索引字段编制索引,包括 date 类型的 released 字段。
{ "mappings": { "dynamic": true } }
以下名为default的索引定义为movies集合中的released字段编制索引:
{ "mappings": { "dynamic": false, "fields": { "released": { "type": "date" } } } }
样本查询
以下查询搜索 2010 年 1 月 1 日至 2015年 1 月 1 日之间上映的电影。它包括一个 $limit 阶段,用于将输出限制为 5 个结果,以及一个 $project 阶段,用于排除 title 和 released 之外的所有字段。
1 db.movies.aggregate([ 2 { 3 "$search": { 4 "range": { 5 "path": "released", 6 "gt": ISODate("2010-01-01T00:00:00.000Z"), 7 "lt": ISODate("2015-01-01T00:00:00.000Z") 8 } 9 } 10 }, 11 { 12 "$limit": 5 13 }, 14 { 15 "$project": { 16 "_id": 0, 17 "title": 1, 18 "released": 1 19 } 20 } 21 ])
1 [ 2 { 3 title: 'Too Much Johnson', 4 released: ISODate('2014-08-30T00:00:00.000Z') 5 }, 6 { 7 title: 'Stolen Desire', 8 released: ISODate('2012-07-01T00:00:00.000Z') 9 }, 10 { 11 title: 'The Monkey King', 12 released: ISODate('2012-01-12T00:00:00.000Z') 13 }, 14 { title: 'The Land', released: ISODate('2012-08-04T00:00:00.000Z') }, 15 { 16 title: 'My Childhood', 17 released: ISODate('2013-07-31T00:00:00.000Z') 18 } 19 ]
ObjectId 示例
以下示例使用 range 操作符查询 sample_mflix.movies 集合中的 objectId 字段。对于本示例,您可以使用静态或动态映射为集合中名为 _id 的 objectId 类型字段建立索引。
样本索引
下列名为 default 的索引定义为 movies 集合中的所有字段编制索引。
以下名为 default 的索引定义为 movies 集合中的所有动态可索引字段编制索引,包括 objectId 类型的 _id 字段。
{ "mappings": { "dynamic": true } }
以下名为default的索引定义为movies集合中的_id字段编制索引:
{ "mappings": { "dynamic": false, "fields": { "_id": { "type": "objectId" } } } }
样本查询
以下示例使用 range 操作符在 _id 字段中查询 sample_mflix.movies 集合中的一系列 objectId 值。
1 db.movies.aggregate([ 2 { 3 "$search": { 4 "range": { 5 "path": "_id", 6 "gte": ObjectId("573a1396f29313caabce4a9a"), 7 "lte": ObjectId('573a1396f29313caabce4ae7') 8 } 9 } 10 }, 11 { 12 "$project": { 13 "_id": 1, 14 "title": 1, 15 "released": 1 16 } 17 } 18 ])
1 [ 2 { 3 _id: ObjectId('573a1396f29313caabce4a9a'), 4 title: 'The Godfather', 5 released: ISODate('1972-03-24T00:00:00.000Z') 6 }, 7 { 8 _id: ObjectId('573a1396f29313caabce4a9b'), 9 title: 'Get to Know Your Rabbit', 10 released: ISODate('1972-06-01T00:00:00.000Z') 11 }, 12 { 13 _id: ObjectId('573a1396f29313caabce4aad'), 14 title: 'The Tall Blond Man with One Black Shoe', 15 released: ISODate('1973-08-30T00:00:00.000Z') 16 }, 17 { 18 _id: ObjectId('573a1396f29313caabce4abe'), 19 title: 'The Great Northfield Minnesota Raid', 20 released: ISODate('1972-05-12T00:00:00.000Z') 21 }, 22 { 23 _id: ObjectId('573a1396f29313caabce4ac4'), 24 title: 'The Heartbreak Kid', 25 released: ISODate('1973-02-01T00:00:00.000Z') 26 }, 27 { 28 _id: ObjectId('573a1396f29313caabce4ac7'), 29 title: 'Gumshoe', 30 released: ISODate('1971-12-01T00:00:00.000Z') 31 }, 32 { 33 _id: ObjectId('573a1396f29313caabce4ad9'), 34 title: 'Heat', 35 released: ISODate('1972-10-06T00:00:00.000Z') 36 }, 37 { 38 _id: ObjectId('573a1396f29313caabce4ae7'), 39 title: 'Horror Express', 40 released: ISODate('1973-12-01T00:00:00.000Z') 41 } 42 ]
字符串示例
以下示例使用 range操作符查询sample_mflix.movies集合中的字符串字段。 对于本示例,您必须使用静态映射将集合中名为 title 的字段索引为MongoDB Search token 类型。
样本索引
以下名为 default 的索引定义将 movies集合中的 title字段索引为MongoDB Search token 类型:
{ "mappings": { "dynamic": false, "fields": { "title": { "type": "token", "normalizer": "lowercase" } } } }
样本查询
以下查询搜索包含 city 和 country 之间的字符的电影标题。它包括一个 $limit 阶段,用于将输出限制为 5 个结果,以及一个 $project 阶段,用于排除除 title 之外的所有字段。
1 db.movies.aggregate([ 2 { 3 "$search": { 4 "range": { 5 "path": "title", 6 "gt": "city", 7 "lt": "country" 8 } 9 } 10 }, 11 { 12 "$limit": 5 13 }, 14 { 15 "$project": { 16 "_id": 0, 17 "title": 1 18 } 19 } 20 ])
[ { title: 'Civilization' }, { title: 'Clash of the Wolves' }, { title: 'City Lights' }, { title: 'Comradeship' }, { title: 'Come and Get It' } ]
嵌入式文档示例
以下查询在 sample_training.companies 集合的 funding_rounds.raised_amount 字段中搜索大于或等于 500000 且小于或等于 1000000 的金额。使用 returnStoredSource: true 和 returnScope 检索并返回 funding_rounds 对象数组中的字段,这些字段存储在 mongot 上。该查询使用对存储源使用相对路径示例中的示例索引。
➤ 在 Search Playground 中尝试此功能。MongoDB Search Playground 中的结果可能会有所不同,因为 MongoDB Search Playground 中的数据比以下示例中使用的示例集合要小。
1 db.companies.aggregate([ 2 { 3 "$search": { 4 "range": { 5 "path": "funding_rounds.raised_amount", 6 "gte": 500000, 7 "lte": 1000000 8 }, 9 "returnStoredSource": true, 10 "returnScope": { 11 "path": "funding_rounds" 12 } 13 } 14 }, 15 { 16 "$limit": 5 17 } 18 ])
[ { round_code: 'angel', raised_amount: 500000, raised_currency_code: 'USD' }, { round_code: 'angel', raised_amount: 800000, raised_currency_code: 'USD' }, { round_code: 'c', raised_amount: 1000000, raised_currency_code: 'USD' }, { round_code: 'seed', raised_amount: 1000000, raised_currency_code: 'USD' }, { round_code: 'a', raised_amount: 1000000, raised_currency_code: 'USD' } ]
元数据示例
以下查询搜索运行时间大于或等于 2 且小于或等于 3 的电影。该查询使用$searchMeta阶段检索符合搜索条件的以下时间段(年)中的所有电影:
1990,包括此存储桶的下限
2000,1990 存储桶的不含上限以及此存储桶的包含下限
2010,2000 存储桶的不含上限以及此存储桶的包含下限
2020,2010 存储桶的独占上限
该查询使用名为 default 的索引,该索引会对 sample_mflix.movies 命名空间中的所有字段进行动态索引。
db.movies.aggregate([ { "$searchMeta": { "facet": { "operator": { "range": { "path": "runtime", "gte": 2, "lte": 3 } }, "facets": { "yearFacet": { "type": "number", "path": "year", "boundaries": [1990, 2000, 2010, 2020 ] } } } } } ])
[ { count: { lowerBound: Long('15') }, facet: { yearFacet: { buckets: [ { _id: 1990, count: Long('2') }, { _id: 2000, count: Long('3') }, { _id: 2010, count: Long('5') } ] } } } ]