在MongoDB c# Driver 2.0中,c#中的聚合对某些属性返回null

本文关键字:属性 null 返回 Driver MongoDB | 更新日期: 2023-09-27 18:08:47

我正在编写一个MongoDB应用程序,我正在使用c#流复杂查询中的聚合管道。当我在shell中复制c#生成的聚合时,一切似乎都很好。然而,当在c#中执行聚合时,一些属性被设置为null

首先,让我给你看看我的模型:

public class Smartphone
{
    #region Properties
    [BsonRepresentation(BsonType.ObjectId)]
    public string Id { get; set; }
    [BsonElement("name")]
    [BsonIgnoreIfNull]
    [BsonIgnoreIfDefault]
    public string Name { get; set; }
    [BsonElement("description")]
    [BsonIgnoreIfNull]
    [BsonIgnoreIfDefault]
    public string Description { get; set; }
    [BsonElement("typenr")]
    [BsonIgnoreIfNull]
    [BsonIgnoreIfDefault]
    public string Type { get; set; }
    [BsonElement("props")]
    [BsonIgnoreIfNull]
    [BsonIgnoreIfDefault]
    public List<SmartphoneProperty> Properties { get; set; }
    #endregion
}
public class UnwindedSmartphone
{
    #region Properties
    [BsonRepresentation(BsonType.ObjectId)]
    public string Id { get; set; }
    [BsonElement("name")]
    [BsonIgnoreIfNull]
    [BsonIgnoreIfDefault]
    public string Name { get; set; }
    [BsonElement("description")]
    [BsonIgnoreIfNull]
    [BsonIgnoreIfDefault]
    public string Description { get; set; }
    [BsonElement("typenr")]
    [BsonIgnoreIfNull]
    [BsonIgnoreIfDefault]
    public string Type { get; set; }
    [BsonElement("props")]
    [BsonIgnoreIfNull]
    [BsonIgnoreIfDefault]
    public SmartphoneProperty Property { get; set; }
    #endregion
}
public class SmartphoneProperty
{
    #region Properties
    [BsonElement("type")]
    [BsonIgnoreIfNull]
    [BsonIgnoreIfDefault]
    public string Type { get; set; }
    [BsonElement("value")]
    [BsonIgnoreIfNull]
    [BsonIgnoreIfDefault]
    public string Value { get; set; }
    #endregion
}

我的集合中的单个文档看起来像这样:

{
    "_id" : ObjectId("55d45c0285afc59c146f66f0"),
    "name" : "LG Nexus 6",
    "description" : "A Nexus 6 device, created by Google.",
    "typenr" : "LG-NEX-5/BLACK",
    "props" : [ 
        {
            "type" : "os",
            "value" : "Android"
        }, 
        {
            "type" : "storage",
            "value" : "8"
        }, 
        {
            "type" : "storage",
            "value" : "16"
        }, 
        {
            "type" : "storage",
            "value" : "32"
        }, 
        {
            "type" : "storage",
            "value" : "64"
        }
    ]
}

需要执行的聚合命令如下:

// Get all the amount of filters that are defined.
db.smartphones.aggregate([
    // Unwind the "props".
    { "$unwind" : "$props" },
    // Grouping phase.
    // Group by unique properties, add a count for the amount of articles, and add articles to an element named "articles".
    // We use the function "$addToSet" here to ensure that only unique articles are being added.
    { 
        "$group" : { 
            "_id" : "$props", 
            count : { "$sum" : 1 }, 
            articles: { 
                "$addToSet": { 
                    name: "$name", 
                    description: "$description", 
                    typenr: "$typenr" 
                } 
            } 
        } 
    },
    // Sort the results based on the "_id" field.
    { "$sort" : { "_id" : 1 } }
]);

这个聚合的一个结果返回如下内容:

"_id" : {
    "type" : "storage",
    "value" : "128"
},
"count" : 1.0000000000000000,
"articles" : [ 
    {
        "name" : "Apple iPhone 6",
        "description" : "An iPhone 6 device, created by Apple.",
        "typenr" : "APP-IPHONE-6/BLACK"
    }
]

现在,在c#中,我以以下方式编写了聚合:

var aggregation = collection.Aggregate()
    .Unwind<Smartphone, UnwindedSmartphone>(x => x.Properties)
    .Group(key => key.Property, g => new
    {
        Id = g.Key,
        count = g.Count(),
        articles = g.Select(x => new
        {
            name = x.Name,
            description = x.Description,
            typenr = x.Type
        }).Distinct()
    })
    .SortBy(x => x.Id);

如果我检查这个聚合转换为的命令,它看起来如下:

// Get all the amount of filters that are defined.
db.smartphones.aggregate([
    // Unwind the "props".
    { "$unwind" : "$props" }, 
    // Grouping phase.
    // Group by unique properties, add a count for the amount of articles, and add articles to an element named "articles".
    // We use the function "$addToSet" here to ensure that only unique articles are being added.
    { 
        "$group" : { 
            "_id" : "$props", 
            "count" : { "$sum" : 1 }, 
            "articles" : { 
                "$addToSet" : { 
                    "name" : "$name", 
                    "description" : "$description", 
                    "typenr" : "$typenr" 
                } 
            } 
        } 
    }, 
    { "$sort" : { "_id" : 1 } }
])

因此,它与我试图转换为c#代码的原始聚合相同,除了属性被双引号包围之外,但这不是问题。

如果我在shell中执行这个聚合,结果很好。然而,当它在c#中执行时,articles属性对于name, descriptiontypenr具有null值。

有谁知道这是为什么吗?

在MongoDB c# Driver 2.0中,c#中的聚合对某些属性返回null

Ok,

感谢@BlakesSeven给我指出了正确的方向(看看他对我的问题的评论,我现在知道为什么我有那个特殊的问题了)。

我需要转换select语句中的group可能不会返回匿名类型。相反,我需要返回一个类型化的对象。

这意味着我的代码需要修改如下:

var aggregation = collection.Aggregate()
    .Unwind<Smartphone, UnwindedSmartphone>(x => x.Properties)
    .Group(key => key.Property, g => new
    {
        Count = g.Count(),
        Articles = g.Select(x => new AggregatedSmartphoneArticle
        {
            Name = x.Name,
            Description = x.Description,
            Type = x.Type
        }).Distinct()
    });

现在一切正常。再次感谢。使用类型化代码比使用BsonDocument要清晰得多。