我正在尝试从我的C#代码运行MongoDB MapReduce,但遗憾的是我无法获得任何结果。
如果我直接在MongoDB shell中运行相同的MapReduce,一切正常
任何帮助将不胜感激。
以下是有关我的问题的数据:
{
"_id" : ObjectId("59c52b3cb602cb6397c2ec9d"),
"Timestamp" : NumberLong(1505860144116),
"Value" : 14,
"Date" : ISODate("2017-09-19T22:29:04.116Z")
}
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 },
});
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的执行情况。 结果如下:
{
"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" : "statrw@stat"
}
{
"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" : "statrw@stat"
}
答案 0 :(得分:0)
正如您在此MongoDB ticket中所看到的,Robert Stam发现了一种解决方法:如果删除了地图中的尾随分号并减少了功能,那么一切正常。
然而,他写道,他打算开一张服务器票,因为无声失败是一个问题。