我正在進行相對調整MongoDB MapReduce demo in C#。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'.
怎麼回事和我該如何解決它?
Thx爲提示。我已經試過了,沒有雪茄:/ – David
,你確定你的mongodb沒有被舊數據「污染」? –
上面發佈的非反序列化數據看起來很好。 錯誤在於反序列化。 – David