Docs Menu
Docs Home
/
MongoDB Atlas
/ /

부분 일치 아틀라스 검색 쿼리를 실행하는 방법

이 페이지의 내용

  • Atlas Search 인덱스 만들기
  • 대/소문자를 구분하는 부분 일치 쿼리 실행

이 튜토리얼에서는 sample_mflix.movies 컬렉션에서 인덱스를 생성하고 plot 필드에 대해 부분 문자열 쿼리를 실행하는 방법을 설명합니다. 부분 문자열 쿼리에 대한 일치 항목을 반환하려면 다음 연산자 중 하나를 사용할 수 있습니다.

  • 자동완성 연산자를 사용하면 쿼리에 지정한 일련의 문자가 포함된 단어나 구를 특정 필드에서 검색할 수 있습니다.

  • 구문 연산자를 사용하면 지정된 필드에서 쿼리 문자열의 용어가 포함된 문서를 용어 사이에 지정한 거리에서 검색할 수 있습니다.

  • 정규식 연산자는 정규식을 사용하여 지정된 필드에서 문자열을 검색합니다.

  • 와일드카드 연산자는 쿼리의 특수 문자를 사용하여 임의의 문자와 일치하도록 지정된 필드를 검색할 수 있습니다.

이 튜토리얼에서는 다음 단계를 안내합니다:

  1. Atlas Search 인덱스를 sample_mflix.movies컬렉션의 plot 필드에 생성합니다.

  2. 자동 완성, 구문, 정규식와일드카드 연산자를 사용하여 Atlas Search 쿼리를 sample_mflix.movies 컬렉션 plot 필드의 부분 문자열에 대해 실행합니다.

시작하기 전에 Atlas 클러스터가 필수구성 요소에 설명된 요건을 충족하는지 확인하십시오.

Atlas Search 인덱스를 생성하려면 프로젝트에 대한 Project Data Access Admin 이상의 액세스 권한이 있어야 합니다.

이 섹션에서는 sample_mflix.movies 컬렉션의 plot 필드에 대한 부분 일치 쿼리를 실행하기 위해 해당 필드에 대한 Atlas Search 인덱스를 만듭니다.

1
  1. 아직 표시되지 않은 경우 탐색 표시줄의 Organizations 메뉴에서 원하는 프로젝트가 포함된 조직을 선택합니다.

  2. 아직 표시되지 않은 경우 탐색 표시줄의 Projects 메뉴에서 원하는 프로젝트를 선택합니다.

  3. 아직 표시되지 않은 경우 사이드바에서 Clusters 클릭하세요.

    클러스터 페이지가 표시됩니다.

2

사이드바, Data Explorer 또는 클러스터 세부 정보 페이지에서 Atlas Search 페이지로 이동할 수 있습니다.

  1. 사이드바에서 Services 제목 아래의 Atlas Search를 클릭합니다.

  2. Select data source 드롭다운에서 클러스터를 선택하고 Go to Atlas Search를 클릭합니다.

    Atlas Search 페이지가 표시됩니다.

  1. cluster의 Browse Collections 버튼을 클릭합니다.

  2. 데이터베이스를 확장하고 컬렉션을 선택합니다.

  3. 컬렉션의 Search Indexes 탭을 클릭합니다.

    Atlas Search 페이지가 표시됩니다.

  1. 클러스터 이름을 클릭합니다.

  2. Atlas Search 탭을 클릭합니다.

    Atlas Search 페이지가 표시됩니다.

3
4
  • 안내를 따라 진행하고자 할 때는 Atlas Search Visual Editor를 선택합니다.

  • 원시 인덱스 정의를 편집하고 싶다면, Atlas Search JSON Editor을 선택합니다.

5
  1. Index Name 필드에 partial-match-tutorial를 입력합니다.

    인덱스 이름을 default로 지정하면 $search 파이프라인 단계에서 index 매개변수를 지정할 필요가 없습니다. 인덱스에 사용자 지정 이름을 지정하는 경우 index 매개변수에 이 이름을 지정해야 합니다.

  2. Database and Collection 섹션에서 sample_mflix 데이터베이스를 찾고 movies 컬렉션을 선택합니다.

6

Atlas 사용자 인터페이스에서 Atlas Search Visual Editor 또는 Atlas Search JSON Editor를 사용하여 인덱스를 생성할 수 있습니다.

  1. Next를 클릭합니다.

  2. Refine Your Index를 클릭합니다.

  3. Index Configurations 섹션에서 Dynamic Mapping을 비활성화하도록 토글합니다.

  4. Field Mappings 섹션에서 다음을 클릭

  5. Add Field Add Field Mapping 창이 표시됩니다.

    1. Quick Start for Text Fields를 클릭합니다.

    2. 드롭다운에서 Search-as-you-type 템플릿을 선택하고 기본 설정을 검토합니다.

      이러한 설정에 관한 자세한 사항은 자동 완성을 위해 필드 인덱싱 방법을 참조하십시오.

    1. Quick Start for Text Fields를 클릭합니다.

    2. 드롭다운에서 Full Text Search 템플릿을 선택하고 기본 설정을 검토합니다.

      이러한 설정에 대해 자세히 알아보려면 문자열 필드를 인덱싱하는 방법을 참조하세요.

    1. 드롭다운 메뉴에서 Customized Configuration 을(를) 선택합니다.

    2. 와일드카드 연산자를 사용하여 쿼리를 실행하려면 Data Type 드롭다운에서 String을 선택합니다.

    3. 아래와 같이 데이터 유형에 대한 기본 설정을 수정합니다.

      UI 필드 이름
      구성
      Index Analyzer
      lucene.keyword
      Search Analyzer
      lucene.keyword
      Index Options
      offsets
      Store
      true
      Ignore Above
      none
      Norms
      include

      이러한 설정에 대해 자세히 알아보려면 문자열 필드를 인덱싱하는 방법을 참조하세요.

    1. 드롭다운 메뉴에서 Customized Configuration 을(를) 선택합니다.

    2. 와일드카드 연산자를 사용하여 쿼리를 실행하려면 Data Type 드롭다운에서 String을 선택합니다.

    3. 아래와 같이 데이터 유형에 대한 기본 설정을 수정합니다.

      UI 필드 이름
      구성
      Index Analyzer
      lucene.keyword
      Search Analyzer
      lucene.keyword
      Index Options
      offsets
      Store
      true
      Ignore Above
      none
      Norms
      include

      이러한 설정에 대해 자세히 알아보려면 문자열 필드를 인덱싱하는 방법을 참조하세요.

  6. Field Name 드롭다운 메뉴에서 plot을(를) 선택합니다.

  7. Add을 클릭하여 Field Mappings 섹션의 목록에 필드를 추가합니다.

  8. Save Changes를 클릭합니다.

  1. 기본 인덱스 정의를 쿼리 실행에 사용하려는 연산자에 대한 다음 예제 인덱스 정의로 바꿉니다.

    {
    "mappings": {
    "dynamic": false,
    "fields": {
    "plot": [
    {
    "type": "autocomplete",
    "tokenization": "edgeGram",
    "minGrams": 2,
    "maxGrams": 15,
    "foldDiacritics": true
    }
    ]
    }
    }
    }
    {
    "mappings": {
    "fields": {
    "plot": {
    "analyzer": "lucene.standard",
    "type": "string"
    }
    }
    }
    }
    {
    "mappings": {
    "fields": {
    "plot": {
    "analyzer": "lucene.keyword",
    "type": "string"
    }
    }
    }
    }
    {
    "mappings": {
    "fields": {
    "plot": {
    "analyzer": "lucene.keyword",
    "type": "string"
    }
    }
    }
    }
  2. Next를 클릭합니다.

7
8

인덱스가 작성 중임을 보여주는 모달 창이 표시됩니다. Close 버튼을 클릭합니다.

9

인덱스 작성에는 약 1분 정도가 소요됩니다. 작성하는 동안 Status 열은 Build in Progress을 읽습니다. 작성이 완료되면 Status 열에 Active이 표시됩니다.


➤ 이 페이지의 언어 선택 드롭다운 메뉴를 사용하여 이 섹션에 있는 예시의 언어를 설정합니다.


자동 완성, 구문, 정규식와일드카드 연산자를 사용하여 대소문자를 구분하는 부분 일치 쿼리를 실행할 수 있습니다. 이 튜토리얼에서는 이러한 연산자를 사용하여 플롯에 지정된 부분 문자열이 포함된 영화를 검색해 보겠습니다.

이 섹션에서는 Atlas 클러스터에 연결하고 sample_mflix.movies 컬렉션 plot 필드에 대해 연산자를 사용하여 샘플 쿼리를 실행합니다.

1
  1. 아직 표시되지 않은 경우 탐색 표시줄의 Organizations 메뉴에서 원하는 프로젝트가 포함된 조직을 선택합니다.

  2. 아직 표시되지 않은 경우 탐색 표시줄의 Projects 메뉴에서 원하는 프로젝트를 선택합니다.

  3. 아직 표시되지 않은 경우 사이드바에서 Clusters 클릭하세요.

    Clusters(클러스터) 페이지가 표시됩니다.

2

사이드바, Data Explorer 또는 클러스터 세부 정보 페이지에서 Atlas Search 페이지로 이동할 수 있습니다.

  1. 사이드바에서 Services 제목 아래의 Atlas Search를 클릭합니다.

  2. Select data source 드롭다운에서 클러스터를 선택하고 Go to Atlas Search를 클릭합니다.

    Atlas Search 페이지가 표시됩니다.

  1. cluster의 Browse Collections 버튼을 클릭합니다.

  2. 데이터베이스를 확장하고 컬렉션을 선택합니다.

  3. 컬렉션의 Search Indexes 탭을 클릭합니다.

    Atlas Search 페이지가 표시됩니다.

  1. 클러스터 이름을 클릭합니다.

  2. Atlas Search 탭을 클릭합니다.

    Atlas Search 페이지가 표시됩니다.

3

쿼리할 인덱스 오른쪽에 있는 Query 버튼을 클릭합니다.

4

Edit Query 을 클릭하면 JSON 형식의 기본 쿼리 구문 샘플을 볼 수 있습니다.

5

다음 쿼리는 연산자를 사용하여 sample_mflix.movies 컬렉션의 plot 필드를 쿼리합니다.

다음 쿼리를 복사하여 Query Editor에 붙여 넣은 다음 Query Editor에서 Search 버튼을 클릭합니다.

이 쿼리를 사용하면 다음을 수행하여 쿼리 문자열 new purchase 필드의 단어와 매칭할 수 있습니다.

  • 단어 new, purchaseplot 필드의 아무 곳에나 표시할 수 있습니다.

  • 쿼리 문자열의 두 문자 변형을 허용하여 쿼리를 필드의 단어와 매칭할 수 있지만 쿼리 문자열의 첫 번째 문자는 변경할 수 없습니다.

  • 최대 256개의 유사한 용어를 고려할 수 있습니다.

1[
2 {
3 "$search": {
4 "index": "partial-match-tutorial",
5 "autocomplete": {
6 "path": "plot",
7 "query": "new purchase",
8 "tokenOrder": "any",
9 "fuzzy": {
10 "maxEdits": 2,
11 "prefixLength": 1,
12 "maxExpansions": 256
13 }
14 },
15 "highlight": {
16 "path": "plot"
17 }
18 }
19 }
20]
SCORE: 3 _id: "573a13a4f29313caabd112f0"
A divorced woman and her diabetic daughter take refuge in their newly-purchased house's safe room, when three men break-in, searching for a missing fortune.
Matching fields: plot
SCORE: 3 _id: "573a13d1f29313caabd8e209"
A lonely writer develops an unlikely relationship with his newly purchased operating system that's designed to meet his every need.
Matching fields: plot
SCORE: 2 _id: "573a13bef29313caabd5b62d"
Set in the near future when artificial organs can be bought on credit, it revolves around a man who struggles to make the payments on a heart he has purchased. He must
Matching fields: plot
SCORE: 2 _id: "573a13b3f29313caabd3c91e"
He is "purchased" by a wealthy television producer and taken to a desolate island where he must fight to the death against nine other condemned killers from all corners of the world, with freedom going to the sole survivor.
Matching fields: plot
SCORE: 2 _id: "573a1398f29313caabceb931"
A country boy becomes the head of a gang through the purchase of some lucky roses from an old lady.
Matching fields: plot, plot
SCORE: 1 _id: "573a13a7f29313caabd1b5ab"
An illegal Nigerian immigrant discovers the unpalatable side of London life.
Matching fields: plot
SCORE: 1 _id: "573a13a7f29313caabd1b5c0"
A young dropout falls in love with a nightclub dancer...
Matching fields: plot
SCORE: 1 _id: "573a13a7f29313caabd1b62f"
In 1990, to protect his fragile mother from a fatal shock after a long coma, a young man must keep her from learning that her beloved nation of East Germany as she knew it has disappeared.
Matching fields: plot
SCORE: 1 _id: "573a13a7f29313caabd1b6af"
Gadget once again has to fight his arch nemesis, Claw.
Matching fields: plot

쿼리는 new ~ purpose 사이의 위치 거리가 최대 5인 쿼리 문자열 new purpose를 지정합니다.

1[
2 {
3 "$search": {
4 "index": "partial-match-tutorial",
5 "phrase": {
6 "path": "plot",
7 "query": "new purpose",
8 "slop": 5
9 },
10 "highlight": {
11 "path": "plot"
12 }
13 }
14 }
15]
SCORE: 3.7209534645080566 _id: "573a13b1f29313caabd37ae6"
The true story of Richard Pimentel, a brilliant public speaker with a troubled past, who returns from Vietnam severely hearing -impaired and finds a new purpose in his landmark efforts on the behalf of Americans with disabilities.
Matching fields: plot
SCORE: 1.1507558822631836 _id: "573a13bdf29313caabd58a26"
But without a hero, he loses all purpose and must find new meaning to his life.
Matching fields: plot
SCORE: 1.0041160583496094 _id: "573a1396f29313caabce5197"
An aging Pat Garrett is hired as a lawman on behalf of a group of wealthy New Mexico cattle barons--his sole purpose being to bring down his old friend Billy the Kid.
Matching fields: plot

이 쿼리는 (.*) 정규 표현식을 사용하여 부분 쿼리 문자열과 임의 수의 문자를 일치시킵니다.

1[
2 {
3 "$search": {
4 "index": "partial-match-tutorial",
5 "regex": {
6 "path": "plot",
7 "query": "(.*)new(.*) pur(.*)"
8 }
9 }
10 }
11]
SCORE: 1 _id: "573a1397f29313caabce77d9"
fullplot: "After the Rebel base on the icy planet Hoth is taken over by the empir…"
imdb: Object
year: 1980
SCORE: 1 _id: "573a1398f29313caabceb893"
plot: "The new owner of the Cleveland Indians puts together a purposely horri…"
genres: Array
runtime: 107
SCORE: 1 _id: "573a13b1f29313caabd37ae6"
plot: "The true story of Richard Pimentel, a brilliant public speaker with a …"
genres: Array
runtime: 94
SCORE: 1 _id: "573a13d1f29313caabd8e209"
fullplot: "Theodore is a lonely man in the final stages of his divorce. When he's…"
imdb: Object
year: 2013
SCORE: 1 _id: "573a13dcf29313caabdb107a"
plot: "An adrenaline junkie walks away from a whirlwind romance and embraces …"
genres: Array
runtime: 146

쿼리는 쿼리 문자열에서 *을(를) 사용하여 부분 쿼리 문자열에서 지정된 단어 앞과 뒤에 0개 이상의 문자를 일치시킵니다.

1[
2 {
3 "$search": {
4 "index": "partial-match-tutorial",
5 "wildcard": {
6 "path": "plot",
7 "query": "*new* pur*"
8 }
9 }
10 }
11]
SCORE: 1 _id: "573a1397f29313caabce77d9"
fullplot: "After the Rebel base on the icy planet Hoth is taken over by the empir…"
imdb: Object
year: 1980
SCORE: 1 _id: "573a1398f29313caabceb893"
plot: "The new owner of the Cleveland Indians puts together a purposely horri…"
genres: Array
runtime: 107
SCORE: 1 _id: "573a13b1f29313caabd37ae6"
plot: "The true story of Richard Pimentel, a brilliant public speaker with a …"
genres: Array
runtime: 94
SCORE: 1 _id: "573a13d1f29313caabd8e209"
fullplot: "Theodore is a lonely man in the final stages of his divorce. When he's…"
imdb: Object
year: 2013
SCORE: 1 _id: "573a13dcf29313caabdb107a"
plot: "An adrenaline junkie walks away from a whirlwind romance and embraces …"
genres: Array
runtime: 146
6

Search Tester는 반환하는 문서의 모든 필드를 표시하지 않을 수 있습니다. 쿼리 경로에 지정한 필드를 비롯하여 모든 필드를 보려면 결과에서 문서를 펼칩니다.

1

터미널 창에서 mongosh 를 열고 클러스터에 연결합니다. 연결에 대한 자세한 지침은 mongosh 를 통한 연결을 참조하세요.

2

mongosh 프롬프트에서 다음 명령을 실행합니다.

use sample_mflix
3

다음 쿼리에서는 연산자를 사용하여 sample_mflix.movies 컬렉션의 plot 필드를 쿼리합니다. 쿼리에는 다음이 포함됩니다.

  • $limit 단계를 사용하여 출력을 5개의 결과로 제한합니다.

  • titleplot를 제외한 모든 필드를 제외하기 위한 $project 단계

이 쿼리를 사용하면 다음을 수행하여 쿼리 문자열 new purchase 필드의 단어와 매칭할 수 있습니다.

  • 단어 new, purchaseplot 필드의 아무 곳에나 표시할 수 있습니다.

  • 쿼리 문자열의 두 문자 변형을 허용하여 쿼리를 필드의 단어와 매칭할 수 있지만 쿼리 문자열의 첫 번째 문자는 변경할 수 없습니다.

  • 최대 256개의 유사한 용어를 고려할 수 있습니다.

1db.movies.aggregate([
2 {
3 "$search": {
4 "index": "partial-match-tutorial",
5 "autocomplete": {
6 "path": "plot",
7 "query": "new purchase",
8 "tokenOrder": "any",
9 "fuzzy": {
10 "maxEdits": 2,
11 "prefixLength": 1,
12 "maxExpansions": 256
13 }
14 },
15 "highlight": {
16 "path": "plot"
17 }
18 }
19 },
20 {
21 "$limit": 5
22 },
23 {
24 "$project": {
25 "_id": 0,
26 "title": 1,
27 "plot": 1,
28 "highlights": { "$meta": "searchHighlights" }
29 }
30 }
31])
[
{
plot: "A divorced woman and her diabetic daughter take refuge in their newly-purchased house's safe room, when three men break-in, searching for a missing fortune.",
title: 'Panic Room',
highlights: [
{
score: 4.364492893218994,
path: 'plot',
texts: [
{
value: 'A divorced woman and her diabetic daughter take refuge in their ',
type: 'text'
},
{ value: "newly-purchased house's safe", type: 'hit' },
{
value: ' room, when three men break-in, searching for a missing fortune.',
type: 'text'
}
]
}
]
},
{
plot: "A lonely writer develops an unlikely relationship with his newly purchased operating system that's designed to meet his every need.",
title: 'Her',
highlights: [
{
score: 4.198050022125244,
path: 'plot',
texts: [
{
value: 'A lonely writer develops an unlikely relationship with his ',
type: 'text'
},
{ value: 'newly purchased operating system', type: 'hit' },
{
value: " that's designed to meet his every ",
type: 'text'
},
{ value: 'need', type: 'hit' },
{ value: '.', type: 'text' }
]
}
]
},
{
plot: "Set in the near future when artificial organs can be bought on credit, it revolves around a man who struggles to make the payments on a heart he has purchased. He must therefore go on the run before said ticker is repossessed.",
title: 'Repo Men',
highlights: [
{
score: 2.6448397636413574,
path: 'plot',
texts: [
{ value: 'Set in the ', type: 'text' },
{ value: 'near future when', type: 'hit' },
{ value: ' artificial organs can be bought on credit, it revolves around a man who struggles to make the payments on a heart he has ', type: 'text' },
{ value: 'purchased. He must', type: 'hit' }
]
}
]
},
{
plot: 'A psychologically troubled novelty supplier is nudged towards a romance with an English woman, all the while being extorted by a phone-sex line run by a crooked mattress salesman, and purchasing stunning amounts of pudding.',
title: 'Punch-Drunk Love',
highlights: [
{
score: 1.2451990842819214,
path: 'plot',
texts: [
{ value: 'A psychologically troubled ', type: 'text' },
{ value: 'novelty supplier is', type: 'hit' },
{ value: ' ', type: 'text' },
{ value: 'nudged towards a', type: 'hit' },
{
value: ' romance with an English woman, all the while being extorted by a phone-sex line run by a crooked mattress salesman, and ',
type: 'text'
},
{ value: 'purchasing stunning amounts', type: 'hit' },
{ value: ' of pudding.', type: 'text' }
]
}
]
},
{
plot: 'Jack Conrad is awaiting the death penalty in a corrupt Central American prison. He is "purchased" by a wealthy television producer and taken to a desolate island where he must fight to the death against nine other condemned killers from all corners of the world, with freedom going to the sole survivor.',
title: 'The Condemned',
highlights: [
{
score: 2.94378924369812,
path: 'plot',
texts: [
{ value: 'He is "', type: 'text' },
{ value: 'purchased" by a', type: 'hit' },
{
value: ' wealthy television producer and taken to a desolate island where he must fight to the death against ',
type: 'text'
},
{ value: 'nine other condemned', type: 'hit' },
{
value: ' killers from all corners of the world, with freedom going to the sole survivor.',
type: 'text'
}
]
}
]
}
]

쿼리는 new ~ purpose 사이의 위치 거리가 최대 5인 쿼리 문자열 new purpose를 지정합니다.

1db.movies.aggregate([
2 {
3 "$search": {
4 "index": "partial-match-tutorial",
5 "phrase": {
6 "path": "plot",
7 "query": "new purpose",
8 "slop": 5
9 },
10 "highlight": {
11 "path": "plot"
12 }
13 }
14 },
15 {
16 "$limit": 5
17 },
18 {
19 "$project": {
20 "_id": 0,
21 "plot": 1,
22 "title": 1,
23 "highlights": { "$meta": "searchHighlights" }
24 }
25 }
26])
[
{
plot: 'The true story of Richard Pimentel, a brilliant public speaker with a troubled past, who returns from Vietnam severely hearing -impaired and finds a new purpose in his landmark efforts on the behalf of Americans with disabilities.',
title: 'Music Within',
highlights: [
{
score: 1.9394469261169434,
path: 'plot',
texts: [
{ value: 'The true story of Richard Pimentel, a brilliant public speaker with a troubled past, who returns from Vietnam severely hearing -impaired and finds a ', type: 'text' },
{ value: 'new', type: 'hit' },
{ value: ' ', type: 'text' },
{ value: 'purpose', type: 'hit' },
{ value: ' in his landmark efforts on the behalf of Americans with disabilities.', type: 'text' }
]
}
]
},
{
plot: 'The supervillain Megamind finally defeats his nemesis, the superhero Metro Man. But without a hero, he loses all purpose and must find new meaning to his life.',
title: 'Megamind',
highlights: [
{
score: 2.90376877784729,
path: 'plot',
texts: [
{ value: 'But without a hero, he loses all ', type: 'text' },
{ value: 'purpose', type: 'hit' },
{ value: ' and must find ', type: 'text' },
{ value: 'new', type: 'hit' },
{ value: ' meaning to his life.', type: 'text' }
]
}
]
},
{
plot: 'An aging Pat Garrett is hired as a lawman on behalf of a group of wealthy New Mexico cattle barons--his sole purpose being to bring down his old friend Billy the Kid.',
title: 'Pat Garrett & Billy the Kid',
highlights: [
{
score: 2.115748405456543,
path: 'plot',
texts: [
{ value: 'An aging Pat Garrett is hired as a lawman on behalf of a group of wealthy ', type: 'text' },
{ value: 'New', type: 'hit' },
{ value: ' Mexico cattle barons--his sole ', type: 'text' },
{ value: 'purpose', type: 'hit' },
{ value: ' being to bring down his old friend Billy the Kid.', type: 'text' }
]
}
]
}
]

이 쿼리는 (.*) 정규 표현식을 사용하여 부분 쿼리 문자열과 임의 수의 문자를 일치시킵니다.

1db.movies.aggregate([
2 {
3 "$search": {
4 "index": "partial-match-tutorial",
5 "regex": {
6 "path": "plot",
7 "query": "(.*)new(.*) pur(.*)"
8 }
9 }
10 },
11 {
12 $limit: 5
13 },
14 {
15 $project: {
16 "_id": 0,
17 "plot": 1,
18 "title": 1
19 }
20 }
21])
[
{
plot: 'After the rebels have been brutally overpowered by the Empire on their newly established base, Luke Skywalker takes advanced Jedi training with Master Yoda, while his friends are pursued by Darth Vader as part of his plan to capture Luke.',
title: 'Star Wars: Episode V - The Empire Strikes Back'
},
{
plot: "The new owner of the Cleveland Indians puts together a purposely horrible team so they'll lose and she can move the team. But when the plot is uncovered, they start winning just to spite her.",
title: 'Major League'
},
{
plot: 'The true story of Richard Pimentel, a brilliant public speaker with a troubled past, who returns from Vietnam severely hearing -impaired and finds a new purpose in his landmark efforts on the behalf of Americans with disabilities.',
title: 'Music Within'
},
{
plot: "A lonely writer develops an unlikely relationship with his newly purchased operating system that's designed to meet his every need.",
title: 'Her'
},
{
plot: 'An adrenaline junkie walks away from a whirlwind romance and embraces a new life as a thief, though he soon finds himself pursued by veteran police officer and engaged in a turf war with a local gangster.',
title: 'Kick'
}
]

쿼리는 쿼리 문자열에서 *을(를) 사용하여 부분 쿼리 문자열에서 지정된 단어 앞과 뒤에 0개 이상의 문자를 일치시킵니다.

1db.movies.aggregate([
2 {
3 "$search": {
4 "index": "partial-match-tutorial",
5 "wildcard": {
6 "path": "plot",
7 "query": "*new* pur*"
8 }
9 }
10 },
11 {
12 "$limit": 5
13 },
14 {
15 "$project": {
16 "_id": 0,
17 "plot": 1
18 }
19 }
20])
[
{
plot: 'After the rebels have been brutally overpowered by the Empire on their newly established base, Luke Skywalker takes advanced Jedi training with Master Yoda, while his friends are pursued by Darth Vader as part of his plan to capture Luke.',
title: 'Star Wars: Episode V - The Empire Strikes Back'
},
{
plot: "The new owner of the Cleveland Indians puts together a purposely horrible team so they'll lose and she can move the team. But when the plot is uncovered, they start winning just to spite her.",
title: 'Major League'
},
{
plot: 'The true story of Richard Pimentel, a brilliant public speaker with a troubled past, who returns from Vietnam severely hearing -impaired and finds a new purpose in his landmark efforts on the behalf of Americans with disabilities.',
title: 'Music Within'
},
{
plot: "A lonely writer develops an unlikely relationship with his newly purchased operating system that's designed to meet his every need.",
title: 'Her'
},
{
plot: 'An adrenaline junkie walks away from a whirlwind romance and embraces a new life as a thief, though he soon finds himself pursued by veteran police officer and engaged in a turf war with a local gangster.',
title: 'Kick'
}
]
1

MongoDB Compass를 열고 cluster에 연결합니다. 연결에 대한 자세한 내용은 Compass를 통한 연결을 참조하세요.

2

Database 화면에서 sample_mflix 데이터베이스를 클릭한 다음 movies collection을 클릭합니다.

3

다음 쿼리에서는 연산자를 사용하여 sample_mflix.movies 컬렉션의 plot 필드를 쿼리합니다. 쿼리에는 다음이 포함됩니다.

  • $limit 단계를 사용하여 출력을 5개의 결과로 제한합니다.

  • titleplot를 제외한 모든 필드를 제외하기 위한 $project 단계

MongoDB Compass에서 이 쿼리를 실행하려면 다음 안내를 따르세요.

  1. Aggregations 탭을 클릭합니다.

  2. Select...를 클릭한 다음 드롭다운에서 단계를 선택하고 해당 단계에 대한 쿼리를 추가하여 다음 파이프라인 단계를 각각 구성합니다. 단계를 추가하려면 Add Stage을 클릭합니다.

    이 쿼리를 사용하면 다음을 수행하여 쿼리 문자열 new purchase 필드의 단어와 매칭할 수 있습니다.

    • 단어 new, purchaseplot 필드의 아무 곳에나 표시할 수 있습니다.

    • 쿼리 문자열의 두 문자 변형을 허용하여 쿼리를 필드의 단어와 매칭할 수 있지만 쿼리 문자열의 첫 번째 문자는 변경할 수 없습니다.

    • 최대 256개의 유사한 용어를 고려할 수 있습니다.

    파이프라인 단계
    쿼리
    $search
    {
    "index": "partial-match-tutorial",
    "autocomplete": {
    "path": "plot",
    "query": "new purchase",
    "tokenOrder": "any",
    "fuzzy": {
    "maxEdits": 2,
    "prefixLength": 1,
    "maxExpansions": 256
    }
    },
    "highlight": {
    "path": "plot"
    }
    }
    $limit
    5
    $project
    {
    "_id": 0,
    "title": 1,
    "plot": 1,
    "highlights": { "$meta": "searchHighlights" }
    }

    쿼리는 new ~ purpose 사이의 위치 거리가 최대 5인 쿼리 문자열 new purpose를 지정합니다.

    파이프라인 단계
    쿼리
    $search
    {
    "index": "partial-match-tutorial",
    "phrase": {
    "path": "plot",
    "query": "new purpose",
    "slop": 5
    },
    "highlight": {
    "path": "plot"
    }
    }
    $limit
    5
    $project
    {
    "_id": 0,
    "title": 1,
    "plot": 1,
    "highlights": { "$meta": "searchHighlights" }
    }

    이 쿼리는 (.*) 정규 표현식을 사용하여 부분 쿼리 문자열과 임의 수의 문자를 일치시킵니다.

    파이프라인 단계
    쿼리
    $search
    {
    "index": "partial-match-tutorial",
    "regex": {
    "path": "plot",
    "query": "(.*)new(.*) pur(.*)"
    }
    }
    $limit
    5
    $project
    {
    "_id": 0,
    "title": 1,
    "plot": 1
    }

    쿼리는 쿼리 문자열에서 *을(를) 사용하여 부분 쿼리 문자열에서 지정된 단어 앞과 뒤에 0개 이상의 문자를 일치시킵니다.

    파이프라인 단계
    쿼리
    $search
    {
    "index": "partial-match-tutorial",
    "wildcard": {
    "path": "plot",
    "query": "*new* pur*"
    }
    }
    $limit
    5
    $project
    {
    "_id": 0,
    "title": 1,
    "plot": 1
    }

Auto Preview를 활성화한 경우 MongoDB Compass는 $project 파이프라인 단계 옆에 다음 문서를 표시합니다.

1[
2 {
3 plot: "A divorced woman and her diabetic daughter take refuge in their newly-purchased house's safe room, when three men break-in, searching for a missing fortune.",
4 title: 'Panic Room',
5 highlights: [
6 {
7 score: 4.364492893218994,
8 path: 'plot',
9 texts: [
10 {
11 value: 'A divorced woman and her diabetic daughter take refuge in their ',
12 type: 'text'
13 },
14 { value: "newly-purchased house's safe", type: 'hit' },
15 {
16 value: ' room, when three men break-in, searching for a missing fortune.',
17 type: 'text'
18 }
19 ]
20 }
21 ]
22 },
23 {
24 plot: "A lonely writer develops an unlikely relationship with his newly purchased operating system that's designed to meet his every need.",
25 title: 'Her',
26 highlights: [
27 {
28 score: 4.198050022125244,
29 path: 'plot',
30 texts: [
31 {
32 value: 'A lonely writer develops an unlikely relationship with his ',
33 type: 'text'
34 },
35 { value: 'newly purchased operating system', type: 'hit' },
36 {
37 value: " that's designed to meet his every ",
38 type: 'text'
39 },
40 { value: 'need', type: 'hit' },
41 { value: '.', type: 'text' }
42 ]
43 }
44 ]
45 },
46 {
47 plot: "Set in the near future when artificial organs can be bought on credit, it revolves around a man who struggles to make the payments on a heart he has purchased. He must therefore go on the run before said ticker is repossessed.",
48 title: 'Repo Men',
49 highlights: [
50 {
51 score: 2.6448397636413574,
52 path: 'plot',
53 texts: [
54 { value: 'Set in the ', type: 'text' },
55 { value: 'near future when', type: 'hit' },
56 { value: ' artificial organs can be bought on credit, it revolves around a man who struggles to make the payments on a heart he has ', type: 'text' },
57 { value: 'purchased. He must', type: 'hit' }
58 ]
59 }
60 ]
61 },
62 {
63 plot: 'A psychologically troubled novelty supplier is nudged towards a romance with an English woman, all the while being extorted by a phone-sex line run by a crooked mattress salesman, and purchasing stunning amounts of pudding.',
64 title: 'Punch-Drunk Love',
65 highlights: [
66 {
67 score: 1.2451990842819214,
68 path: 'plot',
69 texts: [
70 { value: 'A psychologically troubled ', type: 'text' },
71 { value: 'novelty supplier is', type: 'hit' },
72 { value: ' ', type: 'text' },
73 { value: 'nudged towards a', type: 'hit' },
74 {
75 value: ' romance with an English woman, all the while being extorted by a phone-sex line run by a crooked mattress salesman, and ',
76 type: 'text'
77 },
78 { value: 'purchasing stunning amounts', type: 'hit' },
79 { value: ' of pudding.', type: 'text' }
80 ]
81 }
82 ]
83 },
84 {
85 plot: 'Jack Conrad is awaiting the death penalty in a corrupt Central American prison. He is "purchased" by a wealthy television producer and taken to a desolate island where he must fight to the death against nine other condemned killers from all corners of the world, with freedom going to the sole survivor.',
86 title: 'The Condemned',
87 highlights: [
88 {
89 score: 2.94378924369812,
90 path: 'plot',
91 texts: [
92 { value: 'He is "', type: 'text' },
93 { value: 'purchased" by a', type: 'hit' },
94 {
95 value: ' wealthy television producer and taken to a desolate island where he must fight to the death against ',
96 type: 'text'
97 },
98 { value: 'nine other condemned', type: 'hit' },
99 {
100 value: ' killers from all corners of the world, with freedom going to the sole survivor.',
101 type: 'text'
102 }
103 ]
104 }
105 ]
106 }
107]
1[
2 {
3 plot: 'The true story of Richard Pimentel, a brilliant public speaker with a troubled past, who returns from Vietnam severely hearing -impaired and finds a new purpose in his landmark efforts on the behalf of Americans with disabilities.',
4 title: 'Music Within',
5 highlights: [
6 {
7 score: 1.9394469261169434,
8 path: 'plot',
9 texts: [
10 { value: 'The true story of Richard Pimentel, a brilliant public speaker with a troubled past, who returns from Vietnam severely hearing -impaired and finds a ', type: 'text' },
11 { value: 'new', type: 'hit' },
12 { value: ' ', type: 'text' },
13 { value: 'purpose', type: 'hit' },
14 { value: ' in his landmark efforts on the behalf of Americans with disabilities.', type: 'text' }
15 ]
16 }
17 ]
18 },
19 {
20 plot: 'The supervillain Megamind finally defeats his nemesis, the superhero Metro Man. But without a hero, he loses all purpose and must find new meaning to his life.',
21 title: 'Megamind',
22 highlights: [
23 {
24 score: 2.90376877784729,
25 path: 'plot',
26 texts: [
27 { value: 'But without a hero, he loses all ', type: 'text' },
28 { value: 'purpose', type: 'hit' },
29 { value: ' and must find ', type: 'text' },
30 { value: 'new', type: 'hit' },
31 { value: ' meaning to his life.', type: 'text' }
32 ]
33 }
34 ]
35 },
36 {
37 plot: 'An aging Pat Garrett is hired as a lawman on behalf of a group of wealthy New Mexico cattle barons--his sole purpose being to bring down his old friend Billy the Kid.',
38 title: 'Pat Garrett & Billy the Kid',
39 highlights: [
40 {
41 score: 2.115748405456543,
42 path: 'plot',
43 texts: [
44 { value: 'An aging Pat Garrett is hired as a lawman on behalf of a group of wealthy ', type: 'text' },
45 { value: 'New', type: 'hit' },
46 { value: ' Mexico cattle barons--his sole ', type: 'text' },
47 { value: 'purpose', type: 'hit' },
48 { value: ' being to bring down his old friend Billy the Kid.', type: 'text' }
49 ]
50 }
51 ]
52 }
53]
1[
2 {
3 plot: 'After the rebels have been brutally overpowered by the Empire on their newly established base, Luke Skywalker takes advanced Jedi training with Master Yoda, while his friends are pursued by Darth Vader as part of his plan to capture Luke.',
4 title: 'Star Wars: Episode V - The Empire Strikes Back'
5 },
6 {
7 plot: "The new owner of the Cleveland Indians puts together a purposely horrible team so they'll lose and she can move the team. But when the plot is uncovered, they start winning just to spite her.",
8 title: 'Major League'
9 },
10 {
11 plot: 'The true story of Richard Pimentel, a brilliant public speaker with a troubled past, who returns from Vietnam severely hearing -impaired and finds a new purpose in his landmark efforts on the behalf of Americans with disabilities.',
12 title: 'Music Within'
13 },
14 {
15 plot: "A lonely writer develops an unlikely relationship with his newly purchased operating system that's designed to meet his every need.",
16 title: 'Her'
17 },
18 {
19 plot: 'An adrenaline junkie walks away from a whirlwind romance and embraces a new life as a thief, though he soon finds himself pursued by veteran police officer and engaged in a turf war with a local gangster.',
20 title: 'Kick'
21 }
22]
1[
2 {
3 plot: 'After the rebels have been brutally overpowered by the Empire on their newly established base, Luke Skywalker takes advanced Jedi training with Master Yoda, while his friends are pursued by Darth Vader as part of his plan to capture Luke.',
4 title: 'Star Wars: Episode V - The Empire Strikes Back'
5 },
6 {
7 plot: "The new owner of the Cleveland Indians puts together a purposely horrible team so they'll lose and she can move the team. But when the plot is uncovered, they start winning just to spite her.",
8 title: 'Major League'
9 },
10 {
11 plot: 'The true story of Richard Pimentel, a brilliant public speaker with a troubled past, who returns from Vietnam severely hearing -impaired and finds a new purpose in his landmark efforts on the behalf of Americans with disabilities.',
12 title: 'Music Within'
13 },
14 {
15 plot: "A lonely writer develops an unlikely relationship with his newly purchased operating system that's designed to meet his every need.",
16 title: 'Her'
17 },
18 {
19 plot: 'An adrenaline junkie walks away from a whirlwind romance and embraces a new life as a thief, though he soon finds himself pursued by veteran police officer and engaged in a turf war with a local gangster.',
20 title: 'Kick'
21 }
22]
1
  1. partial-match-example라는 새 디렉토리를 만들고 dotnet new 명령으로 프로젝트를 초기화합니다.

    mkdir partial-match-example
    cd partial-match-example
    dotnet new console
  2. .NET/C# 드라이버를 프로젝트에 종속성으로 추가합니다.

    dotnet add package MongoDB.Driver
2
  1. Program.cs 파일의 내용을 다음 코드로 바꿉니다.

    이 코드 예시에서는 다음 작업을 수행합니다:

    • mongodb 패키지 및 종속성을 가져옵니다.

    • Atlas 클러스터에 대한 연결을 설정합니다.

    • 다음을 사용하는 쿼리를 실행합니다.

      • $search 단계에서 텀 검색

      • $limit 단계를 사용하여 출력을 5개의 결과로 제한합니다.

      • $project 단계를 사용하여 다음을 제외한 모든 필드를 제외합니다. title
        개인정보 정책에 plot
    • 커서 위를 반복하여 쿼리와 일치하는 문서를 인쇄합니다.

    이 쿼리를 사용하면 다음을 수행하여 쿼리 문자열 new purchase 필드의 단어와 매칭할 수 있습니다.

    • 단어 new, purchaseplot 필드의 아무 곳에나 표시할 수 있습니다.

    • 쿼리 문자열의 두 문자 변형을 허용하여 쿼리를 필드의 단어와 매칭할 수 있지만 쿼리 문자열의 첫 번째 문자는 변경할 수 없습니다.

    • 최대 256개의 유사한 용어를 고려할 수 있습니다.

    1using MongoDB.Bson;
    2using MongoDB.Bson.Serialization.Attributes;
    3using MongoDB.Bson.Serialization.Conventions;
    4using MongoDB.Driver;
    5using MongoDB.Driver.Search;
    6
    7public class PartialAutocomplete
    8{
    9 private const string MongoConnectionString = "<connection-string>";
    10
    11 public static void Main(string[] args) {
    12 // allow automapping of the camelCase database fields to our MovieDocument
    13 var camelCaseConvention = new ConventionPack { new CamelCaseElementNameConvention() };
    14 ConventionRegistry.Register("CamelCase", camelCaseConvention, type => true);
    15
    16 // connect to your Atlas cluster
    17 var mongoClient = new MongoClient(MongoConnectionString);
    18 var mflixDatabase = mongoClient.GetDatabase("sample_mflix");
    19 var moviesCollection = mflixDatabase.GetCollection<MovieDocument>("movies");
    20
    21 // define fuzzy options
    22 SearchFuzzyOptions fuzzyOptions = new SearchFuzzyOptions()
    23 {
    24 MaxEdits = 2,
    25 PrefixLength = 1,
    26 MaxExpansions = 256
    27 };
    28
    29 // define and run pipeline
    30 var results = moviesCollection.Aggregate()
    31 .Search(Builders<MovieDocument>.Search.Autocomplete(movie => movie.Plot, "new purchase", SearchAutocompleteTokenOrder.Any, fuzzy: fuzzyOptions), new SearchHighlightOptions<MovieDocument>(movie => movie.Plot),
    32 indexName: "partial-match-tutorial")
    33 .Project<MovieDocument>(Builders<MovieDocument>.Projection
    34 .Include(movie => movie.Plot)
    35 .Include(movie => movie.Title)
    36 .Exclude(movie => movie.Id)
    37 .MetaSearchHighlights("highlights"))
    38 .Limit(5)
    39 .ToList();
    40
    41 // print results
    42 foreach (var movie in results) {
    43 Console.WriteLine(movie.ToJson());
    44 }
    45 }
    46}
    47
    48[BsonIgnoreExtraElements]
    49public class MovieDocument {
    50 [BsonIgnoreIfDefault]
    51 public ObjectId Id { get; set; }
    52 public string Plot { get; set; }
    53 public string Title { get; set; }
    54 [BsonElement("highlights")]
    55 public List<SearchHighlight> Highlights { get; set; }
    56}

    쿼리는 new ~ purpose 사이의 위치 거리가 최대 5인 쿼리 문자열 new purpose를 지정합니다.

    1using MongoDB.Bson;
    2using MongoDB.Bson.Serialization.Attributes;
    3using MongoDB.Bson.Serialization.Conventions;
    4using MongoDB.Driver;
    5using MongoDB.Driver.Search;
    6
    7public class PartialPhrase
    8{
    9 private const string MongoConnectionString = "<connection-string>";
    10
    11 public static void Main(string[] args) {
    12 // allow automapping of the camelCase database fields to our MovieDocument
    13 var camelCaseConvention = new ConventionPack { new CamelCaseElementNameConvention() };
    14 ConventionRegistry.Register("CamelCase", camelCaseConvention, type => true);
    15
    16 // connect to your Atlas cluster
    17 var mongoClient = new MongoClient(MongoConnectionString);
    18 var mflixDatabase = mongoClient.GetDatabase("sample_mflix");
    19 var moviesCollection = mflixDatabase.GetCollection<MovieDocument>("movies");
    20
    21 // define and run pipeline
    22 var results = moviesCollection.Aggregate()
    23 .Search(Builders<MovieDocument>.Search.Phrase(movie => movie.Plot, "new purpose", 5), new SearchHighlightOptions<MovieDocument>(movie => movie.Plot),
    24 indexName: "partial-match-tutorial")
    25 .Project<MovieDocument>(Builders<MovieDocument>.Projection
    26 .Include(movie => movie.Plot)
    27 .Include(movie => movie.Title)
    28 .Exclude(movie => movie.Id)
    29 .MetaSearchHighlights("highlights"))
    30 .Limit(5)
    31 .ToList();
    32
    33 // print results
    34 foreach (var movie in results) {
    35 Console.WriteLine(movie.ToJson());
    36 }
    37 }
    38}
    39
    40[BsonIgnoreExtraElements]
    41public class MovieDocument {
    42 [BsonIgnoreIfDefault]
    43 public ObjectId Id { get; set; }
    44 public string Plot { get; set; }
    45 public string Title { get; set; }
    46 [BsonElement("highlights")]
    47 public List<SearchHighlight> Highlights { get; set; }
    48}

    이 쿼리는 (.*) 정규 표현식을 사용하여 부분 쿼리 문자열과 임의 수의 문자를 일치시킵니다.

    1using MongoDB.Bson;
    2using MongoDB.Bson.Serialization.Attributes;
    3using MongoDB.Bson.Serialization.Conventions;
    4using MongoDB.Driver;
    5using MongoDB.Driver.Search;
    6
    7public class PartialRegex
    8{
    9 private const string MongoConnectionString = "<connection-string>";
    10
    11 public static void Main(string[] args) {
    12 // allow automapping of the camelCase database fields to our MovieDocument
    13 var camelCaseConvention = new ConventionPack { new CamelCaseElementNameConvention() };
    14 ConventionRegistry.Register("CamelCase", camelCaseConvention, type => true);
    15
    16 // connect to your Atlas cluster
    17 var mongoClient = new MongoClient(MongoConnectionString);
    18 var mflixDatabase = mongoClient.GetDatabase("sample_mflix");
    19 var moviesCollection = mflixDatabase.GetCollection<MovieDocument>("movies");
    20
    21 // define and run pipeline
    22 var results = moviesCollection.Aggregate()
    23 .Search(Builders<MovieDocument>.Search.Regex(movie => movie.Plot, "(.*)new(.*) pur(.*)"),
    24 indexName: "partial-match-tutorial")
    25 .Project<MovieDocument>(Builders<MovieDocument>.Projection
    26 .Include(movie => movie.Plot)
    27 .Include(movie => movie.Title)
    28 .Exclude(movie => movie.Id))
    29 .Limit(5)
    30 .ToList();
    31
    32 // print results
    33 foreach (var movie in results) {
    34 Console.WriteLine(movie.ToJson());
    35 }
    36 }
    37}
    38
    39[BsonIgnoreExtraElements]
    40public class MovieDocument {
    41 [BsonIgnoreIfDefault]
    42 public ObjectId Id { get; set; }
    43 public string Plot { get; set; }
    44 public string Title { get; set; }
    45}

    쿼리는 쿼리 문자열에서 *을(를) 사용하여 부분 쿼리 문자열에서 지정된 단어 앞과 뒤에 0개 이상의 문자를 일치시킵니다.

    1using MongoDB.Bson;
    2using MongoDB.Bson.Serialization.Attributes;
    3using MongoDB.Bson.Serialization.Conventions;
    4using MongoDB.Driver;
    5using MongoDB.Driver.Search;
    6
    7public class PartialWildcard
    8{
    9 private const string MongoConnectionString = "<connection-string>";
    10
    11 public static void Main(string[] args) {
    12 // allow automapping of the camelCase database fields to our MovieDocument
    13 var camelCaseConvention = new ConventionPack { new CamelCaseElementNameConvention() };
    14 ConventionRegistry.Register("CamelCase", camelCaseConvention, type => true);
    15
    16 // connect to your Atlas cluster
    17 var mongoClient = new MongoClient(MongoConnectionString);
    18 var mflixDatabase = mongoClient.GetDatabase("sample_mflix");
    19 var moviesCollection = mflixDatabase.GetCollection<MovieDocument>("movies");
    20
    21 // define and run pipeline
    22 var results = moviesCollection.Aggregate()
    23 .Search(Builders<MovieDocument>.Search.Wildcard(movie => movie.Plot, "*new* pur*"),
    24 indexName: "partial-match-tutorial")
    25 .Project<MovieDocument>(Builders<MovieDocument>.Projection
    26 .Include(movie => movie.Plot)
    27 .Include(movie => movie.Title)
    28 .Exclude(movie => movie.Id))
    29 .Limit(5)
    30 .ToList();
    31
    32 // print results
    33 foreach (var movie in results) {
    34 Console.WriteLine(movie.ToJson());
    35 }
    36 }
    37}
    38
    39[BsonIgnoreExtraElements]
    40public class MovieDocument {
    41 [BsonIgnoreIfDefault]
    42 public ObjectId Id { get; set; }
    43 public string Plot { get; set; }
    44 public string Title { get; set; }
    45}
  2. 샘플을 실행하기 전에 <connection-string>을 Atlas 연결 문자열로 바꿉니다. 연결 문자열에는 데이터베이스 사용자의 자격 증명이 포함되어 있어야 합니다. 자세한 내용은 드라이버를 통한 연결을 참조하세요.

3
dotnet run partial-match-example.csproj
{
"plot" : "A divorced woman and her diabetic daughter take refuge in
their newly-purchased house's safe room, when three men break-in,
searching for a missing fortune.",
"title" : "Panic Room",
"highlights" : [{
"path" : "plot",
"score" : 4.3644928932189941,
"texts" : [
{ "type" : "Text", "value" : "A divorced woman and her diabetic daughter take refuge in their " },
{ "type" : "Hit", "value" : "newly-purchased house's safe" },
{ "type" : "Text", "value" : " room, when three men break-in, searching for a missing fortune." }]
}]
}
{
"plot" : "A lonely writer develops an unlikely relationship with his
newly purchased operating system that's designed to meet his every
need.",
"title" : "Her",
"highlights" : [{
"path" : "plot",
"score" : 4.1980500221252441,
"texts" : [
{ "type" : "Text", "value" : "A lonely writer develops an unlikely relationship with his " },
{ "type" : "Hit", "value" : "newly purchased operating system" },
{ "type" : "Text", "value" : " that's designed to meet his every " },
{ "type" : "Hit", "value" : "need" },
{ "type" : "Text", "value" : "." }]
}]
}
{
"plot" : "Set in the near future when artificial organs can be
bought on credit, it revolves around a man who struggles to make the
payments on a heart he has purchased. He must therefore go on the
run before said ticker is repossessed.",
"title" : "Repo Men",
"highlights" : [{
"path" : "plot",
"score" : 2.6448397636413574,
"texts" : [
{ "type" : "Text", "value" : "Set in the " },
{ "type" : "Hit", "value" : "near future when" },
{ "type" : "Text", "value" : " artificial organs can be bought on credit, it revolves around a man who struggles to make the payments on a heart he has " },
{ "type" : "Hit", "value" : "purchased. He must" }]
}]
}
{
"plot" : "A psychologically troubled novelty supplier is nudged
towards a romance with an English woman, all the while being
extorted by a phone-sex line run by a crooked mattress salesman, and
purchasing stunning amounts of pudding.",
"title" : "Punch-Drunk Love",
"highlights" : [{
"path" : "plot",
"score" : 1.2451990842819214,
"texts" : [
{ "type" : "Text", "value" : "A psychologically troubled " },
{ "type" : "Hit", "value" : "novelty supplier is" },
{ "type" : "Text", "value" : " " },
{ "type" : "Hit", "value" : "nudged towards a" },
{ "type" : "Text", "value" : " romance with an English woman, all the while being extorted by a phone-sex line run by a crooked mattress salesman, and " },
{ "type" : "Hit", "value" : "purchasing stunning amounts" },
{ "type" : "Text", "value" : " of pudding." }]
}]
}
{
"plot" : "Jack Conrad is awaiting the death penalty in a corrupt
Central American prison. He is \"purchased\" by a wealthy television
producer and taken to a desolate island where he must fight to the
death against nine other condemned killers from all corners of the
world, with freedom going to the sole survivor.",
"title" : "The Condemned",
"highlights" : [{
"path" : "plot",
"score" : 2.9437892436981201,
"texts" : [
{ "type" : "Text", "value" : "He is \"" },
{ "type" : "Hit", "value" : "purchased\" by a" },
{ "type" : "Text", "value" : " wealthy television producer and taken to a desolate island where he must fight to the death against " },
{ "type" : "Hit", "value" : "nine other condemned" },
{ "type" : "Text", "value" : " killers from all corners of the world, with freedom going to the sole survivor." }]
}]
}
dotnet run partial-match-example.csproj
{
"plot" : "The true story of Richard Pimentel, a brilliant public
speaker with a troubled past, who returns from Vietnam severely
hearing -impaired and finds a new purpose in his landmark efforts on
the behalf of Americans with disabilities.",
"title" : "Music Within",
"highlights" : [{
"path" : "plot",
"score" : 1.9394469261169434,
"texts" : [
{ "type" : "Text", "value" : "The true story of Richard Pimentel, a brilliant public speaker with a troubled past, who returns from Vietnam severely hearing -impaired and finds a " },
{ "type" : "Hit", "value" : "new" },
{ "type" : "Text", "value" : " " },
{ "type" : "Hit", "value" : "purpose" },
{ "type" : "Text", "value" : " in his landmark efforts on the behalf of Americans with disabilities." }]
}]
}
{
"plot" : "The supervillain Megamind finally defeats his nemesis, the
superhero Metro Man. But without a hero, he loses all purpose and
must find new meaning to his life.",
"title" : "Megamind",
"highlights" : [{
"path" : "plot",
"score" : 2.90376877784729,
"texts" : [
{ "type" : "Text", "value" : "But without a hero, he loses all " },
{ "type" : "Hit", "value" : "purpose" },
{ "type" : "Text", "value" : " and must find " },
{ "type" : "Hit", "value" : "new" },
{ "type" : "Text", "value" : " meaning to his life." }]
}]
}
{
"plot" : "An aging Pat Garrett is hired as a lawman on behalf of a
group of wealthy New Mexico cattle barons--his sole purpose being to
bring down his old friend Billy the Kid.",
"title" : "Pat Garrett & Billy the Kid",
"highlights" : [{
"path" : "plot",
"score" : 2.115748405456543,
"texts" : [
{ "type" : "Text", "value" : "An aging Pat Garrett is hired as a lawman on behalf of a group of wealthy " },
{ "type" : "Hit", "value" : "New" },
{ "type" : "Text", "value" : " Mexico cattle barons--his sole " },
{ "type" : "Hit", "value" : "purpose" },
{ "type" : "Text", "value" : " being to bring down his old friend Billy the Kid." }]
}]
}
dotnet run partial-match-example.csproj
{
"plot" : "After the rebels have been brutally overpowered by the
Empire on their newly established base, Luke Skywalker takes
advanced Jedi training with Master Yoda, while his friends are
pursued by Darth Vader as part of his plan to capture Luke.",
"title" : "Star Wars: Episode V - The Empire Strikes Back"
}
{
"plot" : "The new owner of the Cleveland Indians puts together a
purposely horrible team so they'll lose and she can move the team.
But when the plot is uncovered, they start winning just to spite
her.",
"title" : "Major League"
}
{
"plot" : "The true story of Richard Pimentel, a brilliant public
speaker with a troubled past, who returns from Vietnam severely
hearing -impaired and finds a new purpose in his landmark efforts on
the behalf of Americans with disabilities.",
"title" : "Music Within"
}
{
"plot" : "A lonely writer develops an unlikely relationship with his
newly purchased operating system that's designed to meet his every
need.",
"title" : "Her"
}
{
"plot" : "An adrenaline junkie walks away from a whirlwind romance
and embraces a new life as a thief, though he soon finds himself
pursued by veteran police officer and engaged in a turf war with a
local gangster.",
"title" : "Kick"
}
dotnet run partial-match-example.csproj
{
"plot" : "After the rebels have been brutally overpowered by the
Empire on their newly established base, Luke Skywalker takes
advanced Jedi training with Master Yoda, while his friends are
pursued by Darth Vader as part of his plan to capture Luke.",
"title" : "Star Wars: Episode V - The Empire Strikes Back"
}
{
"plot" : "The new owner of the Cleveland Indians puts together a
purposely horrible team so they'll lose and she can move the team.
But when the plot is uncovered, they start winning just to spite
her.",
"title" : "Major League"
}
{
"plot" : "The true story of Richard Pimentel, a brilliant public
speaker with a troubled past, who returns from Vietnam severely
hearing -impaired and finds a new purpose in his landmark efforts on
the behalf of Americans with disabilities.",
"title" : "Music Within"
}
{
"plot" : "A lonely writer develops an unlikely relationship with his
newly purchased operating system that's designed to meet his every
need.",
"title" : "Her"
}
{
"plot" : "An adrenaline junkie walks away from a whirlwind romance
and embraces a new life as a thief, though he soon finds himself
pursued by veteran police officer and engaged in a turf war with a
local gangster.",
"title" : "Kick"
}
1
2

다음 쿼리에서는 연산자를 사용하여 sample_mflix.movies 컬렉션의 plot 필드를 쿼리합니다. 쿼리에는 다음이 포함됩니다.

  • $limit 단계를 사용하여 출력을 5개의 결과로 제한합니다.

  • titleplot를 제외한 모든 필드를 제외하기 위한 $project 단계

이 쿼리를 사용하면 다음을 수행하여 쿼리 문자열 new purchase 필드의 단어와 매칭할 수 있습니다.

  • 단어 new, purchaseplot 필드의 아무 곳에나 표시할 수 있습니다.

  • 쿼리 문자열의 두 문자 변형을 허용하여 쿼리를 필드의 단어와 매칭할 수 있지만 쿼리 문자열의 첫 번째 문자는 변경할 수 없습니다.

  • 최대 256개의 유사한 용어를 고려할 수 있습니다.

1package main
2
3import (
4 "context"
5 "fmt"
6 "time"
7
8 "go.mongodb.org/mongo-driver/bson"
9 "go.mongodb.org/mongo-driver/mongo"
10 "go.mongodb.org/mongo-driver/mongo/options"
11)
12
13// define structure of movies collection
14type MovieCollection struct {
15 title string `bson:"Title,omitempty"`
16 plot string `bson:"Plot,omitempty"`
17}
18
19func main() {
20 var err error
21 // connect to the Atlas cluster
22 ctx := context.Background()
23 client, err := mongo.Connect(ctx, options.Client().ApplyURI("<connection-string>"))
24 if err != nil {
25 panic(err)
26 }
27 defer client.Disconnect(ctx)
28 // set namespace
29 collection := client.Database("sample_mflix").Collection("movies")
30 // define pipeline
31 searchStage := bson.D{{"$search", bson.M{
32 "index": "partial-match-tutorial",
33 "autocomplete": bson.M{
34 "path": "plot", "query": "new purchase", "tokenOrder": "any", "fuzzy": bson.M{
35 "maxEdits": 2, "prefixLength": 1, "maxExpansions": 256},
36 },
37 "highlight": bson.D{
38 {"path", "plot"},
39 },
40 }}}
41 limitStage := bson.D{{"$limit", 5}}
42 projectStage := bson.D{{"$project", bson.D{{"title", 1}, {"plot", 1}, {"_id", 0}, {"highlights", bson.D{{"$meta", "searchHighlights"}}}}}}
43 // specify the amount of time the operation can run on the server
44 opts := options.Aggregate().SetMaxTime(5 * time.Second)
45 // run pipeline
46 cursor, err := collection.Aggregate(ctx, mongo.Pipeline{searchStage, limitStage, projectStage}, opts)
47 if err != nil {
48 panic(err)
49 }
50 // print results
51 var results []bson.D
52 if err = cursor.All(context.TODO(), &results); err != nil {
53 panic(err)
54 }
55 for _, result := range results {
56 fmt.Println(result)
57 }
58}

쿼리는 new ~ purpose 사이의 위치 거리가 최대 5인 쿼리 문자열 new purpose를 지정합니다.

1package main
2
3import (
4 "context"
5 "fmt"
6 "time"
7
8 "go.mongodb.org/mongo-driver/bson"
9 "go.mongodb.org/mongo-driver/mongo"
10 "go.mongodb.org/mongo-driver/mongo/options"
11)
12
13// define structure of movies collection
14type MovieCollection struct {
15 title string `bson:"Title,omitempty"`
16 plot string `bson:"Plot,omitempty"`
17}
18
19func main() {
20 var err error
21 // connect to the Atlas cluster
22 ctx := context.Background()
23 client, err := mongo.Connect(ctx, options.Client().ApplyURI("<connection-string>"))
24 if err != nil {
25 panic(err)
26 }
27 defer client.Disconnect(ctx)
28 // set namespace
29 collection := client.Database("sample_mflix").Collection("movies")
30 // define pipeline
31 searchStage := bson.D{{"$search", bson.M{
32 "index": "partial-match-tutorial",
33 "phrase": bson.D{{"path", "plot"}, {"query", "new purpose"}, {"slop", 5}},
34 "highlight": bson.D{{"path", "plot"}},
35 }}}
36 limitStage := bson.D{{"$limit", 5}}
37 projectStage := bson.D{{"$project", bson.D{{"title", 1}, {"plot", 1}, {"_id", 0}, {"highlights", bson.D{{"$meta", "searchHighlights"}}}}}}
38 // specify the amount of time the operation can run on the server
39 opts := options.Aggregate().SetMaxTime(5 * time.Second)
40 // run pipeline
41 cursor, err := collection.Aggregate(ctx, mongo.Pipeline{searchStage, limitStage, projectStage}, opts)
42 if err != nil {
43 panic(err)
44 }
45 // print results
46 var results []bson.D
47 if err = cursor.All(context.TODO(), &results); err != nil {
48 panic(err)
49 }
50 for _, result := range results {
51 fmt.Println(result)
52 }
53}

이 쿼리는 (.*) 정규 표현식을 사용하여 부분 쿼리 문자열과 임의 수의 문자를 일치시킵니다.

1package main
2
3import (
4 "context"
5 "fmt"
6 "time"
7
8 "go.mongodb.org/mongo-driver/bson"
9 "go.mongodb.org/mongo-driver/mongo"
10 "go.mongodb.org/mongo-driver/mongo/options"
11)
12
13// define structure of movies collection
14type MovieCollection struct {
15 title string `bson:"Title,omitempty"`
16 plot string `bson:"Plot,omitempty"`
17}
18
19func main() {
20 var err error
21 // connect to the Atlas cluster
22 ctx := context.Background()
23 client, err := mongo.Connect(ctx, options.Client().ApplyURI("<connection-string>"))
24 if err != nil {
25 panic(err)
26 }
27 defer client.Disconnect(ctx)
28 // set namespace
29 collection := client.Database("sample_mflix").Collection("movies")
30 // define pipeline
31 searchStage := bson.D{{"$search", bson.M{
32 "index": "partial-match-tutorial",
33 "regex": bson.D{{"path", "plot"}, {"query", "(.*)new(.*) pur(.*)"}},
34 }}}
35 limitStage := bson.D{{"$limit", 5}}
36 projectStage := bson.D{{"$project", bson.D{{"title", 1}, {"plot", 1}, {"_id", 0}}}}
37 // specify the amount of time the operation can run on the server
38 opts := options.Aggregate().SetMaxTime(5 * time.Second)
39 // run pipeline
40 cursor, err := collection.Aggregate(ctx, mongo.Pipeline{searchStage, limitStage, projectStage}, opts)
41 if err != nil {
42 panic(err)
43 }
44 // print results
45 var results []bson.D
46 if err = cursor.All(context.TODO(), &results); err != nil {
47 panic(err)
48 }
49 for _, result := range results {
50 fmt.Println(result)
51 }
52}

쿼리는 쿼리 문자열에서 *을(를) 사용하여 부분 쿼리 문자열에서 지정된 단어 앞과 뒤에 0개 이상의 문자를 일치시킵니다.

1package main
2
3import (
4 "context"
5 "fmt"
6 "time"
7
8 "go.mongodb.org/mongo-driver/bson"
9 "go.mongodb.org/mongo-driver/mongo"
10 "go.mongodb.org/mongo-driver/mongo/options"
11)
12
13// define structure of movies collection
14type MovieCollection struct {
15 title string `bson:"Title,omitempty"`
16 plot string `bson:"Plot,omitempty"`
17}
18
19func main() {
20 var err error
21 // connect to the Atlas cluster
22 ctx := context.Background()
23 client, err := mongo.Connect(ctx, options.Client().ApplyURI("<connection-string>"))
24 if err != nil {
25 panic(err)
26 }
27 defer client.Disconnect(ctx)
28 // set namespace
29 collection := client.Database("sample_mflix").Collection("movies")
30 // define pipeline
31 searchStage := bson.D{{"$search", bson.M{
32 "index": "partial-match-tutorial",
33 "wildcard": bson.D{{"path", "plot"}, {"query", "*new* pur*"}},
34 }}}
35 limitStage := bson.D{{"$limit", 5}}
36 projectStage := bson.D{{"$project", bson.D{{"title", 1}, {"plot", 1}, {"_id", 0}}}}
37 // specify the amount of time the operation can run on the server
38 opts := options.Aggregate().SetMaxTime(5 * time.Second)
39 // run pipeline
40 cursor, err := collection.Aggregate(ctx, mongo.Pipeline{searchStage, limitStage, projectStage}, opts)
41 if err != nil {
42 panic(err)
43 }
44 // print results
45 var results []bson.D
46 if err = cursor.All(context.TODO(), &results); err != nil {
47 panic(err)
48 }
49 for _, result := range results {
50 fmt.Println(result)
51 }
52}

참고

샘플을 실행하기 전에 <connection-string>을 Atlas 연결 문자열로 바꿉니다. 연결 문자열에는 데이터베이스 사용자의 자격 증명이 포함되어 있어야 합니다. 자세한 내용은 드라이버를 통한 연결을 참조하세요.

3
go run partial-match-query.go
[
{plot A divorced woman and her diabetic daughter take refuge in their newly-purchased house's safe room, when three men break-in, searching for a missing fortune.}
{title Panic Room}
{highlights [
[
{score 4.364492893218994}
{path plot}
{texts [
[{value A divorced woman and her diabetic daughter take refuge in their } {type text}]
[{value newly-purchased house's safe} {type hit}]
[{value room, when three men break-in, searching for a missing fortune.} {type text}]
]}
]
]}
]
[
{plot A lonely writer develops an unlikely relationship with his newly purchased operating system that's designed to meet his every need.}
{title Her}
{highlights [
[
{score 4.198050022125244}
{path plot}
{texts [
[{value A lonely writer develops an unlikely relationship with his } {type text}]
[{value newly purchased operating system} {type hit}]
[{value that's designed to meet his every } {type text}]
[{value need} {type hit}]
[{value .} {type text}]
]}
]
]}
]
[
{plot Set in the near future when artificial organs can be bought on credit, it revolves around a man who struggles to make the payments on a heart he has purchased. He must therefore go on the run before said ticker is repossessed.}
{title Repo Men}
{highlights [
[
{score 2.6448397636413574}
{path plot}
{texts [
[{value Set in the } {type text}]
[{value near future when} {type hit}]
[{value artificial organs can be bought on credit, it revolves around a man who struggles to make the payments on a heart he has } {type text}]
[{value purchased. He must} {type hit}]
]}
]
]}
]
[
{plot A psychologically troubled novelty supplier is nudged towards a romance with an English woman, all the while being extorted by a phone-sex line run by a crooked mattress salesman, and purchasing stunning amounts of pudding.}
{title Punch-Drunk Love}
{highlights [
[
{score 1.2451990842819214}
{path plot}
{texts [
[{value A psychologically troubled } {type text}]
[{value novelty supplier is} {type hit}]
[{value } {type text}]
[{value nudged towards a} {type hit}]
[{value romance with an English woman, all the while being extorted by a phone-sex line run by a crooked mattress salesman, and } {type text}]
[{value purchasing stunning amounts} {type hit}]
[{value of pudding.} {type text}]
]}
]
]}
]
[
{plot Jack Conrad is awaiting the death penalty in a corrupt Central American prison. He is "purchased" by a wealthy television producer and taken to a desolate island where he must fight to the death against nine other condemned killers from all corners of the world, with freedom going to the sole survivor.}
{title The Condemned}
{highlights [
[
{score 2.94378924369812}
{path plot}
{texts [
[{value He is "} {type text}]
[{value purchased" by a} {type hit}]
[{value wealthy television producer and taken to a desolate island where he must fight to the death against } {type text}]
[{value nine other condemned} {type hit}]
[{value killers from all corners of the world, with freedom going to the sole survivor.} {type text}]
]}
]
]}
]
go run partial-match-query.go
[
{plot The true story of Richard Pimentel, a brilliant public speaker with a troubled past, who returns from Vietnam severely hearing -impaired and finds a new purpose in his landmark efforts on the behalf of Americans with disabilities.}
{title Music Within}
{highlights [[
{score 1.9394469261169434}
{path plot}
{texts [
[{value The true story of Richard Pimentel, a brilliant public speaker with a troubled past, who returns from Vietnam severely hearing -impaired and finds a } {type text}]
[{value new} {type hit}]
[{value } {type text}]
[{value purpose} {type hit}]
[{value in his landmark efforts on the behalf of Americans with disabilities.} {type text}
]
]}
]]}
]
[
{plot The supervillain Megamind finally defeats his nemesis, the superhero Metro Man. But without a hero, he loses all purpose and must find new meaning to his life.}
{title Megamind}
{highlights [[
{score 2.90376877784729}
{path plot}
{texts [
[{value But without a hero, he loses all } {type text}]
[{value purpose} {type hit}]
[{value and must find } {type text}]
[{value new} {type hit}]
[{value meaning to his life.} {type text}]
]}
]]}
]
[
{plot An aging Pat Garrett is hired as a lawman on behalf of a group of wealthy New Mexico cattle barons--his sole purpose being to bring down his old friend Billy the Kid.}
{title Pat Garrett & Billy the Kid}
{highlights [[
{score 2.115748405456543}
{path plot}
{texts [
[{value An aging Pat Garrett is hired as a lawman on behalf of a group of wealthy } {type text}]
[{value New} {type hit}]
[{value Mexico cattle barons--his sole } {type text}]
[{value purpose} {type hit}]
[{value being to bring down his old friend Billy the Kid.} {type text}]
]}
]]}
]
go run partial-match-query.go
[
{plot After the rebels have been brutally overpowered by the Empire on their newly established base, Luke Skywalker takes advanced Jedi training with Master Yoda, while his friends are pursued by Darth Vader as part of his plan to capture Luke.}
{title Star Wars: Episode V - The Empire Strikes Back}
]
[
{plot The new owner of the Cleveland Indians puts together a purposely horrible team so they'll lose and she can move the team. But when the plot is uncovered, they start winning just to spite her.}
{title Major League}
]
[
{plot The true story of Richard Pimentel, a brilliant public speaker with a troubled past, who returns from Vietnam severely hearing -impaired and finds a new purpose in his landmark efforts on the behalf of Americans with disabilities.}
{title Music Within}
]
[
{plot A lonely writer develops an unlikely relationship with his newly purchased operating system that's designed to meet his every need.}
{title Her}
]
[
{plot An adrenaline junkie walks away from a whirlwind romance and embraces a new life as a thief, though he soon finds himself pursued by veteran police officer and engaged in a turf war with a local gangster.}
{title Kick}
]
go run partial-match-query.go
[
{plot After the rebels have been brutally overpowered by the Empire on their newly established base, Luke Skywalker takes advanced Jedi training with Master Yoda, while his friends are pursued by Darth Vader as part of his plan to capture Luke.}
{title Star Wars: Episode V - The Empire Strikes Back}
]
[
{plot The new owner of the Cleveland Indians puts together a purposely horrible team so they'll lose and she can move the team. But when the plot is uncovered, they start winning just to spite her.}
{title Major League}
]
[
{plot The true story of Richard Pimentel, a brilliant public speaker with a troubled past, who returns from Vietnam severely hearing -impaired and finds a new purpose in his landmark efforts on the behalf of Americans with disabilities.}
{title Music Within}
]
[
{plot A lonely writer develops an unlikely relationship with his newly purchased operating system that's designed to meet his every need.}
{title Her}
]
[
{plot An adrenaline junkie walks away from a whirlwind romance and embraces a new life as a thief, though he soon finds himself pursued by veteran police officer and engaged in a turf war with a local gangster.}
{title Kick}
]
1
junit
4.11 또는 그 이상의 버전
mongodb-driver-sync
4.3.0 또는 그 이상의 버전
slf4j-log4j12
1.7.30 또는 그 이상의 버전
2
3

이 코드 예시에서는 다음 작업을 수행합니다:

  • mongodb 패키지 및 종속성을 가져옵니다.

  • Atlas 클러스터에 대한 연결을 설정합니다.

  • 다음을 사용하는 쿼리를 실행합니다.

    • $search 단계에서 텀 검색

    • $limit 단계를 사용하여 출력을 5개의 결과로 제한합니다.

    • titleplot를 제외한 모든 필드를 제외하기 위한 $project 단계

  • 커서 위를 반복하여 쿼리와 일치하는 문서를 인쇄합니다.

이 쿼리를 사용하면 다음을 수행하여 쿼리 문자열 new purchase 필드의 단어와 매칭할 수 있습니다.

  • 단어 new, purchaseplot 필드의 아무 곳에나 표시할 수 있습니다.

  • 쿼리 문자열의 두 문자 변형을 허용하여 쿼리를 필드의 단어와 매칭할 수 있지만 쿼리 문자열의 첫 번째 문자는 변경할 수 없습니다.

  • 최대 256개의 유사한 용어를 고려할 수 있습니다.

1import java.util.Arrays;
2import static com.mongodb.client.model.Aggregates.limit;
3import static com.mongodb.client.model.Aggregates.project;
4import static com.mongodb.client.model.Projections.excludeId;
5import static com.mongodb.client.model.Projections.fields;
6import static com.mongodb.client.model.Projections.include;
7import com.mongodb.client.MongoClient;
8import com.mongodb.client.MongoClients;
9import com.mongodb.client.MongoCollection;
10import com.mongodb.client.MongoDatabase;
11import org.bson.Document;
12
13public class PartialMatchQuery {
14 public static void main( String[] args ) {
15
16 // define query
17 Document agg = new Document("$search",
18 new Document ("index", "partial-match-tutorial")
19 .append("autocomplete",
20 new Document("path", "plot")
21 .append("query", "new purchase")
22 .append("tokenOrder", "any")
23 .append("fuzzy",
24 new Document("maxEdits", 2)
25 .append("prefixLength", 1)
26 .append("maxExpansions", 256))));
27
28 // specify connection
29 String uri = "<connection-string>";
30
31 // establish connection and set namespace
32 try (MongoClient mongoClient = MongoClients.create(uri)) {
33 MongoDatabase database = mongoClient.getDatabase("sample_mflix");
34 MongoCollection<Document> collection = database.getCollection("movies");
35 // run query and print results
36 collection.aggregate(Arrays.asList(agg,
37 limit(5),
38 project(fields(excludeId(), include("title", "plot")))))
39 .forEach(doc -> System.out.println(doc.toJson()));
40 }
41 }
42}

쿼리는 new ~ purpose 사이의 위치 거리가 최대 5인 쿼리 문자열 new purpose를 지정합니다.

1import java.util.Arrays;
2
3import static com.mongodb.client.model.Aggregates.limit;
4import static com.mongodb.client.model.Aggregates.project;
5import static com.mongodb.client.model.Projections.excludeId;
6import static com.mongodb.client.model.Projections.fields;
7import static com.mongodb.client.model.Projections.include;
8import com.mongodb.client.MongoClient;
9import com.mongodb.client.MongoClients;
10import com.mongodb.client.MongoCollection;
11import com.mongodb.client.MongoDatabase;
12import org.bson.Document;
13
14public class PartialMatchQuery {
15 public static void main( String[] args ) {
16 // define query
17 Document agg = new Document("$search",
18 new Document("index", "partial-match-tutorial")
19 .append("phrase",
20 new Document("path", "plot")
21 .append("query", "new purpose")
22 .append("slop", 5)));
23
24 // specify connection
25 String uri = "<connection-string>";
26
27 // establish connection and set namespace
28 try (MongoClient mongoClient = MongoClients.create(uri)) {
29 MongoDatabase database = mongoClient.getDatabase("sample_mflix");
30 MongoCollection<Document> collection = database.getCollection("movies");
31
32 // run query and print results
33 collection.aggregate(Arrays.asList(agg,
34 limit(5),
35 project(fields(excludeId(), include("title", "plot")))))
36 .forEach(doc -> System.out.println(doc.toJson()));
37 }
38 }
39}

이 쿼리는 (.*) 정규 표현식을 사용하여 부분 쿼리 문자열과 임의 수의 문자를 일치시킵니다.

1import java.util.Arrays;
2
3import static com.mongodb.client.model.Aggregates.limit;
4import static com.mongodb.client.model.Aggregates.project;
5import static com.mongodb.client.model.Projections.excludeId;
6import static com.mongodb.client.model.Projections.fields;
7import static com.mongodb.client.model.Projections.include;
8import com.mongodb.client.MongoClient;
9import com.mongodb.client.MongoClients;
10import com.mongodb.client.MongoCollection;
11import com.mongodb.client.MongoDatabase;
12import org.bson.Document;
13
14public class PartialMatchQuery {
15 public static void main( String[] args ) {
16 // define query
17 Document agg = new Document("$search",
18 new Document ("index", "partial-match-tutorial")
19 .append("regex",
20 new Document("path", "plot")
21 .append("query", "(.*)new(.*) pur(.*)")));
22 // specify connection
23 String uri = "<connection-string>";
24 // establish connection and set namespace
25 try (MongoClient mongoClient = MongoClients.create(uri)) {
26 MongoDatabase database = mongoClient.getDatabase("sample_mflix");
27 MongoCollection<Document> collection = database.getCollection("movies");
28 // run query and print results
29 collection.aggregate(Arrays.asList(agg,
30 limit(5),
31 project(fields(excludeId(), include("title", "plot")))))
32 .forEach(doc -> System.out.println(doc.toJson()));
33 }
34 }
35}

쿼리는 쿼리 문자열에서 *을(를) 사용하여 부분 쿼리 문자열에서 지정된 단어 앞과 뒤에 0개 이상의 문자를 일치시킵니다.

1import java.util.Arrays;
2
3import static com.mongodb.client.model.Aggregates.limit;
4import static com.mongodb.client.model.Aggregates.project;
5import static com.mongodb.client.model.Projections.excludeId;
6import static com.mongodb.client.model.Projections.fields;
7import static com.mongodb.client.model.Projections.include;
8import com.mongodb.client.MongoClient;
9import com.mongodb.client.MongoClients;
10import com.mongodb.client.MongoCollection;
11import com.mongodb.client.MongoDatabase;
12import org.bson.Document;
13
14public class PartialMatchQuery {
15 public static void main( String[] args ) {
16 // define query
17 Document agg = new Document("$search",
18 new Document ("index", "partial-match-tutorial")
19 .append("wildcard",
20 new Document("path", "plot")
21 .append("query", "*new* pur*")));
22 // specify connection
23 String uri = "<connection-string>";
24 // establish connection and set namespace
25 try (MongoClient mongoClient = MongoClients.create(uri)) {
26 MongoDatabase database = mongoClient.getDatabase("sample_mflix");
27 MongoCollection<Document> collection = database.getCollection("movies");
28 // run query and print results
29 collection.aggregate(Arrays.asList(agg,
30 limit(5),
31 project(fields(excludeId(), include("title", "plot")))))
32 .forEach(doc -> System.out.println(doc.toJson()));
33 }
34 }
35}

참고

Maven 환경에서 샘플 코드를 실행하려면 파일의 가져오기 문 위에 다음을 추가하세요.

package com.mongodb.drivers;

샘플을 실행하기 전에 <connection-string>을 Atlas 연결 문자열로 바꿉니다. 연결 문자열에는 데이터베이스 사용자의 자격 증명이 포함되어 있어야 합니다. 자세한 내용은 드라이버를 통한 연결을 참조하세요.

4
javac PartialMatchQuery.java
java PartialMatchQuery
{
"plot": "A divorced woman and her diabetic daughter take refuge in their newly-purchased house's safe room, when three men break-in, searching for a missing fortune.",
"title": "Panic Room"
}
{
"plot": "A lonely writer develops an unlikely relationship with his newly purchased operating system that's designed to meet his every need.",
"title": "Her"
}
{
"plot": "Set in the near future when artificial organs can be bought on credit, it revolves around a man who struggles to make the payments on a heart he has purchased. He must therefore go on the run before said ticker is repossessed.",
"title": "Repo Men"
}
{
"plot": "A psychologically troubled novelty supplier is nudged towards a romance with an English woman, all the while being extorted by a phone-sex line run by a crooked mattress salesman, and purchasing stunning amounts of pudding.",
"title": "Punch-Drunk Love"
}
{
"plot": "Jack Conrad is awaiting the death penalty in a corrupt Central American prison. He is \"purchased\" by a wealthy television producer and taken to a desolate island where he must fight to the death against nine other condemned killers from all corners of the world, with freedom going to the sole survivor.",
"title": "The Condemned"
}
javac PartialMatchQuery.java
java PartialMatchQuery
{
"plot": "The true story of Richard Pimentel, a brilliant public speaker with a troubled past, who returns from Vietnam severely hearing -impaired and finds a new purpose in his landmark efforts on the behalf of Americans with disabilities.",
"title": "Music Within"
}
{
"plot": "The supervillain Megamind finally defeats his nemesis, the superhero Metro Man. But without a hero, he loses all purpose and must find new meaning to his life.",
"title": "Megamind"
}
{
"plot": "An aging Pat Garrett is hired as a lawman on behalf of a group of wealthy New Mexico cattle barons--his sole purpose being to bring down his old friend Billy the Kid.",
"title": "Pat Garrett & Billy the Kid"
}
javac PartialMatchQuery.java
java PartialMatchQuery
{
"plot": "After the rebels have been brutally overpowered by the Empire on their newly established base, Luke Skywalker takes advanced Jedi training with Master Yoda, while his friends are pursued by Darth Vader as part of his plan to capture Luke.",
"title": "Star Wars: Episode V - The Empire Strikes Back"
}
{
"plot": "The new owner of the Cleveland Indians puts together a purposely horrible team so they'll lose and she can move the team. But when the plot is uncovered, they start winning just to spite her.",
"title": "Major League"
}
{
"plot": "The true story of Richard Pimentel, a brilliant public speaker with a troubled past, who returns from Vietnam severely hearing -impaired and finds a new purpose in his landmark efforts on the behalf of Americans with disabilities.",
"title": "Music Within"
}
{
"plot": "A lonely writer develops an unlikely relationship with his newly purchased operating system that's designed to meet his every need.",
"title": "Her"
}
{
"plot": "An adrenaline junkie walks away from a whirlwind romance and embraces a new life as a thief, though he soon finds himself pursued by veteran police officer and engaged in a turf war with a local gangster.",
"title": "Kick"
}
javac PartialMatchQuery.java
java PartialMatchQuery
{
"plot": "After the rebels have been brutally overpowered by the Empire on their newly established base, Luke Skywalker takes advanced Jedi training with Master Yoda, while his friends are pursued by Darth Vader as part of his plan to capture Luke.",
"title": "Star Wars: Episode V - The Empire Strikes Back"
}
{
"plot": "The new owner of the Cleveland Indians puts together a purposely horrible team so they'll lose and she can move the team. But when the plot is uncovered, they start winning just to spite her.",
"title": "Major League"
}
{
"plot": "The true story of Richard Pimentel, a brilliant public speaker with a troubled past, who returns from Vietnam severely hearing -impaired and finds a new purpose in his landmark efforts on the behalf of Americans with disabilities.",
"title": "Music Within"
}
{
"plot": "A lonely writer develops an unlikely relationship with his newly purchased operating system that's designed to meet his every need.",
"title": "Her"
}
{
"plot": "An adrenaline junkie walks away from a whirlwind romance and embraces a new life as a thief, though he soon finds himself pursued by veteran police officer and engaged in a turf war with a local gangster.",
"title": "Kick"
}
1
mongodb-driver-kotlin-coroutine
4.10.0 또는 그 이상의 버전
2
3

이 코드 예시에서는 다음 작업을 수행합니다:

  • mongodb 패키지 및 종속성을 가져옵니다.

  • Atlas 클러스터에 대한 연결을 설정합니다.

  • 다음을 사용하는 쿼리를 실행합니다.

    • $search 단계에서 텀 검색

    • $limit 단계를 사용하여 출력을 5개의 결과로 제한합니다.

    • titleplot를 제외한 모든 필드를 제외하기 위한 $project 단계

  • AggregateFlow 인스턴스에서 쿼리와 일치하는 문서를 인쇄합니다.

이 쿼리를 사용하면 다음을 수행하여 쿼리 문자열 new purchase 필드의 단어와 매칭할 수 있습니다.

  • 단어 new, purchaseplot 필드의 아무 곳에나 표시할 수 있습니다.

  • 쿼리 문자열의 두 문자 변형을 허용하여 쿼리를 필드의 단어와 매칭할 수 있지만 쿼리 문자열의 첫 번째 문자는 변경할 수 없습니다.

  • 최대 256개의 유사한 용어를 고려할 수 있습니다.

1import com.mongodb.client.model.Aggregates.limit
2import com.mongodb.client.model.Aggregates.project
3import com.mongodb.client.model.Projections.*
4import com.mongodb.kotlin.client.coroutine.MongoClient
5import kotlinx.coroutines.runBlocking
6import org.bson.Document
7
8fun main() {
9 // establish connection and set namespace
10 val uri = "<connection-string>"
11 val mongoClient = MongoClient.create(uri)
12 val database = mongoClient.getDatabase("sample_mflix")
13 val collection = database.getCollection<Document>("movies")
14
15 runBlocking {
16 // define query
17 val agg = Document(
18 "\$search",
19 Document("index", "partial-match-tutorial")
20 .append(
21 "autocomplete",
22 Document("path", "plot")
23 .append("query", "new purchase")
24 .append("tokenOrder", "any")
25 .append(
26 "fuzzy",
27 Document("maxEdits", 2)
28 .append("prefixLength", 1)
29 .append("maxExpansions", 256)
30 )
31 )
32 )
33
34 // run query and print results
35 val resultsFlow = collection.aggregate<Document>(
36 listOf(
37 agg,
38 limit(5),
39 project(fields(excludeId(), include("title", "plot")))
40 )
41 )
42 resultsFlow.collect { println(it) }
43 }
44 mongoClient.close()
45}

쿼리는 new ~ purpose 사이의 위치 거리가 최대 5인 쿼리 문자열 new purpose를 지정합니다.

1import com.mongodb.client.model.Aggregates.limit
2import com.mongodb.client.model.Aggregates.project
3import com.mongodb.client.model.Projections.*
4import com.mongodb.kotlin.client.coroutine.MongoClient
5import kotlinx.coroutines.runBlocking
6import org.bson.Document
7
8fun main() {
9 // establish connection and set namespace
10 val uri = "<connection-string>"
11 val mongoClient = MongoClient.create(uri)
12 val database = mongoClient.getDatabase("sample_mflix")
13 val collection = database.getCollection<Document>("movies")
14
15 runBlocking {
16 // define query
17 val agg = Document(
18 "\$search",
19 Document("index", "partial-match-tutorial")
20 .append(
21 "phrase",
22 Document("path", "plot")
23 .append("query", "new purpose")
24 .append("slop", 5)
25 )
26 )
27
28 // run query and print results
29 val resultsFlow = collection.aggregate<Document>(
30 listOf(
31 agg,
32 limit(5),
33 project(fields(excludeId(), include("title", "plot")))
34 )
35 )
36 resultsFlow.collect { println(it) }
37 }
38 mongoClient.close()
39}

이 쿼리는 (.*) 정규 표현식을 사용하여 부분 쿼리 문자열과 임의 수의 문자를 일치시킵니다.

1import com.mongodb.client.model.Aggregates.limit
2import com.mongodb.client.model.Aggregates.project
3import com.mongodb.client.model.Projections.*
4import com.mongodb.kotlin.client.coroutine.MongoClient
5import kotlinx.coroutines.runBlocking
6import org.bson.Document
7
8fun main() {
9 // establish connection and set namespace
10 val uri = "<connection-string>"
11 val mongoClient = MongoClient.create(uri)
12 val database = mongoClient.getDatabase("sample_mflix")
13 val collection = database.getCollection<Document>("movies")
14
15 runBlocking {
16 // define query
17 val agg = Document(
18 "\$search",
19 Document("index", "partial-match-tutorial")
20 .append(
21 "regex",
22 Document("path", "plot")
23 .append("query", "(.*)new(.*) pur(.*)")
24 )
25 )
26
27 // run query and print results
28 val resultsFlow = collection.aggregate<Document>(
29 listOf(
30 agg,
31 limit(5),
32 project(fields(excludeId(), include("title", "plot")))
33 )
34 )
35 resultsFlow.collect { println(it) }
36 }
37 mongoClient.close()
38}

쿼리는 쿼리 문자열에서 *을(를) 사용하여 부분 쿼리 문자열에서 지정된 단어 앞과 뒤에 0개 이상의 문자를 일치시킵니다.

1import com.mongodb.client.model.Aggregates.limit
2import com.mongodb.client.model.Aggregates.project
3import com.mongodb.client.model.Projections.*
4import com.mongodb.kotlin.client.coroutine.MongoClient
5import kotlinx.coroutines.runBlocking
6import org.bson.Document
7
8fun main() {
9 // establish connection and set namespace
10 val uri = "<connection-string>"
11 val mongoClient = MongoClient.create(uri)
12 val database = mongoClient.getDatabase("sample_mflix")
13 val collection = database.getCollection<Document>("movies")
14
15 runBlocking {
16 // define query
17 val agg = Document(
18 "\$search",
19 Document("index", "partial-match-tutorial")
20 .append(
21 "wildcard",
22 Document("path", "plot")
23 .append("query", "*new* pur*")
24 )
25 )
26
27 // run query and print results
28 val resultsFlow = collection.aggregate<Document>(
29 listOf(
30 agg,
31 limit(5),
32 project(fields(excludeId(), include("title", "plot")))
33 )
34 )
35 resultsFlow.collect { println(it) }
36 }
37 mongoClient.close()
38}

샘플을 실행하기 전에 <connection-string>을 Atlas 연결 문자열로 바꿉니다. 연결 문자열에는 데이터베이스 사용자의 자격 증명이 포함되어 있어야 합니다. 자세한 내용은 드라이버를 통한 연결을 참조하세요.

4

IDE에서 PartialMatchQuery.kt 프로그램을 실행하면 다음 문서가 인쇄됩니다.

Document{{plot=A divorced woman and her diabetic daughter take refuge in their newly-purchased house's safe room, when three men break-in, searching for a missing fortune., title=Panic Room}}
Document{{plot=A lonely writer develops an unlikely relationship with his newly purchased operating system that's designed to meet his every need., title=Her}}
Document{{plot=A country boy becomes the head of a gang through the purchase of some lucky roses from an old lady. He and a singer at the gang's nightclub try to do a good deed for the old lady when her daughter comes to visit., title=Miracles - Mr. Canton and Lady Rose}}
Document{{plot=A psychologically troubled novelty supplier is nudged towards a romance with an English woman, all the while being extorted by a phone-sex line run by a crooked mattress salesman, and purchasing stunning amounts of pudding., title=Punch-Drunk Love}}
Document{{plot=Jack Conrad is awaiting the death penalty in a corrupt Central American prison. He is "purchased" by a wealthy television producer and taken to a desolate island where he must fight to the death against nine other condemned killers from all corners of the world, with freedom going to the sole survivor., title=The Condemned}}
Document{{plot=The true story of Richard Pimentel, a brilliant public speaker with a troubled past, who returns from Vietnam severely hearing -impaired and finds a new purpose in his landmark efforts on the behalf of Americans with disabilities., title=Music Within}}
Document{{plot=The supervillain Megamind finally defeats his nemesis, the superhero Metro Man. But without a hero, he loses all purpose and must find new meaning to his life., title=Megamind}}
Document{{plot=An aging Pat Garrett is hired as a lawman on behalf of a group of wealthy New Mexico cattle barons--his sole purpose being to bring down his old friend Billy the Kid., title=Pat Garrett & Billy the Kid}}
Document{{plot=After the rebels have been brutally overpowered by the Empire on their newly established base, Luke Skywalker takes advanced Jedi training with Master Yoda, while his friends are pursued by Darth Vader as part of his plan to capture Luke., title=Star Wars: Episode V - The Empire Strikes Back}}
Document{{plot=The new owner of the Cleveland Indians puts together a purposely horrible team so they'll lose and she can move the team. But when the plot is uncovered, they start winning just to spite her., title=Major League}}
Document{{plot=The true story of Richard Pimentel, a brilliant public speaker with a troubled past, who returns from Vietnam severely hearing -impaired and finds a new purpose in his landmark efforts on the behalf of Americans with disabilities., title=Music Within}}
Document{{plot=A lonely writer develops an unlikely relationship with his newly purchased operating system that's designed to meet his every need., title=Her}}
Document{{plot=An adrenaline junkie walks away from a whirlwind romance and embraces a new life as a thief, though he soon finds himself pursued by veteran police officer and engaged in a turf war with a local gangster., title=Kick}}
Document{{plot=After the rebels have been brutally overpowered by the Empire on their newly established base, Luke Skywalker takes advanced Jedi training with Master Yoda, while his friends are pursued by Darth Vader as part of his plan to capture Luke., title=Star Wars: Episode V - The Empire Strikes Back}}
Document{{plot=The new owner of the Cleveland Indians puts together a purposely horrible team so they'll lose and she can move the team. But when the plot is uncovered, they start winning just to spite her., title=Major League}}
Document{{plot=The true story of Richard Pimentel, a brilliant public speaker with a troubled past, who returns from Vietnam severely hearing -impaired and finds a new purpose in his landmark efforts on the behalf of Americans with disabilities., title=Music Within}}
Document{{plot=A lonely writer develops an unlikely relationship with his newly purchased operating system that's designed to meet his every need., title=Her}}
Document{{plot=An adrenaline junkie walks away from a whirlwind romance and embraces a new life as a thief, though he soon finds himself pursued by veteran police officer and engaged in a turf war with a local gangster., title=Kick}}
1
2

이 코드 예시에서는 다음 작업을 수행합니다:

  • MongoDB의 Node.js 드라이버인 mongodb를 가져옵니다.

  • MongoClient 클래스의 인스턴스를 만들어 Atlas 클러스터에 대한 연결을 설정합니다.

  • 다음을 사용하는 쿼리를 실행합니다.

    • $search 단계에서 텀 검색

    • $limit 단계를 사용하여 출력을 5개의 결과로 제한합니다.

    • titleplot를 제외한 모든 필드를 제외하기 위한 $project 단계

  • 커서 위를 반복하여 쿼리와 일치하는 문서를 인쇄합니다.

이 쿼리를 사용하면 다음을 수행하여 쿼리 문자열 new purchase 필드의 단어와 매칭할 수 있습니다.

  • 단어 new, purchaseplot 필드의 아무 곳에나 표시할 수 있습니다.

  • 쿼리 문자열의 두 문자 변형을 허용하여 쿼리를 필드의 단어와 매칭할 수 있지만 쿼리 문자열의 첫 번째 문자는 변경할 수 없습니다.

  • 최대 256개의 유사한 용어를 고려할 수 있습니다.

1const MongoClient = require("mongodb").MongoClient;
2const assert = require("assert");
3
4const agg = [
5 {
6 '$search': {
7 'index': 'partial-match-tutorial',
8 'autocomplete': {
9 'path': 'plot',
10 'query': 'new purchase',
11 'tokenOrder': 'any',
12 'fuzzy': {
13 'maxEdits': 2,
14 'prefixLength': 1,
15 'maxExpansions': 256
16 }
17 },
18 'highlight': {
19 'path': 'plot'
20 }
21 }
22 }, {
23 '$limit': 5
24 }, {
25 '$project': {
26 '_id': 0,
27 'title': 1,
28 'plot': 1,
29 'highlights': {
30 '$meta': 'searchHighlights'
31 }
32 }
33 }
34];
35
36MongoClient.connect(
37 "<connection-string>",
38 { useNewUrlParser: true, useUnifiedTopology: true },
39 async function (connectErr, client) {
40 assert.equal(null, connectErr);
41 const coll = client.db("sample_mflix").collection("movies");
42 let cursor = await coll.aggregate(agg);
43 await cursor.forEach((doc) => console.log(doc));
44 client.close();
45 }
46);

쿼리는 new ~ purpose 사이의 위치 거리가 최대 5인 쿼리 문자열 new purpose를 지정합니다.

1const MongoClient = require("mongodb").MongoClient;
2const assert = require("assert");
3
4const agg = [
5 {
6 '$search': {
7 'index': 'partial-match-tutorial',
8 'phrase': {
9 'path': 'plot',
10 'query': 'new purpose',
11 'slop': 5
12 },
13 'highlight': {
14 'path': 'plot'
15 }
16 }
17 }, {
18 '$limit': 5
19 }, {
20 '$project': {
21 '_id': 0,
22 'plot': 1,
23 'title': 1,
24 'highlights': {
25 '$meta': 'searchHighlights'
26 }
27 }
28 }
29];
30
31MongoClient.connect(
32 "<connection-string>",
33 { useNewUrlParser: true, useUnifiedTopology: true },
34 async function (connectErr, client) {
35 assert.equal(null, connectErr);
36 const coll = client.db("sample_mflix").collection("movies");
37 let cursor = await coll.aggregate(agg);
38 await cursor.forEach((doc) => console.log(doc));
39 client.close();
40 }
41);

이 쿼리는 (.*) 정규 표현식을 사용하여 부분 쿼리 문자열과 임의 수의 문자를 일치시킵니다.

1const MongoClient = require("mongodb").MongoClient;
2const assert = require("assert");
3
4const agg = [
5 {
6 '$search': {
7 'index': 'partial-match-tutorial',
8 'regex': {
9 'path': 'plot',
10 'query': '(.*)new(.*) pur(.*)'
11 }
12 }
13 }, {
14 '$limit': 5
15 }, {
16 '$project': {
17 '_id': 0,
18 'plot': 1,
19 'title': 1
20 }
21 }
22];
23
24MongoClient.connect(
25 "<connection-string>",
26 { useNewUrlParser: true, useUnifiedTopology: true },
27 async function (connectErr, client) {
28 assert.equal(null, connectErr);
29 const coll = client.db("sample_mflix").collection("movies");
30 let cursor = await coll.aggregate(agg);
31 await cursor.forEach((doc) => console.log(doc));
32 client.close();
33 }
34);

쿼리는 쿼리 문자열에서 *을(를) 사용하여 부분 쿼리 문자열에서 지정된 단어 앞과 뒤에 0개 이상의 문자를 일치시킵니다.

1const MongoClient = require("mongodb").MongoClient;
2const assert = require("assert");
3
4const agg = [
5 {
6 '$search': {
7 'index': 'partial-match-tutorial',
8 'wildcard': {
9 'path': 'plot',
10 'query': '*new* pur*'
11 }
12 }
13 }, {
14 '$limit': 5
15 }, {
16 '$project': {
17 '_id': 0,
18 'plot': 1,
19 'title': 1
20 }
21 }
22];
23
24MongoClient.connect(
25 "<connection-string>",
26 { useNewUrlParser: true, useUnifiedTopology: true },
27 async function (connectErr, client) {
28 assert.equal(null, connectErr);
29 const coll = client.db("sample_mflix").collection("movies");
30 let cursor = await coll.aggregate(agg);
31 await cursor.forEach((doc) => console.log(doc));
32 client.close();
33 }
34);

참고

샘플을 실행하기 전에 <connection-string>을 Atlas 연결 문자열로 바꿉니다. 연결 문자열에는 데이터베이스 사용자의 자격 증명이 포함되어 있어야 합니다. 자세한 내용은 드라이버를 통한 연결을 참조하세요.

3
node partial-match-query.js
{
plot: "A divorced woman and her diabetic daughter take refuge in their newly-purchased house's safe room, when three men break-in, searching for a missing fortune.",
title: 'Panic Room',
highlights: [ { score: 4.364492893218994, path: 'plot', texts: [Array] } ]
}
{
plot: "A lonely writer develops an unlikely relationship with his newly purchased operating system that's designed to meet his every need.",
title: 'Her',
highlights: [ { score: 4.198050022125244, path: 'plot', texts: [Array] } ]
}
{
plot: "Set in the near future when artificial organs can be bought on credit, it revolves around a man who struggles to make the payments on a heart he has purchased. He must therefore go on the run before said ticker is repossessed.",
title: 'Repo Men',
highlights: [
{ score: 2.6448397636413574, path: 'plot', texts: [Array] }
]
}
{
plot: 'A psychologically troubled novelty supplier is nudged towards a romance with an English woman, all the while being extorted by a phone-sex line run by a crooked mattress salesman, and purchasing stunning amounts of pudding.',
title: 'Punch-Drunk Love',
highlights: [ { score: 1.2451990842819214, path: 'plot', texts: [Array] } ]
}
{
plot: 'Jack Conrad is awaiting the death penalty in a corrupt Central American prison. He is "purchased" by a wealthy television producer and taken to a desolate island where he must fight to the death against nine other condemned killers from all corners of the world, with freedom going to the sole survivor.',
title: 'The Condemned',
highlights: [ { score: 2.94378924369812, path: 'plot', texts: [Array] } ]
}
node partial-match-query.js
{
plot: 'The true story of Richard Pimentel, a brilliant public speaker with a troubled past, who returns from Vietnam severely hearing -impaired and finds a new purpose in his landmark efforts on the behalf of Americans with disabilities.',
title: 'Music Within',
highlights: [ { score: 1.9394469261169434, path: 'plot', texts: [Array] } ]
}
{
plot: 'The supervillain Megamind finally defeats his nemesis, the superhero Metro Man. But without a hero, he loses all purpose and must find new meaning to his life.',
title: 'Megamind',
highlights: [ { score: 2.90376877784729, path: 'plot', texts: [Array] } ]
}
{
plot: 'An aging Pat Garrett is hired as a lawman on behalf of a group of wealthy New Mexico cattle barons--his sole purpose being to bring down his old friend Billy the Kid.',
title: 'Pat Garrett & Billy the Kid',
highlights: [ { score: 2.115748405456543, path: 'plot', texts: [Array] } ]
}
node partial-match-query.js
{
plot: 'After the rebels have been brutally overpowered by the Empire on their newly established base, Luke Skywalker takes advanced Jedi training with Master Yoda, while his friends are pursued by Darth Vader as part of his plan to capture Luke.',
title: 'Star Wars: Episode V - The Empire Strikes Back'
}
{
plot: "The new owner of the Cleveland Indians puts together a purposely horrible team so they'll lose and she can move the team. But when the plot is uncovered, they start winning just to spite her.",
title: 'Major League'
}
{
plot: 'The true story of Richard Pimentel, a brilliant public speaker with a troubled past, who returns from Vietnam severely hearing -impaired and finds a new purpose in his landmark efforts on the behalf of Americans with disabilities.',
title: 'Music Within'
}
{
plot: "A lonely writer develops an unlikely relationship with his newly purchased operating system that's designed to meet his every need.",
title: 'Her'
}
{
plot: 'An adrenaline junkie walks away from a whirlwind romance and embraces a new life as a thief, though he soon finds himself pursued by veteran police officer and engaged in a turf war with a local gangster.',
title: 'Kick'
}
node partial-match-query.js
{
plot: 'After the rebels have been brutally overpowered by the Empire on their newly established base, Luke Skywalker takes advanced Jedi training with Master Yoda, while his friends are pursued by Darth Vader as part of his plan to capture Luke.',
title: 'Star Wars: Episode V - The Empire Strikes Back'
}
{
plot: "The new owner of the Cleveland Indians puts together a purposely horrible team so they'll lose and she can move the team. But when the plot is uncovered, they start winning just to spite her.",
title: 'Major League'
}
{
plot: 'The true story of Richard Pimentel, a brilliant public speaker with a troubled past, who returns from Vietnam severely hearing -impaired and finds a new purpose in his landmark efforts on the behalf of Americans with disabilities.',
title: 'Music Within'
}
{
plot: "A lonely writer develops an unlikely relationship with his newly purchased operating system that's designed to meet his every need.",
title: 'Her'
}
{
plot: 'An adrenaline junkie walks away from a whirlwind romance and embraces a new life as a thief, though he soon finds himself pursued by veteran police officer and engaged in a turf war with a local gangster.',
title: 'Kick'
}
1
2

다음 코드 예제에서는:

  • pymongo, MongoDB의 Python 드라이버 및 dns 모듈을 가져옵니다. 이 모듈은 DNS 시드 리스트 연결 문자열을 사용하여 pymongoAtlas에 연결하는 데 필요합니다.

  • MongoClient 클래스의 인스턴스를 만들어 Atlas 클러스터에 대한 연결을 설정합니다.

  • 다음을 사용하는 쿼리를 실행합니다.

    • $search 단계에서 텀 검색

    • $limit 단계를 사용하여 출력을 5개의 결과로 제한합니다.

    • titleplot를 제외한 모든 필드를 제외하기 위한 $project 단계

  • 커서 위를 반복하여 쿼리와 일치하는 문서를 인쇄합니다.

이 쿼리를 사용하면 다음을 수행하여 쿼리 문자열 new purchase 필드의 단어와 매칭할 수 있습니다.

  • 단어 new, purchaseplot 필드의 아무 곳에나 표시할 수 있습니다.

  • 쿼리 문자열의 두 문자 변형을 허용하여 쿼리를 필드의 단어와 매칭할 수 있지만 쿼리 문자열의 첫 번째 문자는 변경할 수 없습니다.

  • 최대 256개의 유사한 용어를 고려할 수 있습니다.

1import pymongo
2import dns
3
4client = pymongo.MongoClient('<connection-string>')
5result = client['sample_mflix']['movies'].aggregate([
6 {
7 '$search': {
8 'index': 'partial-match-tutorial',
9 'autocomplete': {
10 'path': 'plot',
11 'query': 'new purchase',
12 'tokenOrder': 'any',
13 'fuzzy': {
14 'maxEdits': 2,
15 'prefixLength': 1,
16 'maxExpansions': 256
17 }
18 },
19 'highlight': {
20 'path': 'plot'
21 }
22 }
23 }, {
24 '$limit': 5
25 }, {
26 '$project': {
27 '_id': 0,
28 'title': 1,
29 'plot': 1,
30 'highlights': {
31 '$meta': 'searchHighlights'
32 }
33 }
34 }
35])
36
37for i in result:
38 print(i)

쿼리는 new ~ purpose 사이의 위치 거리가 최대 5인 쿼리 문자열 new purpose를 지정합니다.

1import pymongo
2import dns
3
4client = pymongo.MongoClient('<connection-string>')
5result = client['sample_mflix']['movies'].aggregate([
6 {
7 '$search': {
8 'index': 'partial-match-tutorial',
9 'phrase': {
10 'path': 'plot',
11 'query': 'new purpose',
12 'slop': 5
13 },
14 'highlight': {
15 'path': 'plot'
16 }
17 }
18 }, {
19 '$limit': 5
20 }, {
21 '$project': {
22 '_id': 0,
23 'plot': 1,
24 'title': 1,
25 'highlights': {
26 '$meta': 'searchHighlights'
27 }
28 }
29 }
30])
31
32for i in result:
33 print(i)

이 쿼리는 (.*) 정규 표현식을 사용하여 부분 쿼리 문자열과 임의 수의 문자를 일치시킵니다.

1import pymongo
2import dns
3
4client = pymongo.MongoClient('<connection-string>')
5result = client['sample_mflix']['movies'].aggregate([
6 {
7 '$search': {
8 'index': 'partial-match-tutorial',
9 'regex': {
10 'path': 'plot',
11 'query': '(.*)new(.*) pur(.*)'
12 }
13 }
14 }, {
15 '$limit': 5
16 }, {
17 '$project': {
18 '_id': 0,
19 'plot': 1,
20 'title': 1
21 }
22 }
23])
24
25for i in result:
26 print(i)

쿼리는 쿼리 문자열에서 *을(를) 사용하여 부분 쿼리 문자열에서 지정된 단어 앞과 뒤에 0개 이상의 문자를 일치시킵니다.

1import pymongo
2import dns
3
4client = pymongo.MongoClient('<connection-string>')
5result = client['sample_mflix']['movies'].aggregate([
6 {
7 '$search': {
8 'index': 'partial-match-tutorial',
9 'wildcard': {
10 'path': 'plot',
11 'query': '*new* pur*'
12 }
13 }
14 }, {
15 '$limit': 5
16 }, {
17 '$project': {
18 '_id': 0,
19 'plot': 1,
20 'title': 1
21 }
22 }
23])
24
25for i in result:
26 print(i)

참고

샘플을 실행하기 전에 <connection-string>을 Atlas 연결 문자열로 바꿉니다. 연결 문자열에는 데이터베이스 사용자의 자격 증명이 포함되어 있어야 합니다. 자세한 내용은 드라이버를 통한 연결을 참조하세요.

3
python partial-match-query.py
{
'plot': "A divorced woman and her diabetic daughter take refuge in their newly-purchased house's safe room, when three men break-in, searching for a missing fortune.",
'title': 'Panic Room',
'highlights': [{
'score': 4.364492893218994,
'path': 'plot',
'texts': [
{'value': 'A divorced woman and her diabetic daughter take refuge in their ', 'type': 'text'},
{'value': "newly-purchased house's safe", 'type': 'hit'},
{'value': ' room, when three men break-in, searching for a missing fortune.', 'type': 'text'}
]
}]
}
{
'plot': "A lonely writer develops an unlikely relationship with his newly purchased operating system that's designed to meet his every need.",
'title': 'Her',
'highlights': [{
'score': 4.198050022125244,
'path': 'plot',
'texts': [
{'value': 'A lonely writer develops an unlikely relationship with his ', 'type': 'text'},
{'value': 'newly purchased operating system', 'type': 'hit'},
{'value': " that's designed to meet his every ", 'type': 'text'},
{'value': 'need', 'type': 'hit'},
{'value': '.', 'type': 'text'}
]
}]
}
{
'plot': "Set in the near future when artificial organs can be bought on credit, it revolves around a man who struggles to make the payments on a heart he has purchased. He must therefore go on the run before said ticker is repossessed.",
'title': 'Repo Men',
'highlights': [{
'score': 2.6448397636413574,
'path': 'plot',
'texts': [
{'value': 'Set in the ', 'type': 'text'},
{'value': 'near future when', 'type': 'hit'},
{'value': ' artificial organs can be bought on credit, it revolves around a man who struggles to make the payments on a heart he has ', 'type': 'text'},
{'value': 'purchased. He must', 'type': 'hit'}
]
}]
}
{
'plot': 'A psychologically troubled novelty supplier is nudged towards a romance with an English woman, all the while being extorted by a phone-sex line run by a crooked mattress salesman, and purchasing stunning amounts of pudding.',
'title': 'Punch-Drunk Love',
'highlights': [{
'score': 1.2451990842819214,
'path': 'plot',
'texts': [
{'value': 'A psychologically troubled ', 'type': 'text'},
{'value': 'novelty supplier is', 'type': 'hit'},
{'value': ' ', 'type': 'text'},
{'value': 'nudged towards a', 'type': 'hit'},
{'value': ' romance with an English woman, all the while being extorted by a phone-sex line run by a crooked mattress salesman, and ', 'type': 'text'},
{'value': 'purchasing stunning amounts', 'type': 'hit'},
{'value': ' of pudding.', 'type': 'text'}
]
}]
}
{
'plot': 'Jack Conrad is awaiting the death penalty in a corrupt Central American prison. He is "purchased" by a wealthy television producer and taken to a desolate island where he must fight to the death against nine other condemned killers from all corners of the world, with freedom going to the sole survivor.',
'title': 'The Condemned',
'highlights': [{
'score': 2.94378924369812,
'path': 'plot',
'texts': [
{'value': 'He is "', 'type': 'text'},
{'value': 'purchased" by a', 'type': 'hit'},
{'value': ' wealthy television producer and taken to a desolate island where he must fight to the death against ', 'type': 'text'},
{'value': 'nine other condemned', 'type': 'hit'},
{'value': ' killers from all corners of the world, with freedom going to the sole survivor.', 'type': 'text'}
]
}]
}
python partial-match-query.py
{
'plot': 'The true story of Richard Pimentel, a brilliant public speaker with a troubled past, who returns from Vietnam severely hearing -impaired and finds a new purpose in his landmark efforts on the behalf of Americans with disabilities.',
'title': 'Music Within',
'highlights': [{
'score': 1.9394469261169434,
'path': 'plot',
'texts': [
{'value': 'The true story of Richard Pimentel, a brilliant public speaker with a troubled past, who returns from Vietnam severely hearing -impaired and finds a ', 'type': 'text'},
{'value': 'new', 'type': 'hit'},
{'value': ' ', 'type': 'text'},
{'value': 'purpose', 'type': 'hit'},
{'value': ' in his landmark efforts on the behalf of Americans with disabilities.', 'type': 'text'}
]
}]
}
{
'plot': 'The supervillain Megamind finally defeats his nemesis, the superhero Metro Man. But without a hero, he loses all purpose and must find new meaning to his life.',
'title': 'Megamind',
'highlights': [{
'score': 2.90376877784729,
'path': 'plot',
'texts': [
{'value': 'But without a hero, he loses all ', 'type': 'text'},
{'value': 'purpose', 'type': 'hit'},
{'value': ' and must find ', 'type': 'text'},
{'value': 'new', 'type': 'hit'},
{'value': ' meaning to his life.', 'type': 'text'}
]
}]
}
{
'plot': 'An aging Pat Garrett is hired as a lawman on behalf of a group of wealthy New Mexico cattle barons--his sole purpose being to bring down his old friend Billy the Kid.',
'title': 'Pat Garrett & Billy the Kid',
'highlights': [{
'score': 2.115748405456543,
'path': 'plot',
'texts': [
{'value': 'An aging Pat Garrett is hired as a lawman on behalf of a group of wealthy ', 'type': 'text'},
{'value': 'New', 'type': 'hit'},
{'value': ' Mexico cattle barons--his sole ', 'type': 'text'},
{'value': 'purpose', 'type': 'hit'},
{'value': ' being to bring down his old friend Billy the Kid.', 'type': 'text'}
]
}]
}
python partial-match-query.py
{
'plot': 'After the rebels have been brutally overpowered by the Empire on their newly established base, Luke Skywalker takes advanced Jedi training with Master Yoda, while his friends are pursued by Darth Vader as part of his plan to capture Luke.',
'title': 'Star Wars: Episode V - The Empire Strikes Back'
}
{
'plot': "The new owner of the Cleveland Indians puts together a purposely horrible team so they'll lose and she can move the team. But when the plot is uncovered, they start winning just to spite her.",
'title': 'Major League'
}
{
'plot': 'The true story of Richard Pimentel, a brilliant public speaker with a troubled past, who returns from Vietnam severely hearing -impaired and finds a new purpose in his landmark efforts on the behalf of Americans with disabilities.',
'title': 'Music Within'
}
{
'plot': "A lonely writer develops an unlikely relationship with his newly purchased operating system that's designed to meet his every need.",
'title': 'Her'
}
{
'plot': 'An adrenaline junkie walks away from a whirlwind romance and embraces a new life as a thief, though he soon finds himself pursued by veteran police officer and engaged in a turf war with a local gangster.',
'title': 'Kick'
}
python partial-match-query.py
{
'plot': 'After the rebels have been brutally overpowered by the Empire on their newly established base, Luke Skywalker takes advanced Jedi training with Master Yoda, while his friends are pursued by Darth Vader as part of his plan to capture Luke.',
'title': 'Star Wars: Episode V - The Empire Strikes Back'
}
{
'plot': "The new owner of the Cleveland Indians puts together a purposely horrible team so they'll lose and she can move the team. But when the plot is uncovered, they start winning just to spite her.",
'title': 'Major League'
}
{
'plot': 'The true story of Richard Pimentel, a brilliant public speaker with a troubled past, who returns from Vietnam severely hearing -impaired and finds a new purpose in his landmark efforts on the behalf of Americans with disabilities.',
'title': 'Music Within'
}
{
'plot': "A lonely writer develops an unlikely relationship with his newly purchased operating system that's designed to meet his every need.",
'title': 'Her'
}
{
'plot': 'An adrenaline junkie walks away from a whirlwind romance and embraces a new life as a thief, though he soon finds himself pursued by veteran police officer and engaged in a turf war with a local gangster.',
'title': 'Kick'
}

돌아가기

다국어