How to set serialization parameters for geodata values โ€‹โ€‹using the official 10gen C # driver?

Given this class:

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; } } } 

I have a geospatial index in a collection set like { Geo: "2d" } . Unfortunately, the driver tries to save the lat / lon coordinates as strings, not numbers, and I get an error message that says Tue Mar 15 16:29:22 [conn8] insert database.locations exception 13026 geo values โ€‹โ€‹must be numbers : {Lat: "50.0853779", long: "19.931276700000012"} 1 ms. To fix this problem, I configure this card:

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

Note that there is no BsonType.Decimal and nothing like that. As a result, when I try to call Save() I get a MongoDB.Bson.TruncationException , which seems logical. What are my options?

+4
source share
1 answer

According to this error (fixed on January 21, 2011 05:46:23 UTC) , the 'AllowTruncation' feature was added to the official C # driver. So, you need the latest driver and enjoy! Also, instead of SetRepresentation, you can use BsonRepresentationAttribute as follows:

 public class C { [BsonRepresentation(BsonType.Double, AllowTruncation=true)] public decimal D; } 
+4
source

Source: https://habr.com/ru/post/1343801/


All Articles