mongo C#驱动程序上的放松/项目出现问题
我试图从报告中获取所有点的数组,以便可以映射它们。要做到这一点,我需要放松/投射,但我正努力让我的头绕过它 结果的格式如下mongo C#驱动程序上的放松/项目出现问题,c#,mongodb,mongodb-.net-driver,C#,Mongodb,Mongodb .net Driver,我试图从报告中获取所有点的数组,以便可以映射它们。要做到这一点,我需要放松/投射,但我正努力让我的头绕过它 结果的格式如下 "totalPoints": 200, "points": [ { "itemId": "5e9592edb4959a52b8493f6d", "itemName": "Numquam hic minus repellat sequi.", "name": null,
"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();
将子文档数组转换为单个子文档,以便.Unwind(x=>x.Locations)
之后的数据如下所示:
{
"_id" : ObjectId("5e968bd5cc41498362cee2d2"),
"ReportTitle" : "aa",
"Locations" : {
"ItemId" : ObjectId("5e968bd5cc41498362cee2d3"),
"ItemName" : "Name",
"Name" : "N",
"Longitude" : 15,
"Latitude" : -24
}
}
这意味着您必须重新考虑intelreportlocationunbound
类定义。您可以使用与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
})
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
})