개요
이 가이드 에서는 프로젝션 사용하여 Rust 운전자 사용하여 읽기 작업에서 반환할 필드를 지정하는 방법을 학습 수 있습니다. 프로젝션 MongoDB 쿼리 에서 반환하는 필드를 지정하는 문서 입니다.
샘플 데이터
이 가이드 의 예제에서는 restaurants sample_restaurants Atlas 샘플 데이터 세트의 데이터베이스 에 있는 컬렉션 사용합니다. Rust 애플리케이션 에서 이 컬렉션 액세스 하려면 Client Atlas cluster 에 연결하는 를 만들고 및 변수에 다음 값을 할당합니다.database collection
let database = client.database("sample_restaurants"); let collection: Collection<Document> = database.collection("restaurants");
무료 MongoDB Atlas 클러스터 생성하고 샘플 데이터 세트를 로드하는 방법을 학습하려면 MongoDB 시작하기 가이드 를 참조하세요.
프로젝션 유형
프로젝션 사용하여 반환된 문서 에 포함하거나 제외할 필드를 지정합니다. _id 필드 제외하지 않는 한 포함 및 제외 문을 단일 프로젝션 에 결합할 수 없습니다.
포함할 필드 지정
읽기 작업 결과에 특정 필드를 포함하려면 find() 메서드 호출 결과에 projection() 메서드를 지정합니다. 다음 구문을 사용하여 포함할 필드를 지정하는 projection() 메서드에 문서 전달합니다. 반환된 문서 에 필드 포함하려면 1 값을 지정하고 필드 제외하려면 0 값을 지정합니다.
.projection(doc! { "<field_name>": 1, // Includes the field in the returned document "_id": 0 // Excludes the _id field from the returned document })
다음 예시 find() 메서드를 사용하여 name 필드 값이 "Emerald
Pub"인 모든 레스토랑을 찾습니다. 그런 다음 이 코드는 projection() 메서드를 호출하여 일치하는 문서의 name, cuisine, borough 필드만 반환합니다.
Asynchronous 또는 Synchronous 탭을 선택하여 각 런타임에 해당하는 코드를 확인합니다.
let filter = doc! { "name": "Emerald Pub" }; let mut cursor = collection .find(filter) .projection(doc! { "name": 1, "cuisine": 1, "borough": 1 }) .await?; while let Some(result) = cursor.try_next().await? { println!("{:?}", result); }
Document({"_id": ObjectId("..."), "borough": String("Manhattan"), "cuisine": String("American"), "name": String("Emerald Pub")}) Document({"_id": ObjectId("..."), "borough": String("Queens"), "cuisine": String("American"), "name": String("Emerald Pub")})
let filter = doc! { "name": "Emerald Pub" }; let mut cursor = collection .find(filter) .projection(doc! { "name": 1, "cuisine": 1, "borough": 1 }) .run()?; for result in cursor { println!("{:?}", result?); }
Document({"_id": ObjectId("..."), "borough": String("Manhattan"), "cuisine": String("American"), "name": String("Emerald Pub")}) Document({"_id": ObjectId("..."), "borough": String("Queens"), "cuisine": String("American"), "name": String("Emerald Pub")})
프로젝션 사용하여 반환 문서 에 포함할 필드를 _id 지정하는 경우 기본값 으로 필드 도 포함됩니다. 다른 모든 필드는 암시적으로 제외됩니다._id 반환 문서 에서 필드 제거하려면 명시적으로 제외합니다.
_id 필드 제외
포함할 필드를 지정할 때 반환된 문서에서 _id 필드를 제외할 수도 있습니다.
다음 예시 이전 예시 와 동일한 쿼리 수행하지만 프로젝션 에서 _id 필드 제외합니다. Asynchronous 또는 Synchronous 탭 선택하여 각 런타임에 해당하는 코드를 확인합니다.
let filter = doc! { "name": "Emerald Pub" }; let mut cursor = collection.find(filter) .projection(doc! { "name": 1, "cuisine": 1, "borough": 1, "_id": 0 }) .await?; while let Some(result) = cursor.try_next().await? { println!("{:?}", result); }
Document({"borough": String("Manhattan"), "cuisine": String("American"), "name": String("Emerald Pub")}) Document({"borough": String("Queens"), "cuisine": String("American"), "name": String("Emerald Pub")})
let filter = doc! { "name": "Emerald Pub" }; let mut cursor = collection.find(filter) .projection(doc! { "name": 1, "cuisine": 1, "borough": 1, "_id": 0 }) .run()?; for result in cursor { println!("{:?}", result?); }
Document({"borough": String("Manhattan"), "cuisine": String("American"), "name": String("Emerald Pub")}) Document({"borough": String("Queens"), "cuisine": String("American"), "name": String("Emerald Pub")})
제외할 필드 지정
읽기 작업 결과에서 특정 필드를 제외하려면 find() 메서드 호출 결과에 projection() 메서드를 지정합니다. 다음 구문을 사용하여 제외할 필드를 지정하는 projection() 메서드에 문서 전달합니다.
.projection(doc! { "<field_name>": 0 })
다음 예시 find() 메서드를 사용하여 name 필드 값이 "Emerald
Pub"인 모든 레스토랑을 찾습니다. 그런 다음 이 코드는 projection() 메서드를 호출하여 결과에서 grades 및 address 필드를 생략합니다. Asynchronous 또는 Synchronous 탭 선택하여 각 런타임에 해당하는 코드를 확인합니다.
let filter = doc! { "name": "Emerald Pub" }; let mut cursor = collection.find(filter) .projection(doc! { "grades": 0, "address": 0 }) .await?; while let Some(result) = cursor.try_next().await? { println!("{:?}", result); }
Document({"_id": ObjectId("..."), "borough": String("Manhattan"), "cuisine": String("American"), "name": String("Emerald Pub"), "restaurant_id": String("40367329")}) Document({"_id": ObjectId("..."), "borough": String("Queens"), "cuisine": String("American"), "name": String("Emerald Pub"), "restaurant_id": String("40668598")})
let filter = doc! { "name": "Emerald Pub" }; let mut cursor = collection.find(filter) .projection(doc! { "grades": 0, "address": 0 }) .run()?; for result in cursor { println!("{:?}", result?); }
Document({"_id": ObjectId("..."), "borough": String("Manhattan"), "cuisine": String("American"), "name": String("Emerald Pub"), "restaurant_id": String("40367329")}) Document({"_id": ObjectId("..."), "borough": String("Queens"), "cuisine": String("American"), "name": String("Emerald Pub"), "restaurant_id": String("40668598")})
프로젝션 사용하여 제외할 필드를 지정하는 경우 지정되지 않은 모든 필드는 반환 문서 에 암시적으로 포함됩니다.
추가 정보
프로젝션에 학습 보려면 MongoDB Server 매뉴얼의 프로젝트 필드 가이드 를 참조하세요.
API 문서
메서드에 대해 find() 자세히 학습 find() API 설명서를 참조하세요.