2017-09-27 77 views
1

我想從我的C#代碼運行MongoDB MapReduce,很不幸,我無法獲得任何結果。
如果我直接在MongoDB shell中運行相同的MapReduce,一切正常。
任何幫助,將不勝感激。C#中的MongoDB MapReduce不會返回任何結果

下面是關於我的問題的數據:

  • C#驅動程序版本:2.4.4
  • MongoDB的版本:3.4.5例輸入文檔的
  • 'DocInput'
{ 
    "_id" : ObjectId("59c52b3cb602cb6397c2ec9d"), 
    "Timestamp" : NumberLong(1505860144116), 
    "Value" : 14, 
    "Date" : ISODate("2017-09-19T22:29:04.116Z") 
} 
  • MapReduce by JS
function mapF() { 
    const key = this.Date.getFullYear(); 
    const valuePerYear = { total: 1}; 

    emit(key, valuePerYear) 
};  

function reduceF(year, values) { 
    let sum = 0 
    values.forEach(v => { 
     sum += v.total; 
    }); 
    return {total: NumberInt(sum)}; 
} 

db 
    .getCollection('DocInput') 
    .mapReduce(mapF, 
       reduceF, 
       { 
        out: { reduce: "Result", nonAtomic: true }, 
       }); 
  • MapReduce的通過C#
public class Program 
{ 
    private const string MapJs = @"function mapF() { 
    const key = this.Date.getFullYear(); 
    const valuePerYear = { total: 1}; 

    emit(key, valuePerYear) 
}; "; 

    private const string ReduceJS = @"function reduceF(year, values) { 
    let sum = 0; 
    values.forEach(v => { 
     sum += v.total; 
    }); 
    return {total: NumberInt(sum)}; 
}"; 

    public void Main1() 
    { 
     string mongoConnectionString = "my-connection-string"; 
     MongoUrl mongoUrl = MongoUrl.Create(mongoConnectionString); 
     MongoClient client = new MongoClient(mongoConnectionString); 
     IMongoDatabase db = client.GetDatabase(mongoUrl.DatabaseName); 
     IMongoCollection<BsonDocument> collection = db.GetCollection<BsonDocument>("DocInput"); 
     BsonJavaScript map = new BsonJavaScript(MapJs); 
     BsonJavaScript reduce = new BsonJavaScript(ReduceJS); 
     FilterDefinitionBuilder<BsonDocument> filterBuilder = new FilterDefinitionBuilder<BsonDocument>(); 
     FilterDefinition<BsonDocument> filter = filterBuilder.Empty; 
     MapReduceOptions<BsonDocument, BsonDocument> options = new MapReduceOptions<BsonDocument, BsonDocument> 
     { 
      Filter = filter, 
      MaxTime = TimeSpan.FromMinutes(1), 
      OutputOptions = MapReduceOutputOptions.Reduce("Result", nonAtomic: true), 
      Verbose = true 
     }; 
     try 
     { 
      collection.MapReduce(map, reduce, options).ToList(); 
     } 
     catch (Exception ex) 
     { 
      Console.WriteLine($"Exception occurred {ex.Message}"); 
     } 
    } 
} 

感謝Alex Blex建議我異形兩個MapReduce的執行。 這裏的結果:

  • MongoDB的shell執行
{ 
    "op" : "command", 
    "ns" : "stat.Result", 
    "command" : { 
     "mapreduce" : "DocInput", 
     "map" : function mapF() { 
    const key = this.Date.getFullYear(); 
    const valuePerYear = { total: 1}; 

    emit(key, valuePerYear); 
}, 
     "reduce" : function reduceF(year, values) { 
    let sum = 0; 
    values.forEach(v => { 
     sum += v.total; 
    }); 
    return {total: NumberInt(sum)}; 
}, 
     "out" : { 
      "reduce" : "Result", 
      "nonAtomic" : true 
     } 
    }, 
    "keysExamined" : 0, 
    "docsExamined" : 0, 
    "numYield" : 1440, 
    "locks" : { 
     "Global" : { 
      "acquireCount" : { 
       "r" : NumberLong(6507), 
       "w" : NumberLong(15), 
       "W" : NumberLong(2) 
      } 
     }, 
     "Database" : { 
      "acquireCount" : { 
       "r" : NumberLong(10), 
       "w" : NumberLong(7), 
       "R" : NumberLong(3235), 
       "W" : NumberLong(11) 
      } 
     }, 
     "Collection" : { 
      "acquireCount" : { 
       "r" : NumberLong(10), 
       "w" : NumberLong(10) 
      } 
     }, 
     "Metadata" : { 
      "acquireCount" : { 
       "W" : NumberLong(1) 
      } 
     } 
    }, 
    "responseLength" : 115, 
    "protocol" : "op_command", 
    "millis" : 1751, 
    "planSummary" : "COUNT", 
    "execStats" : { 
     "stage" : "COUNT", 
     "nReturned" : 0, 
     "executionTimeMillisEstimate" : 0, 
     "works" : 1, 
     "advanced" : 0, 
     "needTime" : 0, 
     "needYield" : 0, 
     "saveState" : 0, 
     "restoreState" : 0, 
     "isEOF" : 1, 
     "invalidates" : 0, 
     "nCounted" : 1, 
     "nSkipped" : 0 
    }, 
    "ts" : ISODate("2017-09-28T07:22:07.678Z"), 
    "client" : "127.0.0.1", 
    "appName" : "MongoDB Shell", 
    "allUsers" : [ 
     { 
      "user" : "statrw", 
      "db" : "stat" 
     } 
    ], 
    "user" : "[email protected]" 
} 
  • C#執行
{ 
    "op" : "command", 
    "ns" : "stat.Result", 
    "command" : { 
     "mapreduce" : "DocInput", 
     "map" : function mapF() { 
    const key = this.Date.getFullYear(); 
    const valuePerYear = { total: 1}; 

    emit(key, valuePerYear); 
};, 
     "reduce" : function reduceF(year, values) { 
    let sum = 0; 
    values.forEach(v => { 
     sum += v.total; 
    }); 
    return {total: NumberInt(sum)}; 
}, 
     "out" : { 
      "reduce" : "Result", 
      "db" : "statdb", 
      "nonAtomic" : true 
     }, 
     "query" : {}, 
     "scope" : {}, 
     "jsMode" : true, 
     "verbose" : true, 
     "maxTimeMS" : 60000.0 
    }, 
    "keysExamined" : 0, 
    "docsExamined" : 179395, 
    "numYield" : 1408, 
    "locks" : { 
     "Global" : { 
      "acquireCount" : { 
       "r" : NumberLong(6432), 
       "w" : NumberLong(12), 
       "W" : NumberLong(2) 
      } 
     }, 
     "Database" : { 
      "acquireCount" : { 
       "r" : NumberLong(6), 
       "w" : NumberLong(4), 
       "R" : NumberLong(3203), 
       "W" : NumberLong(11) 
      } 
     }, 
     "Collection" : { 
      "acquireCount" : { 
       "r" : NumberLong(6), 
       "w" : NumberLong(7) 
      } 
     }, 
     "Metadata" : { 
      "acquireCount" : { 
       "W" : NumberLong(1) 
      } 
     } 
    }, 
    "responseLength" : 243, 
    "protocol" : "op_query", 
    "millis" : 321, 
    "planSummary" : "COLLSCAN", 
    "execStats" : { 
     "stage" : "COLLSCAN", 
     "nReturned" : 179395, 
     "executionTimeMillisEstimate" : 74, 
     "works" : 179397, 
     "advanced" : 179395, 
     "needTime" : 1, 
     "needYield" : 0, 
     "saveState" : 3201, 
     "restoreState" : 3201, 
     "isEOF" : 1, 
     "invalidates" : 0, 
     "direction" : "forward", 
     "docsExamined" : 179395 
    }, 
    "ts" : ISODate("2017-09-28T07:23:20.567Z"), 
    "client" : "127.0.0.1", 
    "allUsers" : [ 
     { 
      "user" : "statrw", 
      "db" : "stat" 
     } 
    ], 
    "user" : "[email protected]" 
} 
+0

從分析器結果來看,C#將'Result'存儲在'statdb'數據庫中。你知道db名字來自哪裏嗎?無論如何,試着明確地設置它:'MapReduceOutputOptions.Reduce(「Result」,mongoUrl.DatabaseName,nonAtomic:true),' –

+0

不幸的是即使你的建議我不能得到任何簡化的文件內的「結果」。此外,我注意到,如果C#執行不存在,則C#執行會創建「結果」集合。但它是空的。 –

+0

那麼,我看到的唯一區別(除輸出數據庫和'emit'之外的額外分號,我認爲這是一個錯字)是額外的參數:''query「:{},」scope「:{}, 「jsMode」:true,「verbose」:true,「maxTimeMS」:60000.0'。我建議在shell請求中指定它們以確認它仍然有效。 –

回答

0

正如你可以在這個MongoDB ticket看到,羅伯特·斯塔姆發現了一個解決方法:如果你刪除e地圖中的尾隨分號和減少函數,一切工作正常。

但是他寫道他打算打開服務器票據,因爲無聲失敗是一個問題。