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

$search

$search

The $search stage performs a full-text search on the specified field or fields. The field or fields must be covered by an MongoDB Search index.

$search 단계는 다음 환경에서 사용할 수 있습니다.

$search 파이프라인 단계의 프로토타입 형식은 다음과 같습니다.

{
$search: {
"index": "<index-name>",
"<operator-name>"|"<collector-name>": {
<operator-specification>|<collector-specification>
},
"highlight": {
<highlight-options>
},
"concurrent": true | false,
"count": {
<count-options>
},
"searchAfter"|"searchBefore": "<encoded-token>",
"scoreDetails": true| false,
"sort": {
<fields-to-sort>: 1 | -1
},
"returnScope": {
"path": "<embedded-documents-field-to-retrieve>"
},
"returnStoredSource": true | false,
"searchNodePreference": {
"key": <preference-string>
}
}
}

$search 단계에서는 다음 필드가 있는 문서를 사용합니다.

필드
유형
필요성
설명

<collector-name>

객체

조건부

쿼리에 사용할 컬렉터의 이름입니다. 이 필드의 값으로 콜렉터별 옵션이 포함된 문서를 제공할 수 있습니다. 이 값 또는 <operator-name>가 필요합니다.

concurrent

부울

옵션

전용 검색 노드에서 세그먼트 간 검색 병렬 처리합니다. 클러스터 에 별도의 검색 노드가 없는 경우 MongoDB Search는 이 플래그를 무시합니다. 생략하면 기본값은 false입니다. 자세히 학습 세그먼트 간 쿼리 실행 병렬 처리를 참조하세요.

count

객체

옵션

결과의 개수 를 검색하기 위한 개수 옵션을 지정하는 문서입니다. 자세한 학습은 MongoDB 검색 결과 집계를 참조하세요.

highlight

객체

옵션

검색어를 원래 문맥에 맞게 표시하기 위한 강조 표시 옵션을 지정하는 문서입니다.

index

문자열

옵션

사용할 MongoDB Search 인덱스 의 이름입니다. 생략하면 기본값은 default입니다.

If you name your index default, you don't need to specify an index parameter in the $search pipeline stage. If you give a custom name to your index, you must specify this name in the index parameter.

인덱스 이름의 철자가 잘못되었거나 지정된 인덱스 클러스터 에 없는 경우 MongoDB Search는 결과를 반환하지 않습니다.

<operator-name>

객체

조건부

검색할 연산자의 이름입니다. 이 필드의 값으로 연산자별 옵션이 포함된 문서를 제공할 수 있습니다. 이 값 또는 <collector-name>가 필요합니다. 복합 연산자를 사용하여 여러 연산자가 포함된 복합 쿼리를 실행하려면 복합 연산자를 사용합니다.

returnScope

객체

옵션

쿼리의 컨텍스트를 지정한 내장된 문서 필드로 설정하는 객체입니다. returnStoredSource를 지정하고 true로 설정해야 합니다.

returnStoredSource

부울

조건부

백엔드 데이터베이스에서 전체 문서 조회를 수행할지, 아니면 MongoDB 검색에서 직접 저장된 소스 필드만 반환할지를 지정하는 플래그입니다. 생략하면 기본값은 false입니다. returnScope를 지정하는 경우 true여야 합니다.

자세한 사항은 Return Stored Source Fields 항목을 참조하십시오.

searchAfter

문자열

옵션

Reference point for retrieving results. searchAfter returns documents starting immediately following the specified reference point. The reference point must be a Base64-encoded token generated by the $meta keyword searchSequenceToken. To learn more, see Paginate the Results. This field is mutually exclusive with the searchBefore field.

searchBefore

문자열

옵션

Reference point for retrieving results. searchBefore returns documents starting immediately before the specified reference point. The reference point must be a Base64-encoded token generated by the $meta keyword searchSequenceToken. To learn more, see Paginate the Results. This field is mutually exclusive with the searchAfter field.

searchNodePreference

객체

옵션

이 쿼리 에 대한 우선 라우팅을 활성화하는 문서입니다. 이 필드 에 값을 설정하다 하면 Atlas 해당 값이 동일하게 유지되는 한 동일한 검색 노드 에 대해 이 쿼리 실행하여 기본값 쿼리 라우팅 동작을 재정의합니다.

이 문서 에는 key 값이 임의의 string인 키-값 쌍이 포함되어 있습니다.

중요: 이 설정을 사용하면 검색 결과의 일관성 향상시킬 수 있지만 영구적인 일관성 보장하지는 않습니다. 데이터 세트, 인덱스 구조, 클러스터 토폴로지 또는 기본 설정 노드 가용성을 변경하면 여전히 불일치가 발생할 수 있습니다.

scoreDetails

부울

옵션

Flag that specifies whether to retrieve a detailed breakdown of the score for the documents in the results. If omitted, defaults to false. To view the details, you must use the $meta expression in the $project stage. To learn more, see Return the Score Details.

sort

객체

옵션

MongoDB Search 결과를 오름차순 또는 내림차순으로 정렬하는 필드를 지정하는 문서입니다. 날짜, 숫자(정수, 부동 소수점 및 이중 값) 및 문자열 값을 기준으로 정렬할 수 있습니다. 자세히 학습 MongoDB 검색 결과 정렬을 참조하세요.

$search must be the first stage of any pipeline it appears in. $search cannot be used in:

$search returns only the results of your query. The metadata results of your $search query are saved in the $$SEARCH_META aggregation variable. You can use the $$SEARCH_META variable to view the metadata results for your $search query.

The $$SEARCH_META aggregation variable can be used anywhere after a $search stage in any pipeline, but it can't be used after the $lookup or $unionWith stage in any pipeline. The $$SEARCH_META aggregation variable can't be used in any subsequent stage after a $searchMeta stage.

예시

sample_mflix.movies collection에 대한 다음 인덱스를 가정합니다.

{
"mappings": {
"dynamic": false,
"fields": {
"released": {
"type": "date"
}
}
}
}

The following query searches for movies released near September 01, 2011 using the $search stage. The query includes a:

  • $project stage to exclude all fields in the documents except title and released.

  • $facet 단계에서는 다음을 출력합니다.

    • docs 상위 5개의 검색 결과 배열이 있는 $facet 필드

    • meta $$SEARCH_META 변수 값이 있는 필드

db.movies.aggregate([
{
"$search": {
"near": {
"path": "released",
"origin": ISODate("2011-09-01T00:00:00.000+00:00"),
"pivot": 7776000000
}
}
},
{
$project: {
"_id": 0,
"title": 1,
"released": 1
}
},
{ "$limit": 5 },
{
"$facet": {
"docs": [],
"meta": [
{"$replaceWith": "$$SEARCH_META"},
{"$limit": 1}
]
}
}
])
{
"docs" : [
{
"title" : "Submarino",
"released" : ISODate("2011-09-01T00:00:00Z")
},
{
"title" : "Devil's Playground",
"released" : ISODate("2011-09-01T00:00:00Z")
},
{
"title" : "Bag It",
"released" : ISODate("2011-09-01T00:00:00Z")
},
{
"title" : "Dos",
"released" : ISODate("2011-09-01T00:00:00Z")
},
{
"title" : "We Were Here",
"released" : ISODate("2011-09-01T00:00:00Z")
}
],
"meta" : [
{ "count" : { "lowerBound" : NumberLong(17373) } }
]
}

$$SEARCH_META 변수 및 그 사용법에 대해 자세히 알아보려면 다음을 참조하세요.

If you are experiencing issues with your MongoDB Search $search queries, see Troubleshoot Queries.

이 페이지 평가하기