데이터 읽기
이 페이지의 내용
개요
이 페이지에서는 MongoDB 에서 데이터를 읽는 데 사용할 수 있는 일반적인 스칼라 운전자 메서드를 보여주는 복사 가능한 코드 예제를 볼 수 있습니다.
팁
이 페이지에 표시된 메서드에 대해 자세히 알아보려면 각 섹션에 제공된 링크를 참조하세요.
이 페이지의 예제를 사용하려면 코드 예제를 샘플 애플리케이션 또는 자체 애플리케이션에 복사합니다. 코드 예제의 모든 자리 표시자(예: <connection string URI>
)를 MongoDB 배포에 필요한 관련 값으로 바꿔야 합니다.
샘플 애플리케이션
샘플 애플리케이션
다음 샘플 애플리케이션을 사용하여 이 페이지의 코드 예제를 테스트할 수 있습니다. 샘플 애플리케이션을 사용하려면 다음 단계를 수행하세요.
Maven 또는 sbt 프로젝트 에 스칼라 운전자 설치되어 있는지 확인합니다.
다음 코드를 복사하여 새
.scala
파일에 붙여넣습니다.이 페이지에서 코드 예제를 복사하여 파일의 지정된 줄에 붙여넣습니다.
1 import org.mongodb.scala._ 2 import org.mongodb.scala.bson.Document 3 import org.mongodb.scala.model.Filters._ 4 import org.mongodb.scala.model.changestream._ 5 6 object SampleReadApp { 7 8 def main(args: Array[String]): Unit = { 9 val mongoClient = MongoClient("<connection string URI>") 10 val database: MongoDatabase = mongoClient.getDatabase("<database name>") 11 val collection: MongoCollection[Document] = database.getCollection("<collection name>") 12 13 14 // Start example code here 15 16 // End example code here 17 18 // Wait for the operations to complete before closing client 19 // Note: This example uses Thread.sleep() for brevity and does not guarantee all 20 // operations will be completed in time 21 Thread.sleep(1000) 22 mongoClient.close() 23 } 24 }
find one
다음 예시 에서는 지정된 필터하다 에 지정된 기준과 일치하는 문서 를 조회합니다.
val filter = equal("<field>", "<value>") collection.find(filter).first().subscribe((doc: Document) => println(doc.toJson()), (e: Throwable) => println(s"There was an error: $e"))
first()
메서드에 대해 자세히 학습 데이터 조회 가이드 참조하세요.
여러 항목 찾기
다음 예시 에서는 지정된 필터하다 에 지정된 기준과 일치하는 모든 문서를 조회합니다.
val filter = equal("<field>", "<value>") collection.find(filter).subscribe((doc: Document) => println(doc.toJson()), (e: Throwable) => println(s"There was an error: $e"))
find()
메서드에 대해 자세히 알아보려면 데이터 조회 가이드를 참조하세요.
컬렉션의 문서 수 계산
다음 예시 에서는 지정된 컬렉션 의 문서 수를 반환합니다.
collection.countDocuments() .subscribe((count: Long) => println(s"Number of documents: $count"), (e: Throwable) => println(s"There was an error: $e"))
countDocuments()
메서드에 학습 보려면 문서 수 가이드 를 참조하세요.
쿼리에서 반환된 문서 수 계산
다음 예시 지정된 컬렉션 에서 쿼리 기준과 일치하는 문서 수를 반환합니다.
collection.countDocuments(equal("<field>", "<value>")) .subscribe((count: Long) => println(s"Number of documents: $count"), (e: Throwable) => println(s"There was an error: $e"))
countDocuments()
메서드에 학습 보려면 문서 수 가이드 를 참조하세요.
예상 문서 수
다음 예시 에서는 컬렉션 메타데이터 를 기반으로 지정된 컬렉션 의 대략적인 문서 수를 반환합니다.
collection.estimatedDocumentCount() .subscribe((count: Long) => println(s"Estimated number of documents: $count"), (e: Throwable) => println(s"There was an error: $e"))
estimatedDocumentCount()
메서드에 학습 보려면 문서 수 가이드 를 참조하세요.
Retrieve Distinct Values
다음 예시 에서는 지정된 컬렉션 에 지정된 필드 이름의 모든 고유 값을 반환합니다.
collection.distinct("<field>") .subscribe((value: String) => println(value), (e: Throwable) => println(s"There was an error: $e"))
distinct()
메서드에 대해 자세히 알아보려면 고유 필드 값 조회 가이드를 참조하세요.
데이터 변경 사항 모니터링
다음 예시 에서는 지정된 컬렉션 에 대한 변경 스트림 을 생성하고 해당 컬렉션 의 후속 변경 이벤트를 출력합니다.
val changeStreamObservable = collection.watch() changeStreamObservable.subscribe( (changeEvent: ChangeStreamDocument[Document]) => { println(s"Received a change to the collection: ${changeEvent}") }, (e: Throwable) => { println(s"Encountered an error: ${e.getMessage}") }, () => println("Completed") )
watch()
메서드에 학습 보려면 데이터 변경 모니터링 가이드 를 참조하세요.