Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
MongoDB C#驱动程序:枚举上的LINQ筛选将枚举序列化为int_C#_Mongodb_Linq - Fatal编程技术网

MongoDB C#驱动程序:枚举上的LINQ筛选将枚举序列化为int

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

我试图在Mongo数据库中查询一组GeoJSON。文档看起来有点像这样:

{
    "_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));