AI 에이전트의 경우: 문서 인덱스는 https://www.mongodb.com/ko-kr/docs/llms.txt에서 사용할 수 있으며, 모든 페이지의 마크다운 버전은 어떤 URL 경로에 .md를 추가하여 사용할 수 있습니다.
Docs Menu

queryString 연산자

queryString

queryString 연산자는 인덱싱된 필드와 값의 조합에 대한 쿼리를 지원합니다. queryString 연산자를 사용하여 string 필드에서 텍스트, 와일드카드, 정규 표현식, 퍼지 및 범위 검색을 수행할 수 있습니다.

queryString 의 구문은 다음과 같습니다:

1{
2 "$search": {
3 "index": <index name>, // optional, defaults to "default"
4 "queryString": {
5 "defaultPath": "<default-field-to-search>",
6 "query": "(<field-to-search>: (<search-values>) AND|OR (<search-values>)) AND|OR (<search-values>)"
7 }
8 }
9}

queryString 는 다음 용어를 사용하여 쿼리를 구성합니다:

필드
유형
설명
필요성

defaultPath

문자열

기본값 으로 검색 할 인덱싱된 필드 입니다. MongoDB Search는 query에서 검색 할 필드 생략한 경우에만 defaultPath 의 필드 를 검색합니다.

네

query

문자열

하나 이상의 인덱싱된 필드와 검색 대상 값입니다. 필드와 값은 콜론으로 구분됩니다. 예를 들어 plot 필드에서 baseball 문자열을 검색하려면 plot:baseball를 사용합니다. 여러 검색 기준을 결합하는 데 사용할 수 있는 연산자는 다음과 같습니다.

다음을 사용하여 필드와 값을 결합할 수 있습니다.

  • ANDAND 불리언 연산자를 나타냅니다. 문서가 결과에 포함되려면 모든 검색 값이 있어야 합니다.

  • OROR 불리언 연산자를 나타냅니다. 문서를 결과에 포함하려면 검색 값 중 하나 이상이 있어야 합니다.

  • NOTNOT 불리언 연산자를 나타냅니다. 문서가 결과에 포함되려면 지정된 검색 값이 없어야 합니다.

  • TO: 검색할 범위를 나타냅니다. 포함 범위에는 [], 배타적 범위에는 {}, 반개방 범위에는 {] 및 [}를 사용할 수 있습니다. 문서가 결과에 포함되려면 지정된 범위 내에 속하는 모든 값이 있어야 합니다.

  • ()하위 쿼리용 구분 기호입니다. 괄호를 사용하여 검색할 필드와 값을 그룹화할 수 있습니다.

다음을 사용하여 와일드카드 및 정규 표현식 쿼리를 실행할 수 있습니다.

  • ?: 일치시킬 단일 문자를 나타냅니다.

  • *: 일치시킬 문자가 0 개 이상임을 나타냅니다.

    queryString 연산자는 쿼리의 첫 번째 문자로 * 와일드카드 쿼리를 지원하지 않습니다. queryString 연산자는 * 앞의 모든 문자를 접두사 또는 정확히 일치해야 하는 문자로 취급합니다.

  • /정규 표현식의 구분자입니다.

  • ~: 지정된 편집 횟수까지 검색어와 유사한 문자열을 찾기 위한 퍼지 검색을 나타냅니다. 예를 들어 { "query":"foo":"stop"~2 }는 다음과 같습니다.

    { "defaultPath": "foo", "query": "stop", "maxEdits": 2 }

    queryString 연산자는 편집 2건 이하에 대해서만 퍼지 검색을 지원합니다. 거리가 멀면 결과가 사전의 여러 용어와 일치할 수 있으므로 유용하지 않습니다.

    ~ 을 문자열의 여러 용어와 함께 사용하는 경우 queryString 연산자는 지정된 수의 용어 범위 내에서 근접 검색을 수행합니다. 예를 들어 { "query":"foo":"New York"~2 }는 다음과 같습니다.

    { "defaultPath": "foo", "query": "New York", "slop": 2 }

네

score

객체

일치하는 검색 결과에 할당된 점수입니다. 다음 옵션을 사용하여 기본 점수를 수정할 수 있습니다.

  • boost결과 점수에 주어진 숫자를 곱합니다.

  • constant결과 점수를 주어진 숫자로 바꿉니다.

  • function결과 점수를 주어진 표현식으로 대체합니다.

배열 값을 쿼리할 때 MongoDB Search는 배열 에서 쿼리 와 일치하는 값이 더 많을수록 더 높은 점수를 할당합니다.

쿼리에서 score 사용에 대한 자세한 내용은 결과에서 문서 채점하기를 참조하십시오.

no

기본값으로 text 연산자는 bm25 유사성 알고리즘을 사용하여 쿼리와의 관련성에 따라 문서에 점수를 부여합니다.

MongoDB 검색 인덱스 정의의 string 필드에 있는 similarity.type 속성에서 다른 유사성 알고리즘을 지정하여 사용되는 유사성 알고리즘을 변경할 수 있습니다. string 유형의 MongoDB 검색 인덱스에 대한 내용은 String 필드를 인덱스하는 방법을 참조하세요.

지원되는 유사성 알고리즘에 대한 자세한 내용은 점수 세부 정보를 참조하세요.

The following examples use the movies collection in the sample_mflix database. If you have the sample dataset on your cluster , you can create the MongoDB Search index named default with dynamic mappings and run the example queries on your cluster.

The following example uses the queryString operator to query for movies with the title Rocky and either IV, 4, or Four at path title in the movies collection. In the following query, a field is specified in the defaultPath, but no fields are specified in the query. The query includes a $project stage to exclude all fields except title.

1db.movies.aggregate([
2 {
3 $search: {
4 "queryString": {
5 "defaultPath": "title",
6 "query": "Rocky AND (IV OR 4 OR Four)"
7 }
8 }
9 },
10 {
11 $project: {
12 "_id": 0,
13 "title": 1
14 }
15 }
16])

이 쿼리 의 경우 query에 필드가 없기 때문에 MongoDB Search는 defaultPath 에서 검색 수행합니다. 다음과 같은 결과를 반환합니다.

{ "title" : "Rocky IV" }

이 섹션의 예시에서는 queryString 연산자를 사용하여 captain, kirk, chess 용어가 포함된 영화 줄거리를 쿼리합니다. 이 예에서는 괄호를 사용하여 동일한 검색어를 서로 다르게 그룹화하여 검색 결과에 서로 다른 문서가 포함될 수 있는 방법을 보여줍니다.

The queries also includes a $project stage to:

  • title, plot, fullpath 이외의 모든 필드 제외

  • 다음과 같은 이름의 필드를 추가합니다. score

다음 쿼리는 plot 필드에서 chess과 captain 또는 kirk 중 하나를 검색합니다.

1db.movies.aggregate([
2 {
3 $search: {
4 "queryString": {
5 "defaultPath": "fullplot",
6 "query": "plot:(captain OR kirk) AND chess"
7 }
8 }
9 },
10 {
11 $project: {
12 "_id": 0,
13 "title": 1,
14 "plot": 1,
15 "fullplot": 1,
16 score: { $meta: "searchScore" }
17 }
18 }
19])
{
"fullplot" : "When the crew of the Enterprise is called back home, they find an unstoppable force of terror from within their own organization has detonated the fleet and everything it stands for, leaving our world in a state of crisis. With a personal score to settle, Captain Kirk leads a manhunt to a war-zone world to capture a one-man weapon of mass destruction. As our heroes are propelled into an epic chess game of life and death, love will be challenged, friendships will be torn apart, and sacrifices must be made for the only family Kirk has left: his crew.",
"plot" : "After the crew of the Enterprise find an unstoppable force of terror from within their own organization, Captain Kirk leads a manhunt to a war-zone world to capture a one-man weapon of mass destruction.",
"title" : "Star Trek Into Darkness",
"score" : 7.968792915344238
}

결과의 문서는 plot에 captain 및 kirk라는 용어가 포함되어 있지만 일치 기준을 충족하려면 하나만 필요하고 plot에는 없지만 chess이라는 용어가 fullplot인 defaultPath에 나타나므로 일치하는 문서입니다.

다음 예시에서는 queryString 연산자를 사용하여 문자 count부터 임의의 문자 (와일드카드 문자 *를 사용하여 정의)까지의 텍스트 값 범위를 알파벳 오름차순으로 영화 제목을 쿼리합니다.

1db.movies.aggregate([
2 {
3 "$search": {
4 "queryString": {
5 "defaultPath": "plot",
6 "query": "title:[count TO *]"
7 }
8 }
9 },
10 {
11 "$limit": 10
12 },
13 {
14 "$project": {
15 "_id": 0,
16 "title": 1
17 }
18 }
19])
1[
2 { title: 'Blacksmith Scene' },
3 { title: 'The Great Train Robbery' },
4 { title: 'The Land Beyond the Sunset' },
5 { title: 'A Corner in Wheat' },
6 { title: 'Winsor McCay, the Famous Cartoonist of the N.Y. Herald and His Moving Comics' },
7 { title: 'Traffic in Souls' },
8 { title: 'Gertie the Dinosaur' },
9 { title: 'In the Land of the Head Hunters' },
10 { title: 'The Perils of Pauline' },
11 { title: 'The Birth of a Nation' }
12]

쿼리 의 범위 문자 count에서 시작하지만 MongoDB Search 결과에는 제목이 Blacksmith Scene 및 A Corner in Wheat 인 영화가 포함됩니다. 기본값 분석기 lucene.standard를 사용하여 인덱스 하면 MongoDB Search는 title 필드 의 용어에 대해 별도의 토큰을 생성하고 쿼리 텀 개별 토큰과 일치시킵니다. 구체적으로 MongoDB Search는 다음 토큰을 생성하며, 그 중 하나 이상(√로 표시)이 쿼리 기준과 일치하는 토큰을 생성합니다.

제목
표준 분석기 토큰

블랙스미스 씬(Blacksmith Scene)

blacksmith, scene √

어 코어 인 윗트(A Corner in Wheat)

a, corner, in, wheat √

lucene.keyword 분석기 사용하여 인덱스 경우 MongoDB Search는 title 필드 의 전체 문자열에 대해 단일 토큰을 생성하므로 동일한 쿼리 에 대해 다음과 유사한 결과가 나타납니다.

[
{ title: 'è Nous la Libertè' },
{ title: 'tom thumb' },
{ title: 'è nos amours' },
{ title: 'èke och hans vèrld' },
{ title: 'èdipussi' },
{ title: 's/y Glèdjen' },
{ title: 'èAy, Carmela!' },
{ title: 'èlisa' },
{ title: 'èxtasis' },
{ title: 'eXistenZ' }
]

The following examples use the queryString operator to query for movie titles using fuzzy, wildcard, and regular expressions. The queries include a $project stage to exclude all fields except title.

다음 쿼리에서는 퍼지 (~) 를 사용하여 title 필드에서 최대 두 자가 변형된 catch 유사 텀이 포함된 영화 제목에 대해 퍼지 검색을 수행합니다.

1db.movies.aggregate([
2 {
3 "$search": {
4 "queryString": {
5 "defaultPath": "title",
6 "query": "catch~2"
7 }
8 }
9 },
10 {
11 "$limit": 10
12 },
13 {
14 "$project": {
15 "_id": 0,
16 "title": 1
17 }
18 }
19])
1[
2 { title: 'Catch-22' },
3 { title: 'Catch That Girl' },
4 { title: 'Catch That Kid' },
5 { title: 'Catch a Fire' },
6 { title: 'Catch Me Daddy' },
7 { title: 'Death Watch' },
8 { title: 'Patch Adams' },
9 { title: "Batch '81" },
10 { title: 'Briar Patch' },
11 { title: 'Night Watch' }
12]

다음 쿼리는 movies 컬렉션의 title 필드에서 Marvel, Avengers, Iron Man 또는 Captain America를 검색합니다.

다음 쿼리는 검색 조건과 일치하는 다음 버킷(연도)의 영화를 반환합니다.

  • 2000, 이 버킷에 대한 포괄적인 하한값

  • 2005, 2000 버킷의 상한 제외 및 이 버킷의 하한 포함

  • 2010, 2005 버킷의 상한 제외 및 이 버킷의 하한 포함

  • 2015, 2010 버킷의 상한 제외 및 이 버킷의 하한 포함

  • 2020, 2015 버킷의 배타적 상한선

1db.movies.aggregate([
2 {
3 "$searchMeta": {
4 "facet": {
5 "operator": {
6 "queryString": {
7 "defaultPath": "title",
8 "query": "\"Marvel\" OR \"Avengers\" OR \"Iron Man\" OR \"Captain America\""
9 }
10 },
11 "facets": {
12 "yearFacet": {
13 "type": "number",
14 "path": "year",
15 "boundaries": [1990, 2000, 2005, 2010, 2015, 2020 ]
16 }
17 }
18 }
19 }
20 }
21])