首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >MongoDB .NET驱动程序不对集合进行反序列化

MongoDB .NET驱动程序不对集合进行反序列化
EN

Stack Overflow用户
提问于 2020-03-24 11:28:47
回答 1查看 1.1K关注 0票数 1

我在反序列化来自MongoDB的集合时遇到了问题。对于单个对象来说,它似乎很好,但是对于对象集合来说,它却失败了。集合是具有坐标的Mongo中的GeoJSON对象。这似乎就是问题所在。也许在我的C#类中,我并没有代表这一点。虽然它似乎对一个单一的对象很好。

我根据这篇文章创建了一个通用的集合回购:在.NET核中实现的通用Mongo存储模式

鉴于我的班级:

代码语言:javascript
运行
复制
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的收藏:

以及本身的小片段(我根据使用MongoDB将Shapefile导入GeoJSON从一个Shapefile转换了这个片段):

代码语言:javascript
运行
复制
[
    { "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 ] 
               ] 
           ] 
        } 
    }
]

我收到一个错误:

VisualStatsPoCAPI.Repositories.Models.Mongo.GardaSubdistrictBoundaryMongo::在反序列化类VisualStatsPoCAPI.Repositories.Models.Mongo.Geometry:的几何属性时发生错误,当反序列化类VisualStatsPoCAPI.Repositories.Models.Mongo.Geometry:的坐标属性时发生错误,无法从BsonType 'Array‘反序列化'Double’。

我对单个对象使用的SDK调用是:

代码语言:javascript
运行
复制
public virtual TDocument FindOne(Expression<Func<TDocument, bool>> filterExpression)
{
    return _collection.Find(filterExpression).FirstOrDefault();
}

对于一个收藏品,要么是:

代码语言:javascript
运行
复制
public virtual IEnumerable<TProjected> FilterBy<TProjected>(
    Expression<Func<TDocument, bool>> filterExpression,
    Expression<Func<TDocument, TProjected>> projectionExpression)
{
    return _collection.Find(filterExpression).Project(projectionExpression).ToEnumerable();
}

代码语言:javascript
运行
复制
public virtual Task<IEnumerable<TDocument>> FindAll()
{
    FilterDefinition<TDocument> filter = FilterDefinition<TDocument>.Empty;

    return Task.Run(() => _collection.Find(filter).ToList().AsEnumerable());
}

这和我表示几何学有关,但我不确定。我有点糊涂。有人能帮忙吗?

更新(2020年3月25日):有人建议我使用GeoJsonPolygon。我试过这样做:

代码语言:javascript
运行
复制
public GeoJsonPolygon<GeoJson2DCoordinates> Geometry { get; set; }

同样,对于单个文档来说,这也很好。当我尝试将它用于整个集合时,我会得到:

VisualStatsPoCAPI.Repositories.Models.Mongo.GardaSubdistrictBoundaryMongo::反序列化类System.FormatException无效的GeoJson类型:'MultiPolygon‘的几何属性时发生了错误。预期:“多边形”。

当我切换到使用GeoJsonMultiPolygon (正如编译器所建议的那样)时,我得到:

VisualStatsPoCAPI.Repositories.Models.Mongo.GardaSubdistrictBoundaryMongo::反序列化类System.FormatException无效GeoJson类型的几何属性时发生错误:“多边形”。预期:“多重多边形”。

EN

Stack Overflow用户

回答已采纳

发布于 2020-03-29 00:48:13

从所提供的屏幕截图和模型中看不清楚,但是当您粘贴所得到的错误时就变得很清楚了。

看起来您的集合包含两个Polygons

代码语言:javascript
运行
复制
{ geometry: { 'type' : 'Polygon', 'coordinates' : [[[100.0, 0.0], [101.0, 0.0], [101.0, 1.0], [100.0, 0.0]]] } }

MultiPolygons

代码语言:javascript
运行
复制
{ geometry: { 'type' : 'MultiPolygon', 'coordinates' : [[[[102.0, 2.0], [103.0, 2.0], [103.0, 3.0], [102.0, 3.0], [102.0, 2.0]]], [[[102.0, 2.0], [103.0, 2.0], [103.0, 3.0], [102.0, 3.0], [102.0, 2.0]], [[102.0, 2.0], [103.0, 2.0], [103.0, 3.0], [102.0, 3.0], [102.0, 2.0]]]] } }

MongoDB .NET驱动程序为两种多边形类型(GeoJsonPolygonGeoJsonMultiPolygon)提供类。两个类都是从GeoJsonGeometry派生的。此外,您可以使用GeoJson2DCoordinates来表示双元素数组。

驱动程序将处理rest -您可以将基本抽象类型指定为Geometry,并且在运行时将文档反序列化为相关的具体类型:

代码语言:javascript
运行
复制
[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 GeoJsonGeometry<GeoJson2DCoordinates> Geometry { get; set; }
}

票数 0
EN
查看全部 1 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60830149

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档