Join us at MongoDB.local London on 7 May to unlock new possibilities for your data. Use WEB50 to save 50%.
Register now >
Docs Menu
Docs Home
/ /

返すフィールドを指定する

このガイドでは、 Rustドライバーを使用して、プロジェクションを使用して読み取り操作から返されるフィールドを指定する方法を学習できます。プロジェクションは、 MongoDB がクエリから返すフィールドを指定するドキュメントです。

このガイドの例では、restaurants sample_restaurantsAtlasサンプルデータセットの データベース内の コレクションを使用します。 Rustアプリケーションからこのコレクションにアクセスするには、AtlasClient クラスターに接続する を作成し、次の値を 変数と 変数に割り当てます。databasecollection

let database = client.database("sample_restaurants");
let collection: Collection<Document> = database.collection("restaurants");

MongoDB Atlasクラスターを無料で作成して、サンプルデータセットをロードする方法については、MongoDBを使い始めるガイドを参照してください。

プロジェクションを使用して、返されるドキュメントに含めるフィールドと除外するフィールドを指定します。 _idフィールドを除外しない限り、包含ステートメントと除外ステートメントを 1 つのプロジェクションに組み合わせることはできません。

読み取り操作結果に特定のフィールドを含めるには、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() メソッドを呼び出して、一致するドキュメントの namecuisineborough フィールドのみを返します。

AsynchronousSynchronous各実行時に対応するコードを表示するには、 タブまたは タブを選択します。

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フィールドを除外しています。各実行時に対応するコードを表示するには、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マニュアルの「プロジェクト フィールド 」ガイドを参照してください。

find()メソッドの詳細については、 find() APIドキュメントを参照してください。

戻る

返すドキュメントを指定する

項目一覧