简体   繁体   中英

MongoDB MapReduce issue in C#

I'm working on a relative trival MongoDB MapReduce demo in C# .

The code is as follows:

    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;
    }

Objects to deserialize to:

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; }
}

Here's how the BSON output looks like:

[0]: { "_id" : 1.0, "value" : { "count" : 3.0, "names" : "Games,Technologie,Auto" } }
[1]: { "_id" : 2.0, "value" : { "count" : 1.0, "names" : "Hoofdpunten" } }

However I keep getting the following exception:

An error occurred while deserializing the value property of class MetroNews.Managers.CategorySummaryResult:
 Expected element name to be '_t', not 'count'.

What's going wrong and how can I fix it ?

You cannot properly serialize/deserialize an ICollection.

It will serialize fine into a list of whatever objects but the problem occurs when you want to deserialize it.

The deserializer cannot instantiate an ICollection and does not know what specific type of to use without you specifying it through conventions.

you should change

public class CategorySummaryResult
{
   public double id { get; set; }
   public ICollection<CategorySummary> value { get; set; }
}

to something like

public class CategorySummaryResult
{
   public double id { get; set; }
   public List<CategorySummary> value { get; set; }
}

尝试将[BsonDiscriminatorAttribute(required=true)]CategorySummary类。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM