MongoDB C#驱动程序:枚举上的LINQ筛选将枚举序列化为int
我试图在Mongo数据库中查询一组GeoJSON。文档看起来有点像这样:MongoDB C#驱动程序:枚举上的LINQ筛选将枚举序列化为int,c#,mongodb,linq,C#,Mongodb,Linq,我试图在Mongo数据库中查询一组GeoJSON。文档看起来有点像这样: { "_id" : ObjectId("5db2d9d7a9912b215bb6bfc8"), "type" : "Feature", "properties" : { //snip }, "geometry" : { "type" : "Polygon", "coordinates" : [ //snip ] } } public clas
{
"_id" : ObjectId("5db2d9d7a9912b215bb6bfc8"),
"type" : "Feature",
"properties" : {
//snip
},
"geometry" : {
"type" : "Polygon",
"coordinates" : [ //snip ]
}
}
public class Geometry
{
[JsonProperty("type")]
[BsonElement("type")]
[BsonRepresentation(BsonType.String)]
public GeometryType Type { get; set; }
}
Builders<Feature>.Filter.Ne(f => f.Geometry.Type, GeometryType.LineString)
"geometry.type" : {
"$ne" : 1
}
BsonSerializer.RegisterSerializer(new EnumSerializer<GeometryType>(BsonType.String));
几何体属性的C#类如下所示:
{
"_id" : ObjectId("5db2d9d7a9912b215bb6bfc8"),
"type" : "Feature",
"properties" : {
//snip
},
"geometry" : {
"type" : "Polygon",
"coordinates" : [ //snip ]
}
}
public class Geometry
{
[JsonProperty("type")]
[BsonElement("type")]
[BsonRepresentation(BsonType.String)]
public GeometryType Type { get; set; }
}
Builders<Feature>.Filter.Ne(f => f.Geometry.Type, GeometryType.LineString)
"geometry.type" : {
"$ne" : 1
}
BsonSerializer.RegisterSerializer(new EnumSerializer<GeometryType>(BsonType.String));
但是我有一些LINQ过滤代码,有点像这样:
{
"_id" : ObjectId("5db2d9d7a9912b215bb6bfc8"),
"type" : "Feature",
"properties" : {
//snip
},
"geometry" : {
"type" : "Polygon",
"coordinates" : [ //snip ]
}
}
public class Geometry
{
[JsonProperty("type")]
[BsonElement("type")]
[BsonRepresentation(BsonType.String)]
public GeometryType Type { get; set; }
}
Builders<Feature>.Filter.Ne(f => f.Geometry.Type, GeometryType.LineString)
"geometry.type" : {
"$ne" : 1
}
BsonSerializer.RegisterSerializer(new EnumSerializer<GeometryType>(BsonType.String));
这不会按预期进行筛选,因为它将LineString序列化为1,而不是数据库中表示它的“LineString”。从字符串反序列化可以很好地工作
如何使Mongo驱动程序生成正确的查询?我必须在启动时为枚举类型注册序列化程序,如下所示:
{
"_id" : ObjectId("5db2d9d7a9912b215bb6bfc8"),
"type" : "Feature",
"properties" : {
//snip
},
"geometry" : {
"type" : "Polygon",
"coordinates" : [ //snip ]
}
}
public class Geometry
{
[JsonProperty("type")]
[BsonElement("type")]
[BsonRepresentation(BsonType.String)]
public GeometryType Type { get; set; }
}
Builders<Feature>.Filter.Ne(f => f.Geometry.Type, GeometryType.LineString)
"geometry.type" : {
"$ne" : 1
}
BsonSerializer.RegisterSerializer(new EnumSerializer<GeometryType>(BsonType.String));
BsonSerializer.RegisterSerializer(新的EnumSerializer(BsonType.String));