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

고유 필드 값 검색

이 가이드 에서는 C 운전자 사용하여 컬렉션 전체에서 지정된 필드 의 고유 값을 조회 방법을 학습 수 있습니다.

컬렉션 내에서 서로 다른 문서에 단일 필드 에 대해 서로 다른 값이 포함될 수 있습니다. 예시 들어 restaurant 컬렉션 의 한 문서 는 borough 값이 "Manhattan"이고 다른 문서의 borough 값은 "Queens"입니다. C 운전자 사용하면 컬렉션 의 여러 문서에서 필드 에 포함된 고유 값을 모두 조회 할 수 있습니다.

이 가이드의 예시에서는 Atlas 샘플 데이터 세트sample_restaurants 데이터베이스에 있는 restaurants 컬렉션을 사용합니다. 무료 MongoDB Atlas 클러스터 생성하고 샘플 데이터 세트를 로드하는 방법을 학습하려면 MongoDB 시작하기 가이드 를 참조하세요.

지정된 필드 의 고유 값을 조회 하려면 mongoc_collection_read_command_with_opts() 함수를 호출하고 distinct 명령을 사용하도록 지시합니다. 또한 데이터를 조회 할 컬렉션 과 필드 지정해야 합니다.

다음 예에서는 restaurants 컬렉션에 있는 borough 필드의 고유 값을 검색합니다.

bson_t reply;
bson_error_t error;
bson_t *command = BCON_NEW("distinct",
BCON_UTF8("restaurants"),
"key",
BCON_UTF8("borough"));
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);

결과에는 컬렉션의 모든 문서에서 borough 필드에 나타나는 모든 고유 값이 표시됩니다. 여러 문서의 borough 필드에 동일한 값이 있지만 각 값은 결과에 한 번만 표시됩니다.

명령에 쿼리 필터하다 제공하여 컬렉션 의 문서 하위 집합에서 distinct 고유 필드 값을 찾을 수 있습니다. 쿼리 필터하다 는 작업에서 문서를 일치시키는 데 사용되는 검색 기준을 지정하는 표현식 입니다.

쿼리 필터 만들기에 대해 자세히 학습 쿼리 지정을 참조하세요.

다음 예에서는 cuisine 필드 값이 "Italian" 인 모든 문서에 대해 borough 필드의 고유 값을 검색합니다.

bson_t reply;
bson_error_t error;
bson_t *query = BCON_NEW("cuisine", BCON_UTF8("Italian"));
bson_t *command = BCON_NEW("distinct", BCON_UTF8("restaurants"),
"key", BCON_UTF8("borough"),
"query", BCON_DOCUMENT(query));
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);
bson_destroy(query);

distinct 명령은 mongoc_collection_read_command_with_opts() 함수에 옵션을 전달하여 수정할 수 있습니다. 옵션을 지정하지 않으면 운전자 작업을 사용자 지정하지 않습니다.

다음 표에서는 distinct 작업을 사용자 지정하는 데 사용할 수 있는 몇 가지 옵션을 설명합니다.

옵션
설명

collation

Specifies the kind of language collation to use when sorting results. For more information, see Collation in the MongoDB Server manual.

comment

작업에 첨부할 주석을 지정합니다.

작업을 수정하는 데 사용할 distinct 있는 옵션의 전체 목록은 MongoDB Server 매뉴얼의 고유 문서를 참조하세요.

다음 예시에서는 borough 필드 값이 "Bronx" 이고 cuisine 필드 값이 "Pizza" 인 모든 문서에 대해 name 필드의 고유 값을 검색합니다. 또한 comment 옵션을 사용하여 작업에 주석을 추가합니다.

bson_t reply;
bson_error_t error;
bson_t *query = BCON_NEW("borough", BCON_UTF8("Bronx"),
"cuisine", BCON_UTF8("Pizza"));
bson_t *command = BCON_NEW("distinct", BCON_UTF8("restaurants"),
"key", BCON_UTF8("name"),
"query", BCON_DOCUMENT(query));
bson_t *opts = BCON_NEW("comment", BCON_UTF8("Bronx pizza restaurants"));
if (!mongoc_collection_read_command_with_opts(collection, command, NULL, opts, &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);
bson_destroy(query);
bson_destroy(opts);

distinct 명령에 대해 자세히 학습 MongoDB Server 매뉴얼의 Distinct 페이지를 참조하세요.

함수에 대해 자세히 학습 API mongoc_collection_read_command_with_opts() 설명서를 참조하세요.