Docs Menu
Docs Home
/ / /
Scala

인덱스를 사용하여 쿼리 최적화

이 페이지의 내용

  • 개요
  • 샘플 애플리케이션
  • 단일 필드 인덱스
  • 복합 인덱스
  • Multikey Index
  • 지리 공간적 인덱스
  • 드롭 인덱스
  • Atlas 검색 인덱스 관리
  • Atlas Search 인덱스 만들기
  • 검색 인덱스 나열
  • 검색 인덱스 업데이트
  • Atlas Search 인덱스 삭제
  • API 문서

이 페이지에서는 MongoDB 스칼라 드라이버 사용하여 다양한 유형의 인덱스를 관리 방법을 보여주는 복사 가능한 코드 예제를 볼 수 있습니다.

이 페이지의 예시 사용하려면 코드 예시 샘플 애플리케이션 또는 자체 애플리케이션 에 복사합니다. 코드 예시의 모든 자리 표시자(예: <connection string URI>)를 MongoDB deployment 에 필요한 관련 값으로 바꿔야 합니다.

다음 샘플 애플리케이션 사용하여 이 페이지의 코드를 테스트할 수 있습니다. 샘플 애플리케이션 사용하려면 다음 단계를 수행하세요.

  1. 프로젝트 에 스칼라 운전자 설치되어 있는지 확인합니다. 자세한 학습 은 다운로드 및 설치 가이드 참조하세요.

  2. 다음 코드를 복사하여 새 .scala 파일에 붙여넣습니다.

  3. 이 페이지에서 코드 예제를 복사하여 파일의 지정된 줄에 붙여넣습니다.

import org.mongodb.scala._
import org.mongodb.scala.model.SearchIndexModel
import java.util.concurrent.TimeUnit
import scala.concurrent.Await
import scala.concurrent.duration.Duration
import org.mongodb.scala.model.Indexes
object SearchIndexes {
def main(args: Array[String]): Unit = {
// Create a new client and connect to the server
val mongoClient = MongoClient("<connection string URI>")
val database = mongoClient.getDatabase("<database name>")
val collection = database.getCollection("<collection name>")
// Start example code here
// End example code here
Thread.sleep(1000)
mongoClient.close()
}
}

다음 예시 에서는 지정된 필드 에 오름차순 인덱스 를 생성합니다.

val index = Indexes.ascending("<field name>")
val observable = collection.createIndex(index)
Await.result(observable.toFuture(), Duration(10, TimeUnit.SECONDS))

단일 필드 인덱스에 학습 보려면 단일 필드 인덱스 가이드 를 참조하세요.

다음 예시 지정된 두 필드에 복합 인덱스 생성합니다.

val index = Indexes.compoundIndex(
Indexes.descending("<field name 1>"),
Indexes.ascending("<field name 2>")
)
val observable = collection.createIndex(index)
Await.result(observable.toFuture(), Duration(10, TimeUnit.SECONDS))

복합 인덱스에 대해 자세히 알아보려면 복합 인덱스 가이드를 참조하세요.

다음 예시 에서는 지정된 배열 값 필드 에 멀티키 인덱스 를 생성합니다.

val index = Indexes.ascending("<field name>")
val observable = collection.createIndex(index)
Await.result(observable.toFuture(), Duration(10, TimeUnit.SECONDS))

멀티키 인덱스에 대해 자세히 알아보려면 멀티키 인덱스 가이드를 참조하세요.

다음 예시 에서는 GeoJSON 객체를 포함하는 지정된 필드 에 2dsphere 인덱스 를 생성합니다.

val observable = collection.createIndex(Indexes.geo2dsphere("<2d index>"))
Await.result(observable.toFuture(), Duration(10, TimeUnit.SECONDS))

2dsphere 인덱스에 대한 자세한 내용은 MongoDB Server 매뉴얼의 dsphere2인덱스 가이드 를 참조하세요.

GeoJSON 유형에 대한 자세한 내용은 MongoDB Server 매뉴얼의 GeoJSON 객체 가이드 참조하세요.

다음 예시 에서는 지정된 이름의 인덱스 를 삭제합니다.

val observable = collection.dropIndex("<index name>")
Await.result(observable.toFuture(), Duration(10, TimeUnit.SECONDS))

다음 섹션에는 Atlas Search 인덱스를 관리 하는 방법을 설명하는 코드 예제가 포함되어 있습니다.

검색 인덱스에 대해 자세히 학습 Atlas Search 인덱스 가이드 참조하세요.

다음 예시 에서는 지정된 필드 에 Atlas Search 인덱스 를 생성합니다.

val index = Document("mappings" -> Document("dynamic" -> <boolean value>))
val observable = collection.createSearchIndex("<index name>", index)
Await.result(observable.toFuture(), Duration(10, TimeUnit.SECONDS))

다음 예시 에서는 지정된 컬렉션 의 Atlas Search 인덱스 목록을 출력합니다.

val observable = collection.listSearchIndexes()
observable.subscribe(new Observer[Document] {
override def onNext(index: Document): Unit = println(index.toJson())
override def onError(e: Throwable): Unit = println("Error: " + e.getMessage)
override def onComplete(): Unit = println("Completed")
})

다음 예시 에서는 지정된 새 인덱스 정의로 기존 Atlas Search 인덱스 를 업데이트합니다.

val updateIndex = Document("mappings" -> Document("dynamic" -> false))
val observable = collection.updateSearchIndex("<index to update>", updateIndex)
Await.result(observable.toFuture(), Duration(10, TimeUnit.SECONDS))

다음 예시 에서는 지정된 이름의 Atlas Search 인덱스 를 삭제합니다.

val observable = collection.dropSearchIndex("<index name>")
Await.result(observable.toFuture(), Duration(10, TimeUnit.SECONDS))

이 가이드 에 사용된 메서드 또는 객체에 대해 자세히 학습 다음 API 설명서를 참조하세요.

돌아가기

복제본 세트에 대한 작업