一意の ID に従ってコレクションからドキュメントを取得する、非常に単純なクエリを作成しています。いくつかの欲求不満の後(私はmongoとasync / awaitプログラミングモデルが初めてです)、私はこれを理解しました:
IMongoCollection<TModel> collection = // ...
FindOptions<TModel> options = new FindOptions<TModel> { Limit = 1 };
IAsyncCursor<TModel> task = await collection.FindAsync(x => x.Id.Equals(id), options);
List<TModel> list = await task.ToListAsync();
TModel result = list.FirstOrDefault();
return result;
それはうまくいきます!しかし、「検索」メソッドへの参照が引き続き表示され、これを解決しました。
IMongoCollection<TModel> collection = // ...
IFindFluent<TModel, TModel> findFluent = collection.Find(x => x.Id == id);
findFluent = findFluent.Limit(1);
TModel result = await findFluent.FirstOrDefaultAsync();
return result;
結局のところ、これもうまくいきます。
これらの結果を達成するために 2 つの異なる方法があることには、何らかの重要な理由があると確信しています。これらの方法論の違いは何ですか? また、どちらか一方を選択する必要があるのはなぜですか?