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

데이터 읽기

이 페이지에서는 문서를 검색하기 위한 일반적인 C++ 운전자 메서드를 보여주는 복사 가능한 코드 예제를 볼 수 있습니다.

팁

이 페이지에 표시된 메서드에 대해 자세히 알아보려면 각 섹션에 제공된 링크를 참조하세요.

To use an example from this page, copy the code example into the sample application or your own application. Be sure to replace all placeholders, such as <connection string>, with the relevant values for your MongoDB deployment.

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

  1. 프로젝트 에서 가져올 수 있는 위치 에 C++ 운전자 가 설치되어 있는지 확인합니다.

  2. 다음 코드를 복사하여 프로젝트 내의 새 .cpp 파일 에 붙여넣습니다.

  3. 이 페이지에서 코드 예시 를 복사하여 파일 의 강조 표시된 섹션에 붙여넣습니다.

1#include <iostream>
2
3#include <bsoncxx/builder/basic/document.hpp>
4#include <bsoncxx/json.hpp>
5#include <mongocxx/client.hpp>
6#include <mongocxx/exception/exception.hpp>
7#include <mongocxx/instance.hpp>
8#include <mongocxx/uri.hpp>
9
10using bsoncxx::builder::basic::kvp;
11using bsoncxx::builder::basic::make_document;
12
13int main() {
14 try {
15 mongocxx::instance instance;
16
17 mongocxx::uri uri("<connection string>");
18 mongocxx::client client(uri);
19
20 auto database = client["<database name>"];
21 auto collection = database["<collection name>"];
22
23 // Start example code here
24
25 // End example code here
26
27 } catch (const mongocxx::exception& e) {
28 std::cout << "An exception occurred: " << e.what() << "\n";
29 return EXIT_FAILURE;
30 }
31
32 return EXIT_SUCCESS;
33}

다음 코드는 지정된 기준과 일치하는 컬렉션 에서 단일 문서 를 조회 하는 방법을 보여줍니다.

auto result = collection.find_one(make_document(kvp("<field name>", "<value>")));
std::cout << bsoncxx::to_json(*result) << std::endl;

find_one() 메서드에 학습 보려면 데이터 조회 가이드 의 하나의 문서 찾기 섹션을 참조하세요.

다음 코드는 지정된 기준과 일치하는 컬렉션 에서 모든 문서를 조회 하는 방법을 보여줍니다.

auto results = collection.find(make_document(kvp("<field name>", "<value>")));
for(auto&& doc : results) {
std::cout << bsoncxx::to_json(doc) << std::endl;
}

find() 메서드에 학습 보려면 데이터 조회 가이드 의 여러 문서 찾기 섹션을 참조하세요.

다음 코드는 컬렉션 의 문서 수를 계산하는 방법을 보여줍니다.

auto result = collection.count_documents({});
std::cout << result << std::endl;

count_documents() 메서드에 학습 보려면 문서 계수 가이드 의 정확한 계수 조회 섹션을 참조하세요.

다음 코드는 지정된 기준과 일치하는 컬렉션 의 문서 수를 계산하는 방법을 보여줍니다.

auto result = collection.count_documents(make_document(kvp("<field name>", "<value>")));
std::cout << result << std::endl;

count_documents() 메서드에 학습 보려면 문서 계수 가이드 의 정확한 계수 조회 섹션을 참조하세요.

다음 코드는 컬렉션 에 있는 문서 수의 예상 개수를 조회 하는 방법을 보여줍니다.

auto result = collection.estimated_document_count();
std::cout << result << std::endl;

estimated_document_count() 메서드에 학습 보려면 문서 개수 가이드 의 예상 개수 조회 섹션을 참조하세요.

다음 코드는 지정된 기준과 일치하는 문서에 대해 필드 의 고유 값을 조회 하는 방법을 보여줍니다.

auto results = collection.distinct("<field name>", "<filter>");
for(auto&& doc : results) {
std::cout << bsoncxx::to_json(doc) << std::endl;
}

distinct() 메서드에 대해 자세히 알아보려면 고유 필드 값 조회 가이드를 참조하세요.

다음 코드는 컬렉션 의 변경 사항을 모니터 하고 출력하는 방법을 보여줍니다.

auto stream = collection.watch();
while (true) {
for (const auto& event : stream) {
std::cout << bsoncxx::to_json(event) << std::endl;
}
}

watch() 메서드에 학습 보려면 데이터 변경 모니터링 가이드 를 참조하세요.