2016-09-02 7 views
0

私はC#で検索メソッドを実装しています。問題はスコアのメタデータが結果に含まれておらず、最後の行で例外になることです。これはプロジェクトの句がなくても検索が完了しているようです。私はバージョン2.2.4.26のC#ドライバを使用しています。asyncを使用したmongodbクエリ投影の動作が異なります

[HttpPost] public async Task<JsonResult> SearchPost(string str) 
    { 
     _client = new MongoClient("mongodb://localhost:27017"); 
     _database = _client.GetDatabase("test"); 
     IMongoCollection<BsonDocument> collection = _database.GetCollection<BsonDocument>("test"); 

     MongoDB.Bson.BsonDocument searchDoc 
      = MongoDB.Bson.Serialization.BsonSerializer.Deserialize<BsonDocument>(
       "{$text:{$search:'" + str + "'}}"); 
     MongoDB.Bson.BsonDocument metaDoc 
      = MongoDB.Bson.Serialization.BsonSerializer.Deserialize<BsonDocument>(
       "{score: {$meta: 'textScore'}}"); 

     IFindFluent<BsonDocument, BsonDocument> query = collection.Find(searchDoc); 
     query.Project(metaDoc); 

     List<BsonDocument> col = await query.ToListAsync(); 
     foreach (BsonDocument doc in col) 
     { 
      jsonResult = doc.ToJson(); 
      double score = doc.FirstOrDefault(x => x.Name == "score").Value.AsDouble; 

私は同等の構文的に私には思えるこのように、クエリを設定した場合、私は戻ってスコア結果を得るか:

List<BsonDocument> col = await collection.Find(searchDoc).Project(metaDoc).ToListAsync(); 

正しい結果は次のようになります。

{ 
    "_id" : "41608a74-8434-45e4-8404-99922f761dae", 
    "Path" : "C:\\src\\ba\\mongo\\samples\\xml\\item_20081_v11", 
    "Files" : [ 
     "content_en-us.xml", 
     "content_es-mx.xml", 
     "metadata.xml", 
     "rubric.xml", 
     "template.xml", 
     "translation_en-us.xml", 
     "translation_es-mx.xml" 
    ], 
    "ItemXml" : "....be used as a Paramecium cell membrane ...", 
    "score" : 1.58333333333333 
} 

答えて

1

query.Project(metaDoc)の結果をクエリに戻す必要があります。 LINQと同様に、IFindFluentインターフェイスは不変です。

query = query.Project(metaDoc); 
+0

私はSQLパラメータコレクションのように考えていましたが、これはこのインターフェイスの仕組みではありません。私はFluent Assertionsのコンセプトに精通していませんでしたが、それは事だと思います...ありがとう! –

関連する問題