我正在编写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
属性的null
,name
和description
的值为typenr
。
任何知道为什么会这样的人?
答案 0 :(得分:1)
确定,
感谢@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
更清晰。