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

カーソルからデータにアクセスする

このガイドでは、 .NET/ C#ドライバーを使用して、カーソルからデータにアクセスする方法を学習できます。

カーソルは、読み取り操作の結果を反復可能なバッチで返すツールです。カーソルは一度にドキュメントのサブセットのみを保持するため、メモリ消費とネットワーク帯域幅使用量の両方が削減されます。

FindSync() メソッドまたは FindAsync() メソッドを使用してカーソルを取得できます。また、ToCursor() メソッドまたは ToCursorAsync() メソッドを連結して、Find() メソッドの結果をカーソルに変換することもできます。

The examples in this guide use the restaurants collection in the sample_restaurants database provided in the Atlas sample datasets. To learn how to create a free MongoDB Atlas cluster and load the sample datasets, see the Get Started with the .NET/C# Driver tutorial.

このページの例では、次の Restaurantオブジェクトを使用して、 restaurantsコレクション内のドキュメントをモデル化します。

[BsonIgnoreExtraElements]
public class Restaurant
{
public ObjectId Id { get; set; }
[BsonElement("name")]
public string Name { get; set; }
}

カーソルの内容を反復処理するには、using ブロック内で foreach ループを使用します。次の例では、restaurantsコレクションから nameフィールドの値が "Starbucks" であるドキュメントを検索し、その結果を反復処理します。対応するコードを表示するには、Synchronous タブまたは Asynchronousタブを選択します。

var filter = Builders<Restaurant>.Filter.Eq(r => r.Name, "Starbucks");
using (var cursor = collection.FindSync(filter))
{
while (cursor.MoveNext())
{
foreach (var restaurant in cursor.Current)
{
Console.WriteLine(restaurant.Name);
}
}
}

次の例では同じ操作を実行しますが、ToCursor() メソッドを使用します。対応するコードを表示するには、Synchronous タブまたは Asynchronousタブを選択します。

var filter = Builders<Restaurant>.Filter.Eq(r => r.Name, "Starbucks");
using (var cursor = collection.Find(filter).ToCursor())
{
while (cursor.MoveNext())
{
foreach (var restaurant in cursor.Current)
{
Console.WriteLine(restaurant.Name);
}
}
}

警告

If the number and size of documents returned by your query exceeds available application memory, your program might crash. If you expect a large result set, access your cursor iteratively.

カーソルからすべてのドキュメントを検索するには、次の例に示すように ToList() メソッドを使用します。対応するコードを表示するには、Synchronous タブまたは Asynchronousタブを選択します。

var filter = Builders<Restaurant>.Filter.Eq(r => r.Name, "Dunkin' Donuts");
var results = collection.FindSync(filter).ToList();

When querying on a capped collection, you can use a tailable cursor that remains open after the client exhausts the results in a cursor. To create a tailable cursor, create a FindOptions object and set the CursorType property to CursorType.TailableAwait. Then, pass the FindOptions object to one of the find operation methods. The following example shows how to create a tailable cursor on a capped collection. Select the Synchronous or Asynchronous tab to see the corresponding code:

var filter = Builders<Restaurant>.Filter.Eq(r => r.Name, "Dunkin' Donuts");
var options = new FindOptions<Restaurant>
{
CursorType = CursorType.TailableAwait
};
using (var cursor = collection.FindSync(filter, options))
{
while (cursor.MoveNext())
{
foreach (var restaurant in cursor.Current)
{
Console.WriteLine(restaurant.Name);
}
}
}

このガイドで使用されているメソッドとクラスの詳細については、次のAPIドキュメントを参照してください。