Overview
In this guide, you can learn how to use the Rust driver to specify which fields to return from a read operation by using a projection. A projection is a document that specifies which fields MongoDB returns from a query.
Sample Data
The examples in this guide use the restaurants collection in the sample_restaurants database from the Atlas
sample datasets. To access this collection from your Rust application, create a Client that connects to an Atlas cluster and assign the following values to your database and collection variables:
let database = client.database("sample_restaurants"); let collection: Collection<Document> = database.collection("restaurants");
To learn how to create a free MongoDB Atlas cluster and load the sample datasets, see the MongoDB Get Started guide.
Projection Types
Use a projection to specify which fields to include or exclude in a returned document. You cannot combine inclusion and exclusion statements in a single projection, unless you exclude the _id field.
Specify Fields to Include
To include specific fields in a read operation result, specify the projection() method on the result of a find() method call. Pass a document to the projection() method that specifies the fields to include by using the following syntax. Specify a value of 1 to include a field in the returned document and a value of 0 to exclude a field.
.projection(doc! { "<field_name>": 1, // Includes the field in the returned document "_id": 0 // Excludes the _id field from the returned document })
The following example uses the find() method to find all restaurants in which the name field value is "Emerald Pub". The code then calls the projection() method to return only the name, cuisine, and borough fields of matching documents.
Select the Asynchronous or Synchronous tab to see the corresponding code for each runtime:
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")})
When using a projection to specify fields to include in the return document, the _id field is also included by default. All other fields are implicitly excluded. To remove the _id field from the return document, explicitly exclude it.
Exclude the _id Field
When specifying fields to include, you can also exclude the _id field from the returned document.
The following example performs the same query as the preceding example but excludes the _id field from the projection. Select the Asynchronous or Synchronous tab to see the corresponding code for each runtime:
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")})
Specify Fields to Exclude
To exclude specific fields from a read operation result, specify the projection() method on the result of a find() method call. Pass a document to the projection() method that specifies the fields to exclude by using the following syntax:
.projection(doc! { "<field_name>": 0 })
The following example uses the find() method to find all restaurants in which the name field value is "Emerald Pub". The code then calls the projection() method to omit the grades and address fields from the result. Select the Asynchronous or Synchronous tab to see the corresponding code for each runtime:
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")})
When using a projection to specify which fields to exclude, any unspecified fields are implicitly included in the return document.
Additional Information
To learn more about projections, see the Project Fields guide in the MongoDB Server manual.
API Documentation
To learn more about the find() method, see the find() API documentation.