mongo C# 驱动程序上的 unwind/project 出现问题
Trouble with an unwind/project on mongo C# driver
我正在尝试从报告中获取所有点的数组,以便可以映射它们。为此,我需要一个 unwind/projection,但我正在努力解决它。
结果格式如下..
"totalPoints": 200,
"points": [
{
"itemId": "5e9592edb4959a52b8493f6d",
"itemName": "Numquam hic minus repellat sequi.",
"name": null,
"longitude": null,
"latitude": null
},
...
如您所见,我得到了底部 3 项的空值。
这是我正在使用的查询
var points = await _reportDocs.Aggregate()
.Match(filter.GenerateFilter())
.Unwind<IntelReport, IntelReportLocationUnwound>(x => x.Locations)
.Project(x => new LocationDto
{
ItemId = x.Id,
ItemName = x.Name,
Name = x.Location.Name,
Latitude = x.Location.Point.Coordinates[1],
Longitude = x.Location.Point.Coordinates[0]
})
.ToListAsync();
这是原始报告class(但我删除了一些此处不需要的内容)
public class IntelReport
{
[BsonId]
[BsonRepresentation(BsonType.ObjectId)]
public string Id { get; set; }
public string ReportTitle { get; set; }
public ICollection<Location> Locations { get; set; }
}
和 classes 我认为我需要放松...
public class LocationDto
{
[BsonRepresentation(BsonType.ObjectId)]
public string ItemId { get; set; }
public string ItemName { get; set; }
public string Name { get; set; }
public double? Longitude { get; set; }
public double? Latitude { get; set; }
}
[BsonIgnoreExtraElements]
public class IntelReportLocationUnwound
{
[BsonId]
[BsonRepresentation(BsonType.ObjectId)]
public string Id { get; set; }
[BsonElement("ReportTitle")]
[BsonIgnoreIfDefault]
public string Name { get; set; }
[BsonElement("locations")]
[BsonIgnoreIfDefault]
public LocationForAgg Location { get; set; }
}
public class LocationForAgg
{
[BsonElement("name")]
[BsonIgnoreIfDefault]
public string Name { get; set; }
[BsonElement("point")]
[BsonIgnoreIfDefault]
public PointForAgg Point { get; set; }
}
public class PointForAgg
{
[BsonElement("coordinates")]
[BsonIgnoreIfDefault]
public double[] Coordinates { get; set; }
}
$unwind 将子文档数组转换为单个子文档,因此 .Unwind<IntelReport, IntelReportLocationUnwound>(x => x.Locations)
之后的数据将如下所示:
{
"_id" : ObjectId("5e968bd5cc41498362cee2d2"),
"ReportTitle" : "aa",
"Locations" : {
"ItemId" : ObjectId("5e968bd5cc41498362cee2d3"),
"ItemName" : "Name",
"Name" : "N",
"Longitude" : 15,
"Latitude" : -24
}
}
这意味着您必须重新考虑 IntelReportLocationUnwound
class 定义。您可以使用与 IntelReport
中相同的类型,因为 MongoDB 只会将其从数组转换为嵌套对象:
public class IntelReportLocationUnwound
{
[BsonId]
[BsonRepresentation(BsonType.ObjectId)]
public string Id { get; set; }
[BsonElement("ReportTitle")]
[BsonIgnoreIfDefault]
public string Name { get; set; }
[BsonElement("Locations")]
[BsonIgnoreIfDefault]
public Location Location { get; set; }
}
然后你可以修改你的聚合:
.Project(x => new LocationDto
{
ItemId = x.Id,
ItemName = x.Name,
Name = x.Location.Name,
Latitude = x.Location.Latitude,
Longitude = x.Location.Longitude
})
我正在尝试从报告中获取所有点的数组,以便可以映射它们。为此,我需要一个 unwind/projection,但我正在努力解决它。
结果格式如下..
"totalPoints": 200,
"points": [
{
"itemId": "5e9592edb4959a52b8493f6d",
"itemName": "Numquam hic minus repellat sequi.",
"name": null,
"longitude": null,
"latitude": null
},
...
如您所见,我得到了底部 3 项的空值。
这是我正在使用的查询
var points = await _reportDocs.Aggregate()
.Match(filter.GenerateFilter())
.Unwind<IntelReport, IntelReportLocationUnwound>(x => x.Locations)
.Project(x => new LocationDto
{
ItemId = x.Id,
ItemName = x.Name,
Name = x.Location.Name,
Latitude = x.Location.Point.Coordinates[1],
Longitude = x.Location.Point.Coordinates[0]
})
.ToListAsync();
这是原始报告class(但我删除了一些此处不需要的内容)
public class IntelReport
{
[BsonId]
[BsonRepresentation(BsonType.ObjectId)]
public string Id { get; set; }
public string ReportTitle { get; set; }
public ICollection<Location> Locations { get; set; }
}
和 classes 我认为我需要放松...
public class LocationDto
{
[BsonRepresentation(BsonType.ObjectId)]
public string ItemId { get; set; }
public string ItemName { get; set; }
public string Name { get; set; }
public double? Longitude { get; set; }
public double? Latitude { get; set; }
}
[BsonIgnoreExtraElements]
public class IntelReportLocationUnwound
{
[BsonId]
[BsonRepresentation(BsonType.ObjectId)]
public string Id { get; set; }
[BsonElement("ReportTitle")]
[BsonIgnoreIfDefault]
public string Name { get; set; }
[BsonElement("locations")]
[BsonIgnoreIfDefault]
public LocationForAgg Location { get; set; }
}
public class LocationForAgg
{
[BsonElement("name")]
[BsonIgnoreIfDefault]
public string Name { get; set; }
[BsonElement("point")]
[BsonIgnoreIfDefault]
public PointForAgg Point { get; set; }
}
public class PointForAgg
{
[BsonElement("coordinates")]
[BsonIgnoreIfDefault]
public double[] Coordinates { get; set; }
}
$unwind 将子文档数组转换为单个子文档,因此 .Unwind<IntelReport, IntelReportLocationUnwound>(x => x.Locations)
之后的数据将如下所示:
{
"_id" : ObjectId("5e968bd5cc41498362cee2d2"),
"ReportTitle" : "aa",
"Locations" : {
"ItemId" : ObjectId("5e968bd5cc41498362cee2d3"),
"ItemName" : "Name",
"Name" : "N",
"Longitude" : 15,
"Latitude" : -24
}
}
这意味着您必须重新考虑 IntelReportLocationUnwound
class 定义。您可以使用与 IntelReport
中相同的类型,因为 MongoDB 只会将其从数组转换为嵌套对象:
public class IntelReportLocationUnwound
{
[BsonId]
[BsonRepresentation(BsonType.ObjectId)]
public string Id { get; set; }
[BsonElement("ReportTitle")]
[BsonIgnoreIfDefault]
public string Name { get; set; }
[BsonElement("Locations")]
[BsonIgnoreIfDefault]
public Location Location { get; set; }
}
然后你可以修改你的聚合:
.Project(x => new LocationDto
{
ItemId = x.Id,
ItemName = x.Name,
Name = x.Location.Name,
Latitude = x.Location.Latitude,
Longitude = x.Location.Longitude
})