2015-10-04 8 views
5

データベースにある各ドキュメントからマイナーなデータを取得する必要がありますが、 "テーブルスキャン"を防止するためにトラフィックを削減したいだけです。 。c#mongo 2.0 FindAsyncのトラフィックを減らす

私は「書籍」と言うことができるコレクションを持っています(誰もがそれを使って例を挙げているからです)。今、私の問題は与えられた著者の書籍タイトルだけです。

var filter = Builders<Book>.Filter.Eq(n => n.Author, AuthorId); 

      List<string> books = new List<string>(); 

      using (var cursor = await BooksCollection.FindAsync(filter)) 
      { 
       while (await cursor.MoveNextAsync()) 
       { 
        var batch = cursor.Current; 
        foreach (Book b in batch) 
         books.Add(b.Title); 
       } 
      } 

ただし、コレクション結果全体をスキャンすると、大きなデータが使用されますか?それらは本ではなく、グリッドネットワーク全体であり、各ドキュメントは約5〜10 MBで、何千ものものがあると仮定できます。別のコレクションに必要なこのデータを格納せずに、ここでトラフィックを減らすことはできますか?

私はSQLデータベースで "ビュー"と呼ばれると思います。返されたドキュメントはBsonDocumentオブジェクトの代わりであることを

var filter = Builders<Book>.Filter.Eq(n => n.Author, AuthorId); 
// Just project the Title and Author properties of each Book document 
var projection = Builders<Book>.Projection 
    .Include(b => b.Title) 
    .Include(b => b.Author) 
    .Exclude("_id"); // _id is special and needs to be explicitly excluded if not needed 
var options = new FindOptions<Book, BsonDocument> { Projection = projection }; 

List<string> books = new List<string>(); 

using (var cursor = await BooksCollection.FindAsync(filter, options)) 
{ 
    while (await cursor.MoveNextAsync()) 
    { 
     var batch = cursor.Current; 
     foreach (BsonDocument b in batch) 
      // Get the string value of the Title field of the BsonDocument 
      books.Add(b["Title"].AsString); 
    } 
} 

注:

答えて

8

あなたは、あなただけが必要なフィールドを含めるFindAsyncへのFindOptionsパラメータで設定できるprojectionを経由して返されたドキュメントのサイズを小さくすることができますBookオブジェクトには投影されたフィールドのみが含まれているためです。

関連する問題