Make the MongoDB docs better! We value your opinion. Share your feedback for a chance to win $100.
Click here >
Docs Menu
Docs Home
/ /

wildcard 연산자

wildcard

wildcard 연산자는 검색 문자열에 모든 문자와 일치할 수 있는 특수 문자를 사용하는 쿼리를 활성화합니다.

캐릭터
설명

?

모든 단일 문자와 일치합니다.

*

0개 이상의 문자와 일치합니다.

\

이스케이프 문자.

wildcard 텀 수준 연산자이므로 query 필드는 분석되지 않습니다. query 필드는 특수 문자가 포함된 단일 텀으로 취급되므로 텀 수준 연산자는 키워드 분석기에서 잘 작동합니다. 분석된 query 필드에 대한 쿼리 예시는 분석된 필드 예시를 참조하세요.

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

{
$search: {
"index": <index name>, // optional, defaults to "default"
"wildcard": {
"query": "<search-string>",
"path": "<field-to-search>",
"allowAnalyzedField": <boolean>,
"score": <options>
}
}
}

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

필드
유형
설명
필요성
기본값

query

문자열 또는 문자열 배열

검색할 문자열 또는 문자열을 입력합니다.

path

문자열 또는 문자열 배열

인덱싱된 필드 또는 검색할 필드. 검색할 와일드카드 경로를 지정할 수도 있습니다.

allowAnalyzedField

부울

Set to true if you run your query against a field indexed with a tokenizing analyzer. This avoids the field's analyzer from splitting the indexed value into many terms. Fields indexed with the Keyword Analyzer don't require this option.

no

false

score

객체

일치하는 검색어 결과에 부여된 점수를 수정합니다. 옵션은 다음과 같습니다.

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

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

  • function: 주어진 표현식을 사용하여 결과 점수를 대체합니다.

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

no

wildcard is a term-level operator, meaning that the query field isn't analyzed. Use the wildcard operator on fields indexed with tokenizing analyzers by setting allowAnalyzedField to true. Fields indexed with the Keyword Analyzer already treat the field value as a single term, so allowAnalyzedField: true isn't required.

If you use wildcard with allowAnalyzedField: true, MongoDB Search applies character filters and token filters based on the specified analyzer or custom analyzer for the field. MongoDB Search skips the tokenization and the output is always a single token.

다음 예시는 분석된 필드에서 검색을 수행할 때 wildcard 연산자가 어떻게 작동하는지 보여줍니다.

예시

표준 분석기 foo bar baz 로 필드 인덱싱한다고 가정해 보겠습니다. MongoDB Search는 필드 foo, bar, baz로 분석하고 인덱싱합니다. 와일드카드 연산자 foo bar* 를 끝에 와일드카드가 있는 단일 검색 텀 처리하기 때문에 이 필드 에서 foo bar* 를 검색하면 아무것도 찾을 수 없습니다. 즉, MongoDB Search는 foo bar 로 시작하는 모든 텀 를 필드 검색하지만 텀 없기 때문에 아무것도 찾지 못합니다.

예시

키워드 분석기로 인덱싱된 필드에서 *Star Trek*을 검색하면 해당 필드에 어떤 컨텍스트에서든 Star Trek string이 포함된 모든 문서를 찾을 수 있습니다. 표준 분석기로 인덱싱된 필드에서 *Star Trek*을 검색하면 StarTrek 사이에 공백이 있고 인덱스에 공백이 포함되어 있지 않기 때문에 아무것도 찾을 수 없습니다.

mongosh 또는 드라이버와 함께 이스케이프 문자를 사용하는 경우 이스케이프할 문자 앞에 이중 백슬래시를 사용해야 합니다.

예시

집계 파이프라인에서 리터럴 별표가 포함된 문자열을 검색하는 와일드카드 표현식을 만들려면 다음 표현식을 사용하세요.

"*\\**"

첫 번째 별표와 마지막 별표는 모든 문자와 일치하는 와일드카드 역할을 하며 \\* 는 리터럴 별표와 일치합니다.

참고

리터럴 백슬래시를 이스케이프하려면 다음 표현식을 사용합니다.

"*\\\*"

The following examples use the movies collection in the sample_mflix database with a custom index definition that uses the keyword analyzer. If you have the sample dataset on your cluster, you can create a MongoDB Search index on the movies collection and run the example queries on your cluster.

다음 인덱스 정의는 키워드 분석기를사용하여 movies 컬렉션의 title 필드를 인덱싱합니다.

1{
2 "mappings": {
3 "fields": {
4 "title": {
5 "analyzer": "lucene.keyword",
6 "type": "string"
7 }
8 }
9 }
10}

다음은 title 필드에서 Green D로 시작하고 그 뒤로 다른 문자 여러 개가 이어지는 영화 제목을 모두 검색하는 예시입니다.

다음 예에서는 문자열 title(여기서 Wom?n은(는) 단일 문자일 수 있음)로 시작하고 그 뒤에 공백 한 칸이 있고, 뒤이어 다른 문자 여러 개가 이어지는 영화 제목을 모든 ? 필드에서 검색합니다.

다음 예제에서는 이스케이프 문자를 사용하여 title 필드가 물음표로 끝나는 문서를 검색합니다.

query 필드의 * 문자는 모든 문자와 일치하며, \\? 문자열은 리터럴 물음표와 일치합니다.

돌아가기

Vector Search

이 페이지의 내용