개요
이 페이지에서는 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 in the code examples, such as <connection string URI>, with the relevant values for your MongoDB deployment.
샘플 애플리케이션
다음 샘플 애플리케이션을 사용하여 이 페이지의 코드 예제를 테스트할 수 있습니다. 샘플 애플리케이션을 사용하려면 다음 단계를 수행하세요.
C 운전자 설치되어 있는지 확인합니다.
다음 코드를 복사하여 새
.c파일에 붙여넣습니다.이 페이지에서 코드 예제를 복사하여 파일의 지정된 줄에 붙여넣습니다.
1 2 3 4 5 int 6 main(void) 7 { 8 char *str; 9 bson_error_t error; 10 11 mongoc_init(); 12 13 mongoc_client_t *client = mongoc_client_new("<connection string URI>"); 14 mongoc_collection_t *collection = mongoc_client_get_collection(client, "<database name>", "collection name"); 15 16 // Start example code here 17 18 // End example code here 19 20 mongoc_collection_destroy(collection); 21 mongoc_client_destroy(client); 22 mongoc_cleanup(); 23 24 return EXIT_SUCCESS; 25 }
팁
C 운전자 설치하는 방법에 대한 지침은 시작하기 가이드 에서 다운로드 및 설치를 참조하세요.
문서 찾기
다음 예시 에서는 지정된 필터하다 에 지정된 기준과 일치하는 문서 목록을 조회합니다.
bson_t *query = bson_new(); // Add fields to query here mongoc_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 here int64_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() 학습 문서 개수 가이드 의 예상 개수 조회 섹션을 참조하세요.
Retrieve Distinct Values
다음 예시 에서는 지정된 컬렉션 에 지정된 필드 이름의 모든 고유 값을 반환합니다.
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 here const 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() 자세히 학습 데이터 변경 모니터링 가이드 참조하세요.