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

문서 찾기

FindOne() 메서드를 사용하면 컬렉션에서 단일 문서를 검색할 수 있습니다.

팁

이 예시를 실행하는 방법에 대해 알아보려면 사용 예시를 읽어보세요.

이 예시에서는 restaurants 컬렉션의 문서에 대한 모델로 다음 Restaurant 구조체를 사용합니다:

type Restaurant struct { ID primitive.ObjectID `bson:"_id"` Name string RestaurantId string `bson:"restaurant_id"` Cuisine string Address interface{} Borough string Grades []interface{}}

다음 예시에서는 name이(가) 'Bagels N Buns'인 restaurants collection의 문서를 매칭하여 일치하는 첫 번째 문서를 반환합니다.

coll := client.Database("sample_restaurants").Collection("restaurants") // Creates a query filter to match documents in which the "name" is// "Bagels N Buns"filter := bson.D{{"name", "Bagels N Buns"}} // Retrieves the first matching documentvar result Restauranterr = coll.FindOne(context.TODO(), filter).Decode(&result) // Prints a message if no documents are matched or if any// other errors occur during the operationif err != nil { if err == mongo.ErrNoDocuments { return } panic(err)}

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

전체 예시를 실행하면 result 변수에 Restaurant 구조체로 저장되는 다음 문서가 인쇄됩니다.

// results truncated{ "ID": "5eb3d668b31de5d588f42950", "Name": "Bagels N Buns", "RestaurantId": "40363427" "Address": [...], "Borough": "Staten Island", "Cuisine": "Delicatessen", "Grades": [...]}

쿼리 필터 지정 및 잠재적 오류 처리에 대해 자세히 알아보려면 데이터 조회를 참조하세요.

쿼리 연산자에 대해 자세히 알아보려면 MongoDB 쿼리 연산자 참고 설명서를 참조하세요.

FindOne()

이 페이지 평가하기