我正在尝试从报告中获取所有点的数组,以便可以对其进行映射。为此,我需要放松/投射,但我一直在努力解决这个问题。
结果格式如下。
"totalPoints": 200,
"points": [
{
"itemId": "5e9592edb4959a52b8493f6d",
"itemName": "Numquam hic minus repellat sequi.",
"name": null,
"longitude": null,
"latitude": null
},
...
正如您所看到的,我的前三项都为空。
这是我正在使用的查询
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();
这里是原始报告类(但是我已经删除了一些不需要的东西)
public class IntelReport
{
[BsonId]
[BsonRepresentation(BsonType.ObjectId)]
public string Id { get; set; }
public string ReportTitle { get; set; }
public ICollection<Location> Locations { get; set; }
}
以及我认为需要放松的课程...
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)
之后的数据将如下所示: