문서 메뉴

문서 홈애플리케이션 개발MongoDB 드라이버Go 드라이버

필드의 고유 값 가져오기

Distinct() 메서드를 사용하여 컬렉션 전체의 필드에 대한 고유 값 목록을 조회할 수 있습니다.

Read the Usage Examples to learn how to run this example.

다음 예시에서는 movies 컬렉션에서 다음을 수행합니다.

  • directors에 "나탈리 포트만"이 포함된 문서를 일치시킵니다.

  • 일치하는 문서에서 title의 고유 값을 반환합니다.

coll := client.Database("sample_mflix").Collection("movies")
filter := bson.D{{"directors", "Natalie Portman"}}
// Retrieves the distinct values of the "title" field in documents
// that match the filter
results, err := coll.Distinct(context.TODO(), "title", filter)
// Prints a message if any errors occur during the operation
if err != nil {
panic(err)
}

완전히 실행 가능한 예제 보기

전체 예시를 실행하면 다음 값이 포함된 interface 유형의 빈 조각이 반환됩니다.

A Tale of Love and Darkness
New York, I Love You

고유 값 검색에 대한 자세한 내용은 고유 값 조회를 참조하세요.

Distinct()

← 문서 수 계산