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

데이터 읽기

이 페이지에서는 C 운전자 사용하여 문서를 조회 데 사용할 수 있는 일반적인 함수를 보여주는 복사 가능한 코드 예제를 확인할 수 있습니다.

팁

이 페이지에 표시된 기능에 대해 자세히 학습 각 섹션에 제공된 관련 가이드 링크를 참조하세요.

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

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

  1. C 운전자 설치되어 있는지 확인합니다.

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

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

#include <bson/bson.h>#include <mongoc/mongoc.h>#include <stdio.h> intmain(void){ char *str; bson_error_t error; mongoc_init(); mongoc_client_t *client = mongoc_client_new("<connection string URI>"); mongoc_collection_t *collection = mongoc_client_get_collection(client, "<database name>", "collection name"); // Start example code here // End example code here mongoc_collection_destroy(collection); mongoc_client_destroy(client); mongoc_cleanup(); return EXIT_SUCCESS;}

팁

C 운전자 설치하는 방법에 대한 지침은 시작하기 가이드 에서 다운로드 및 설치를 참조하세요.

다음 예시 에서는 지정된 필터하다 에 지정된 기준과 일치하는 문서 목록을 조회합니다.

bson_t *query = bson_new();// Add fields to query heremongoc_cursor_t* results = mongoc_collection_find_with_opts(collection, query, NULL, NULL);const bson_t *doc; while (mongoc_cursor_next(results, &doc)) { char *str = bson_as_canonical_extended_json(doc, NULL); printf("%s\n", str); bson_free(str);} mongoc_cursor_destroy(results);bson_destroy(query);

함수에 대해 자세히 mongoc_collection_find_with_opts() 학습 문서 찾기 가이드 참조하세요.

다음 예시 에서는 지정된 컬렉션 의 문서 수를 반환합니다.

bson_t *query = bson_new();int64_t count = mongoc_collection_count_documents(collection, query, NULL, NULL, NULL, &error); printf("%" PRId64 "\n", count);bson_destroy(query);

함수에 대해 자세히 mongoc_collection_count_documents() 학습 문서 계수 가이드 의 정확한 계수 조회 섹션을 참조하세요.

다음 예시 에서는 지정된 필터하다 에 지정된 기준과 일치하는 문서 수를 반환합니다.

bson_t *query = bson_new();// Add fields to query hereint64_t count = mongoc_collection_count_documents(collection, query, NULL, NULL, NULL, &error); printf("%" PRId64 "\n", count); bson_destroy(query);

함수에 대해 자세히 mongoc_collection_count_documents() 학습 문서 계수 가이드 의 정확한 계수 조회 섹션을 참조하세요.

다음 예시 에서는 컬렉션 메타데이터 를 기반으로 지정된 컬렉션 의 대략적인 문서 수를 반환합니다.

int64_t count = mongoc_collection_estimated_document_count(collection, NULL, NULL, NULL, &error); printf("%" PRId64 "\n", count);

함수에 대해 자세히 mongoc_collection_estimated_document_count() 학습 문서 개수 가이드 의 예상 개수 조회 섹션을 참조하세요.

다음 예시 에서는 지정된 컬렉션 에 지정된 필드 이름의 모든 고유 값을 반환합니다.

bson_t reply;bson_t *command = BCON_NEW("distinct", BCON_UTF8("<collection name>"), "key", BCON_UTF8("<field name>")); if (!mongoc_collection_read_command_with_opts(collection, command, NULL, NULL, &reply, &error)) { fprintf(stderr, "An error occurred: %s\n", error.message);} else { char *str = bson_as_canonical_extended_json(&reply, NULL); printf("%s\n", str); bson_free(str);}bson_destroy(&reply);bson_destroy(command);

명령에 대해 자세히 학습 distinct 고유 필드 값 가이드 참조하세요.

다음 예시 에서는 지정된 컬렉션 에 대한 변경 스트림 을 생성하고 해당 컬렉션 의 후속 변경 이벤트를 출력합니다.

mongoc_change_stream_t *change_stream;bson_t *pipeline = bson_new();// Add stages to pipeline hereconst bson_t *doc; change_stream = mongoc_collection_watch(collection, pipeline, NULL); while (mongoc_change_stream_next(change_stream, &doc)) { char *str = bson_as_canonical_extended_json(doc, NULL); printf("Change: %s\n", str); bson_free(str);} bson_destroy(pipeline);mongoc_change_stream_destroy(change_stream);

함수에 대해 mongoc_collection_watch() 자세히 학습 데이터 변경 모니터링 가이드 참조하세요.