我正在用C#编写一个相对简单的MongoDB MapReduce演示。
代码如下:
public List<CategorySummaryResult> GetCategorySummaries()
{
string map = @"
function() {
var key = this.FeedType;
var value = {count: 1, names: this.Name};
emit(key, value);
}";
string reduce = @"
function(key, values) {
var result = {count: 0, names: ''};
values.forEach(function(value) {
result.count += value.count;
result.names += ',' + value.names;
});
return result;
}";
string finalize = @"
function(key, value) {
if (value.names.charAt(0) === ',')
value.names = value.names.substr(1);
return value;
}";
var options =
MapReduceOptions
.SetFinalize(finalize)
.SetOutput(MapReduceOutput.Inline);
var result =
_db.GetCollection("NewsCategories")
.MapReduce(map, reduce, options)
.GetInlineResultsAs<CategorySummaryResult>()
.ToList();
return result;
}
要反序列化的对象:
public class CategorySummaryResult
{
public double id { get; set; }
public ICollection<CategorySummary> value { get; set; }
}
public class CategorySummary
{
public double count { get; set; }
public string names { get; set; }
}
下面是 BSON 输出的样子:
[0]: { "_id" : 1.0, "value" : { "count" : 3.0, "names" : "Games,Technologie,Auto" } }
[1]: { "_id" : 2.0, "value" : { "count" : 1.0, "names" : "Hoofdpunten" } }
但是我不断收到以下异常:
An error occurred while deserializing the value property of class MetroNews.Managers.CategorySummaryResult:
Expected element name to be '_t', not 'count'.
出了什么问题,我该如何解决?