我在反序列化MongoDB的集合时遇到问题。对于单个对象来说似乎很好,但是对于对象集合却失败了。该集合是Mongo中带有坐标的GeoJSON对象。这似乎是问题所在。也许我没有在C#类中代表这种权利。尽管对于单个对象来说似乎正常工作。
我根据这篇文章创建了一个通用的收集仓库:Generic Mongo Repository pattern implemented in .NET Core
鉴于我的班级:
using System.Collections.Generic;
using MongoDB.Driver.GeoJsonObjectModel;
using MongoDB.Bson;
using MongoDB.Bson.Serialization.Attributes;
namespace VisualStatsPoCAPI.Repositories.Models.Mongo
{
[BsonCollection("garda_subdistrict_boundaries")]
public class GardaSubdistrictBoundaryMongo : Document
{
[BsonElement("type")]
public string Type { get; set; }
[BsonElement("properties")]
public Properties Properties { get; set; }
[BsonElement("geometry")]
public Geometry Geometry { get; set; }
}
public class Properties
{
public string REGION { get; set; }
public string REG_CODE { get; set; }
public string DIVISION { get; set; }
public string DIV_CODE { get; set; }
public string DISTRICT { get; set; }
public string DIST_CODE { get; set; }
public string SUB_DIST { get; set; }
public string SUB_IRISH { get; set; }
public string SUB_CODE { get; set; }
public string COUNTY_1 { get; set; }
public string COUNTY_2 { get; set; }
public string GEOGID { get; set; }
public int Male2011 { get; set; }
public int Female2011 { get; set; }
public int Total2011 { get; set; }
public int PPOcc2011 { get; set; }
public int Unocc2011 { get; set; }
public int Vacant2011 { get; set; }
public int HS2011 { get; set; }
public double PCVac2011 { get; set; }
public string CREATEDBY { get; set; }
}
public class Geometry
{
[BsonElement("type")]
public string Type { get; set; }
[BsonElement("coordinates")]
public IEnumerable<IEnumerable<GeoJson2DCoordinates>> Coordinates { get; set; }
}
}
和MongoDB集合:
和small片段本身的片段(我根据Importing a Shapefile into MongoDB using GeoJSON从Shapefile转换为该片段):
[
{ "type": "Feature",
"properties": {
"REGION": "Southern Region",
"REG_CODE": "03",
"DIVISION": "Cork West",
"DIV_CODE": "0319",
"DISTRICT": "Bandon",
"DIST_CODE": "4300A",
"SUB_DIST": "Kinsale",
"SUB_IRISH": "Cionn tS�ile",
"SUB_CODE": "4305B",
"COUNTY_1": "Cork",
"COUNTY_2": null,
"GEOGID": "M4305B",
"Male2011": 5765,
"Female2011": 5963,
"Total2011": 11728,
"PPOcc2011": 4054,
"Unocc2011": 1177,
"Vacant2011": 1013,
"HS2011": 5231,
"PCVac2011": 19.4,
"CREATEDBY": "Paul Creaner"
},
"geometry": {
"type": "Polygon",
"coordinates": [
[
[-8.665517347801826, 51.701921804534543 ],
[-8.665512199746647, 51.702050730841847 ]
]
]
}
}
]
我收到一个错误:
System.FormatException:在反序列化类VisualStatsPoCAPI.Repositories.Models.Mongo.GardaSubdistrictBoundaryMongo时发生错误:在反序列化类VisualStatsPoCAPI.Repositories.Models.Mongo.Geometry的Coordinates属性时发生错误:无法反序列化' Double”(来自BsonType“数组”)。
我用于单个对象的SDK调用是:
public virtual TDocument FindOne(Expression<Func<TDocument, bool>> filterExpression)
{
return _collection.Find(filterExpression).FirstOrDefault();
}
并且对于集合,可以是:
public virtual IEnumerable<TProjected> FilterBy<TProjected>(
Expression<Func<TDocument, bool>> filterExpression,
Expression<Func<TDocument, TProjected>> projectionExpression)
{
return _collection.Find(filterExpression).Project(projectionExpression).ToEnumerable();
}
或
public virtual Task<IEnumerable<TDocument>> FindAll()
{
FilterDefinition<TDocument> filter = FilterDefinition<TDocument>.Empty;
return Task.Run(() => _collection.Find(filter).ToList().AsEnumerable());
}
这与我如何表示几何形状有关,但我不确定。我有点困惑。谁能帮忙?
更新(2020年3月25日):建议使用GeoJsonPolygon。我尝试使用如下所示:
public GeoJsonPolygon<GeoJson2DCoordinates> Geometry { get; set; }
同样,对于单个文档来说效果很好。当我尝试将其用于整个收藏集时,我得到:
System.FormatException:反序列化类VisualStatsPoCAPI.Repositories.Models.Mongo.GardaSubdistrictBoundaryMongo的Geometry属性时发生错误:无效的GeoJson类型:'MultiPolygon'。预期:“多边形”。
当我切换到使用GeoJsonMultiPolygon时(如编译器建议的那样,我得到:
System.FormatException:反序列化类VisualStatsPoCAPI.Repositories.Models.Mongo.GardaSubdistrictBoundaryMongo的Geometry属性时发生错误:无效的GeoJson类型:'Polygon'。预期:“ MultiPolygon”。
根据提供的屏幕快照,它看起来geometry.coordinates
是MongoDB中的一个坐标数组,而您的C#模型希望它是一个坐标数组。在这种情况下,您可以使用GeoJsonPolygon
(单元测试here)。
因此,您必须转换数据或将Geometry
类更改为:
public class Geometry
{
[BsonElement("type")]
public string Type { get; set; }
[BsonElement("coordinates")]
public IEnumerable<GeoJson2DCoordinates> Coordinates { get; set; }
}