4

考虑到这个类:

public class Location
{
    public Coordinates Geo { get; set; }

    public Location()
    {
        Geo = new Coordinates();
    }

    public class Coordinates
    {
        public decimal Lat { get; set; }
        public decimal Long { get; set; }
    }
}

我在集合集上有一个地理空间索引,例如{ Geo: "2d" }. 不幸的是,驱动程序尝试将纬度/经度坐标存储为字符串,而不是数字,我收到一条错误消息,提示Tue Mar 15 16:29:22 [conn8] insert database.locations exception 13026 geo values must be numbers: { Lat: “50.0853779”,长:“19.931276700000012”} 1ms。为了缓解这个问题,我设置了一个这样的地图:

BsonClassMap.RegisterClassMap<Location.Coordinates>(cm =>
{
    cm.AutoMap();
    cm.MapProperty(c => c.Lat).SetRepresentation(BsonType.Double);
    cm.MapProperty(c => c.Long).SetRepresentation(BsonType.Double);
});

请注意,没有BsonType.Decimal类似的东西。实际上,当我试图打电话时,Save()我得到了一个MongoDB.Bson.TruncationException,这似乎是合乎逻辑的。我有哪些选择?

4

1 回答 1

5

根据此错误(已修复 2011 年 1 月 21 日 05:46:23 AM UTC),在 c# 官方驱动程序中添加了“AllowTruncation”功能。因此,您需要下载最新的驱动程序版本并享受!您也可以像这样使用 BsonRepresentationAttribute 而不是 SetRepresentation:

public class C {
  [BsonRepresentation(BsonType.Double, AllowTruncation=true)]
  public decimal D;
}
于 2011-03-15T16:05:25.320 回答