For AI agents: a documentation index is available at https://www.mongodb.com/zh-cn/docs/llms.txt — markdown versions of all pages are available by appending .md to any URL path.
Make the MongoDB docs better! We value your opinion. Share your feedback for a chance to win $100.
MongoDB Branding Shape
Click here >
Docs 菜单

span (MongoDB搜索操作符)

注意

span操作符已弃用。请改用短语

span

span 操作符查找文本字段区域内的Atlas Search匹配文本。 您可以使用它以指定的精度查找彼此接近的字符串。 与其他操作符相比, span操作符的计算量更大,因为查询必须跟踪位置信息。

span 是术语级操作符,这意味着不分析query字段。术语级操作符适用于关键字分析器,因为query字段被视为包含特殊字符的单个术语。

span 查询不按分数排名。

span 通过以下语法实现:

{
$search: {
"index": <index name>, // optional, defaults to "default"
"span": {
"term" | <positional-operator>": {
<operator-specification>
}
}
}
}

注意

span 搜索查询不能使用复合操作符。

You can use the term operator to specify the terms to search. The term operator is required and when you use it with span positional operators, it must be the innermost child of the positional operators.

term操作符的语法如下:

"term": {
"path": "<path-to-field>",
"query": "<terms-to-search>"
}

term操作符采用以下字段:

选项
类型
必需?
说明

path

字符串

要搜索的索引字段。

query

字符串

Atlas Search的术语或短语。

You can use the positional operators to specify the position of the terms that you want to search with the term operator. The positional operators are of type document. You must specify at least one positional operator in your span operator query. The positional operators can take other span positional operators, recursively.

注意

关于示例

The examples on this page use the sample_mflix.movies collection. If you load the sample data and create a dynamic index named default on the movies collection , you can run the following $search sample queries against the collection. The sample queries use the $limit stage to limit the results to 5 documents and the $project stage to exclude all fields except the title field in the results.

span 采用以下可选位置操作符。

The contains positional operator matches terms that are contained within other terms. You can use positional operators recursively or just the term operator within contains to specify the search terms.

contains位置操作符采用以下语法:

{
"$search": {
"span": {
"contains": {
"spanToReturn": "inner"|"outer",
"little": { <positional-or-term-operator-specification> },
"big": { <positional-or-term-operator-specification> }
}
}
}
}

contains位置操作符采用以下字段:

字段
类型
必需?
说明

big

文档

以递归方式指定的一个或多个位置操作符或仅指定术语操作符。下表显示了 span 根据 spanToReturn 的值对 big 执行的查询类型。

outer

span 匹配来自 big 的术语,并且这些术语至少包含一个来自 little 的术语。

inner

span 匹配来自 little 的术语,这些术语位于来自 big 的术语之内

little

文档

以递归方式指定的一个或多个位置操作符或仅指定术语操作符。下表显示了 span 根据 spanToReturn 的值对 little 执行的查询类型。

inner

span 匹配来自 little 的术语,这些术语位于来自 big 的术语之内

outer

span 匹配来自 big 的术语,并且这些术语至少包含一个来自 little 的术语。

score

文档

no

要应用于此Atlas Search结果的分数。

spanToReturn

字符串

要执行的查询类型和要返回的匹配结果。 值可以是以下之一:

  • inner - 执行内部查询,匹配位于big术语内部的little术语

  • outer - 执行包含查询,以匹配来自big的术语,而这些术语至少包含一个来自little的术语

以下示例查询使用span.contains查找词语train与词语greatrobbery一起出现的文档,其中greatrobbery 5 title字段。

1db.movies.aggregate([
2 {
3 "$search": {
4 "span": {
5 "contains": {
6 "spanToReturn": "outer",
7 "little": {
8 "term": {
9 "path": "title",
10 "query": "train"
11 }
12 },
13 "big": {
14 "near": {
15 "clauses": [
16 {
17 "term": {
18 "path": "title",
19 "query": "great"
20 }
21 },
22 {
23 "term": {
24 "path": "title",
25 "query": "robbery"
26 }
27 }
28 ],
29 "slop": 5
30 }
31 }
32 }
33 }
34 }
35 },
36 {
37 "$limit": 5
38 },
39 {
40 "$project": {
41 "_id": 0,
42 "title": 1
43 }
44 }
45])
[
{ title: 'The Great Train Robbery' },
{ title: 'The Great Train Robbery' },
{ title: "The Great St. Trinian's Train Robbery" }
]

MongoDB Search 返回在词语 greatrobbery(使用 big 指定)内包含术语train(使用 little 指定)的文档。如果您将第 6 行上的 spanToReturn 设立为 inner, MongoDB搜索将返回相同的文档,因为术语 train(使用 little 指定)出现在词语 greatrobbery(使用 big之内

The first positional operator identifies the position of the search term by using a specified number. You can specify the search terms using positional operators recursively, or just the term operator. span matches documents where the position of the search term is less than or equal to the specified number.

first位置操作符采用以下语法:

{
"$search": {
"span": {
"first": {
"endPositionLte": <term-position>,
"operator": { <span-positional-or-term-operator-specification> },
"score": { <score-options> }
}
}
}
}

first位置操作符采用以下字段:

选项
类型
必需?
说明

endPositionLte

int

no

指定Atlas Search词语位置的数字。 如果为多个词语指定Atlas Search ,则最后一个词语应小于或等于此值。 如果省略,则默认为3

operator

文档

包含位置操作符或术语操作符选项的文档。

score

文档

no

要应用于此Atlas Search结果的分数。

以下示例查询使用 span.first 来查找 title 字段中出现指定string的文档。 endPositionLte 参数的值为 2,这意味着使用 term 操作符指定的Atlas Search词语必须是该字段中的第一个或第二个单词。

1db.movies.aggregate([
2 {
3 "$search": {
4 "span": {
5 "first": {
6 "endPositionLte": 2,
7 "operator": {
8 "term": {
9 "path": "title",
10 "query": "dance"
11 }
12 }
13 }
14 }
15 }
16 },
17 {
18 "$limit": 5
19 },
20 {
21 "$project": {
22 "_id": 0,
23 "title": 1
24 }
25 }
26])
[
{ title: 'Dance Program' },
{ title: 'Slam Dance' },
{ title: 'Last Dance' },
{ title: 'War Dance' },
{ title: 'Delhi Dance' }
]

MongoDB Search 返回在 title字段的第一个或第二个位置包含搜索词 dance 的文档。

1db.movies.aggregate([
2 {
3 "$search": {
4 "span": {
5 "first": {
6 "endPositionLte": 2,
7 "operator": {
8 "or": {
9 "clauses": [
10 { "term": { "path": "title", "query": "man" } },
11 { "term": { "path": "title", "query": "woman" } }
12 ]
13 }
14 }
15 }
16 }
17 }
18 },
19 {
20 "$limit": 5
21 },
22 {
23 "$project": {
24 "_id": 0,
25 "title": 1
26 }
27 }
28])
[
{ title: "Everybody's Woman" },
{ title: 'Marked Woman' },
{ title: 'Wonder Man' },
{ title: 'Designing Woman' },
{ title: 'Watermelon Man' }
]

MongoDB搜索返回在 title字段的第一个或第二个位置包含搜索词 manwoman 的文档。MongoDB搜索不会在同一 title 中返回两个搜索词,因为该示例包含 or操作符clauses 来指定搜索词。

The near positional operator matches two or more clauses that contain the search term near each other. You can specify the search terms using a list of positional operators recursively or just the term operator.

near位置操作符采用以下语法:

{
"$search": {
"span": {
"near": {
"clauses": [
{ <span-positional-or-term-operator-specification> },
...
],
"slop": <distance-number>,
"inOrder": true|false
}
}
}
}

near位置操作符采用以下字段:

字段
类型
必需?
说明

clauses

文档数组

必须彼此靠近的 Span 子句。 子句不能为空。 每个文档包含span 位置或仅术语操作符选项。

inOrder

布尔

no

该标志指定Atlas Search是否必须按指定的顺序搜索子句中的术语并且不能重叠。

值可以是以下值之一:

  • true — Atlas Search按指定顺序搜索子句中的术语,而不会重叠

  • false - Atlas Search以任意顺序搜索子句中的术语

如果省略,则默认值为 false

score

文档

no

要应用于此Atlas Search结果的分数。

slop

整型

no

子句中术语之间的允许距离。 较低的值允许词语之间的位置距离较小,较大的值允许词语之间的距离较大,以满足查询要求。 默认值为0 ,这意味着不同子句中的单词必须相邻才能被视为匹配。

以下示例查询使用 span.near Atlas Search字符串 princepauper 彼此接近的文档。 inOrder 参数设置为 false,因此Atlas Search词语可以按任意顺序排列。 slop 参数设置为 4,因此Atlas Search术语最多只能用 4 个单词分隔。

1db.movies.aggregate([
2 {
3 "$search" : {
4 "span": {
5 "near": {
6 "clauses": [
7 { "term": { "path": "title", "query": "prince" } },
8 { "term": { "path": "title", "query": "pauper" } }
9 ],
10 "slop": 4,
11 "inOrder": false
12 }
13 }
14 }
15 },
16 {
17 "$limit": 5
18 },
19 {
20 "$project": {
21 "_id": 0,
22 "title": 1
23 }
24 }
25])
[ { title: 'The Prince and the Pauper' } ]

MongoDB Search 在 title字段中返回包含搜索词 princepauper 的文档,这些词之间的间隔少于四个单词。

The or positional operator matches any of two or more clauses. You can specify the search terms using a list of positional operators recursively or just the term operator.

or位置操作符采用以下语法:

{
"$search": {
"span": {
"or": {
"clauses": [
{ <span-positional-or-term-operator-specification> },
...
],
"score": { <scoring-options> }
}
}
}
}

or位置操作符采用以下字段:

选项
类型
必需?
说明

clauses

文档数组

指定搜索词的 Span 子句。 其中一个子句必须匹配,并且子句不能为空。 每个文档必须包含以递归方式指定的span 位置操作符或仅包含术语操作符选项。

score

文档

no

要应用于此Atlas Search结果的分数。

The following example query uses span.or clauses to specify two term operator queries that search for documents in which the title field has either city or country.

1db.movies.aggregate([
2 {
3 "$search" : {
4 "span": {
5 "or": {
6 "clauses": [
7 { "term": { "path": "title", "query": "city" } },
8 { "term": { "path": "title", "query": "country" } }
9 ],
10 }
11 }
12 }
13 },
14 {
15 "$limit": 5
16 },
17 {
18 "$project": {
19 "_id": 0,
20 "title": 1
21 }
22 }
23])
[
{ title: 'Country' },
{ title: 'City Lights' },
{ title: 'King & Country' },
{ title: 'Fat City' },
{ title: 'Atlantic City' }
]

MongoDB Search 返回 title字段中包含搜索词 citycountry 的文档,但不会同时包含在同一 title 中。

The subtract positional operator removes matches that overlap with another match. You can specify the search terms using a list of positional operators recursively or just the term operator. The subtract clause can be used to exclude certain strings from your search results.

subtract位置操作符采用以下语法:

{
"$search": {
"span": {
"subtract": {
"include": { <span-positional-or-term-operator-specification> },
"exclude": { <span-positional-or-term-operator-specification> }
}
}
}
}

subtract位置操作符采用以下字段:

选项
类型
必需?
说明

exclude

文档

指定术语或短语匹配项的文档,以删除与 include字段中指定的术语或短语匹配项的重叠部分。 您可以使用任何跨度位置操作符和术语操作符符来指定术语或短语。

include

文档

用于指定术语匹配项以包括使用任何位置操作符或仅使用术语操作符的文档。

score

文档

no

要应用于此Atlas Search结果的分数。

以下示例查询使用 span.subtract Atlas Search以下文档:title 字段包含单词 fatherson(顺序不限),且彼此相差不超过 3 个单词。 它排除在fatherson之间出现单词like的任何文档。

1db.movies.aggregate([
2 {
3 "$search" : {
4 "span": {
5 "subtract": {
6 "include": {
7 "near": {
8 "clauses": [
9 { "term": { "path": "title", "query": "father" } },
10 { "term": { "path": "title", "query": "son" } }
11 ],
12 "inOrder": false,
13 "slop": 3
14 }
15 },
16 "exclude": { "term": { "path": "title", "query": "like" } }
17 }
18 }
19 }
20 },
21 {
22 "$limit": 5
23 },
24 {
25 "$project": {
26 "_id": 0,
27 "title": 1
28 }
29 }
30])
[
{ title: 'Father, Son & Holy Cow' },
{ title: 'My Father and My Son' },
{ title: 'Jimmy Rosenberg: The Father, the Son & the Talent' }
]

MongoDB Search 不会返回标题为 Like Father Like Son 的文档,因为尽管 title字段包含单词 fatherson,但它们之间还有 like,即 exclude 条件中的查询。