AI エージェント向け: ドキュメントインデックスは https://www.mongodb.com/ja-jp/docs/llms.txt で利用できます。すべてのページの markdown バージョンは、いずれかの URL パスに .md を追加することで利用できます。
Docs Menu

ドキュメントの検索

You can retrieve a single document from a collection by calling the find_one() method on a Collection instance.

Pass a query filter to the find_one() method to return one document in the collection that matches the filter. If multiple documents match the query filter, this method returns the first matching document according to their natural order in the database or according to the sort order specified in a FindOneOptions instance.

The find_one() method returns an Option<T> type, where T is the type with which you parameterized your Collection instance.

ドキュメント取得の詳細については、「データ取得」ガイドを参照してください。

この例では、 sample_restaurantsデータベース内のrestaurantsコレクションからクエリフィルターに一致するドキュメントを検索します。 この例では、検索されたドキュメントのデータをRestaurant構造体に入力します。

この例では、 nameフィールドの値が"Tompkins Square Bagels"であるドキュメントに一致するクエリフィルターを使用します。 MongoDB は、クエリフィルターに一致する最初のドキュメントを検索します。

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

use mongodb::{
bson::doc,
Client,
Collection
};
use serde::{ Deserialize, Serialize };
#[derive(Serialize, Deserialize, Debug)]
struct Restaurant {
name: String,
cuisine: String,
}
#[tokio::main]
async fn main() -> mongodb::error::Result<()> {
let uri = "<connection string>";
let client = Client::with_uri_str(uri).await?;
let my_coll: Collection<Restaurant> = client
.database("sample_restaurants")
.collection("restaurants");
let result = my_coll.find_one(
doc! { "name": "Tompkins Square Bagels" },
None
).await?;
println!("{:#?}", result);
Ok(())
}
use mongodb::{
bson::doc,
sync::{Client, Collection}
};
use serde::{ Deserialize, Serialize };
#[derive(Serialize, Deserialize, Debug)]
struct Restaurant {
name: String,
cuisine: String,
}
fn main() -> mongodb::error::Result<()> {
let uri = "<connection string>";
let client = Client::with_uri_str(uri)?;
let my_coll: Collection<Restaurant> = client
.database("sample_restaurants")
.collection("restaurants");
let result = my_coll.find_one(
doc! { "name": "Tompkins Square Bagels" },
None
)?;
println!("{:#?}", result);
Ok(())
}
このページを評価