Docs Menu
Docs Home
/ / /
Scala
/

Retrieve Data

이 페이지의 내용

  • 개요
  • 샘플 데이터
  • 문서 찾기
  • 여러 문서 찾기
  • 하나의 문서 찾기
  • 검색 동작 수정
  • 추가 정보
  • API 문서

이 가이드 에서는 스칼라 운전자 로 읽기 작업을 통해 MongoDB 컬렉션 에서 데이터를 조회 방법을 학습 수 있습니다. 컬렉션 에서 메서드를 호출하여 find() 기준의 설정하다 과 일치하는 문서를 조회 할 수 있습니다.

이 가이드 의 예제에서는 companies sample_training Atlas 샘플 데이터 세트의 데이터베이스 에 있는 컬렉션 사용합니다. 스칼라 애플리케이션 에서 이 컬렉션 액세스 하려면 MongoClient Atlas cluster 에 연결하는 를 만들고 및 변수에 다음 값을 할당합니다.database collection

val database: MongoDatabase = mongoClient.getDatabase("sample_training")
val collection: MongoCollection[Document] = database.getCollection("companies")

무료 MongoDB Atlas cluster 를 생성하고 샘플 데이터 세트를 로드하는 방법을 학습 보려면 Atlas 시작하기 가이드 를 참조하세요.

컬렉션 에서 문서를 조회 하려면 find() 메서드를 사용합니다. 이 메서드는 쿼리 필터하다 매개 변수를 사용하고 FindObservable 쿼리 결과에 액세스 할 수 있는 클래스의 인스턴스 반환합니다. 클래스는 FindObservable 인스턴스 에 연결하여 FindObservable 동작을 수정할 수 있는 추가 메서드(예:)도 제공합니다.first()

쿼리 필터에 학습 보려면 쿼리 지정 가이드 를 참조하세요.

컬렉션에서 여러 문서를 찾으려면 검색하려는 문서의 기준을 지정하는 find() 메서드에 쿼리 필터를 전달합니다.

find() 메서드는 FindObservable의 인스턴스 를 반환하며, 이를 반복하여 일치하는 문서를 볼 수 있습니다. subscribe() 메서드를 사용하여 FindObservable를 반복합니다.

다음 예시 find() 메서드를 사용하여 founded_year 필드 값이 1970 인 모든 문서를 찾고 결과를 인쇄합니다.

val filter = equal("founded_year", 1970)
collection.find(filter).subscribe((doc: Document) => println(doc.toJson()),
(e: Throwable) => println(s"There was an error: $e"))
{"_id":{"$oid":"..."},"name":"Mitsubishi Motors","permalink":"mitsubishi-motors",
"crunchbase_url":"http://www.crunchbase.com/company/mitsubishi-motors",
... }
{"_id":{"$oid":"..."},"name":"Western Digital","permalink":"western-digital",
"crunchbase_url":"http://www.crunchbase.com/company/western-digital",
... }
{"_id":{"$oid":"..."},"name":"Celarayn","permalink":"celarayn","crunchbase_url":
"http://www.crunchbase.com/company/celarayn",
... }

참고

모든 문서 찾기

컬렉션 의 모든 문서를 찾으려면 매개변수를 전달하지 않고 find() 메서드를 호출합니다.

collection.find()

컬렉션 에서 단일 문서 찾으려면 find() 메서드를 호출하고 찾으려는 문서 의 기준을 지정하는 쿼리 필터하다 전달합니다. 그런 다음 first() 메서드를 find()에 연결합니다.

find() 메서드는 FindObservable 인스턴스 반환하고, first() 메서드는 FindObservable에 저장된 첫 번째 쿼리 결과가 포함된 SingleObserver 인스턴스 반환합니다. subscribe() 메서드를 호출하여 SingleObserver 결과 액세스 할 수 있습니다.

다음 예시 find()first() 메서드를 사용하여 name 필드 값이 "LinkedIn"인 첫 번째 문서 찾습니다.

val filter = equal("name", "LinkedIn")
collection.find(filter).first().subscribe((doc: Document) => println(doc.toJson()),
(e: Throwable) => println(s"There was an error: $e"))
{"_id": {"$oid": "..."}, "name": "LinkedIn", "permalink": "linkedin", "crunchbase_url":
"http://www.crunchbase.com/company/linkedin", "homepage_url": "http://linkedin.com",
...}

정렬 순서

정렬 기준이 지정되지 않은 경우 first() 메서드는 디스크에 기본 순서에 따라 첫 번째 문서를 반환합니다.

FindObservable 클래스에서 제공하는 메서드를 연결하여 find() 메서드의 동작을 수정할 수 있습니다. 다음 표에서는 이러한 메서드 중 일부에 대해 설명합니다.

메서드
설명

explain()

Explains the execution plan for this operation with the specified verbosity level.
Parameter Type: ExplainVerbosity

collation()

Sets the collation to use for the operation. The default value is the collation specified for the collection.
Parameter Type: Collation

comment()

Attaches a comment to the operation.
Parameter Type: String

first()

Returns an Observable that stores only the first query result. To view an example that uses this method, see Find One Document on this page.

limit()

Sets the maximum number of documents the operation can return.
Parameter Type: Int

skip()

Sets the number of documents to skip before returning results.
Parameter Type: Int

sort()

Sets the order in which the operation returns matching documents.
Parameter Type: Bson

다음 예시 find() 메서드를 사용하여 number_of_employees 필드 값이 1000인 모든 문서를 찾습니다. 이 예시 limit() 메서드를 사용하여 최대 5 개의 결과를 반환합니다.

val filter = equal("number_of_employees", 1000)
collection.find(filter).limit(5).subscribe((doc: Document) => println(doc.toJson()),
(e: Throwable) => println(s"There was an error: $e"))
{"_id": {"$oid": "..."}, "name": "Akamai Technologies", "permalink": "akamai-technologies",
"crunchbase_url": "http://www.crunchbase.com/company/akamai-technologies", "homepage_url":
"http://www.akamai.com", ... }
{"_id": {"$oid": "..."}, "name": "Yodle", "permalink": "yodle", "crunchbase_url":
"http://www.crunchbase.com/company/yodle", "homepage_url": "http://www.yodle.com", ... }
{"_id": {"$oid": "..."}, "name": "Antal International", "permalink": "antal-international",
"crunchbase_url": "http://www.crunchbase.com/company/antal-international", "homepage_url":
"http://antal.com", ... }
{"_id": {"$oid": "..."}, "name": "Yatra online", "permalink": "yatra-online", "crunchbase_url":
"http://www.crunchbase.com/company/yatra-online", "homepage_url": "http://www.Yatra.com", ... }
{"_id": {"$oid": "..."}, "name": "Gumtree", "permalink": "gumtree", "crunchbase_url":
"http://www.crunchbase.com/company/gumtree", "homepage_url": "http://www.gumtree.co.za", ... }

FindObservable 멤버 메서드의 전체 목록은 FindObservable 클래스에 대한 API 설명서를 참조하세요.

쿼리 필터에 학습 보려면 쿼리 지정 가이드 를 참조하세요.

스칼라 운전자 사용하여 문서를 조회 하는 코드 예제를 보려면 MongoDB 에서 데이터 읽기를 참조하세요.

이 가이드에서 설명하는 메서드에 대해 자세히 알아보려면 다음 API 설명서를 참조하세요.

  • find()

  • first()

  • limit()

돌아가기

데이터 읽기