使用 GuidRepresentation.Standard GuidSerializer 执行查询时 MongoDB C# 驱动程序出现问题
Trouble with MongoDB C# driver when performing queries using GuidRepresentation.Standard GuidSerializer
我正在使用 MongoDB C# 驱动程序版本 2.11.0。我知道过去每个驱动程序如何以不同方式处理 GUID,现在有一个通用标准。出于向后兼容的原因,默认情况下 C# 驱动程序中不使用它。目前的建议似乎是在序列化程序上设置 GuidRepresentation。全局或每个映射的 Guid 属性 单独。
没关系,我面临的问题是针对集合的查询不遵循序列化设置,并且只有在使用已弃用的 MongoDefaults 设置时才能正常工作。使用正确的 GuidRepresentation 正确存储文档,但查询似乎尝试匹配 CSUUID 而不是 UUID,因此它永远不会与数据库中的文档匹配。
这是一张基本的 class 地图:
public static void RegisterClassMaps()
{
BsonClassMap.RegisterClassMap<Widget>(cm =>
{
cm.MapIdProperty(w => w.Id)
.SetSerializer(new GuidSerializer(GuidRepresentation.Standard));
cm.MapProperty(w => w.ParentId)
.SetSerializer(new GuidSerializer(GuidRepresentation.Standard));
cm.MapProperty(w => w.Name);
}
}
这是一个匹配 Guid 和字符串的简单查询。以下方法将始终 return null,因为它使用 CSUUID 而不是 UUID 构建查询。
private readonly IMongoCollection<Widget> _collection;
public async Task<Widget> FindByNameAsync(Guid parentId, string name)
{
var query = _collection.Find(w =>
w.ParentId == parentId &&
w.Name = name);
return await query.SingleOrDefaultAsync();
}
使用 AsQueryable() 而不是 Find() 具有相同的结果。两者都使用 CSUUID 而不是 UUID 构建查询。
我也尝试将全局 GuidSerializer 设置为使用 GuidRepresentation.Standard,但我得到了相同的结果。
BsonSerializer.RegisterSerializer(new GuidSerializer(GuidRepresentation.Standard));
如果我更改已弃用的 MongoDefaults 属性 的值,一切正常。
MongoDefaults.GuidRepresentation = GuidRepresantion.Standard;
我是否遗漏了有关如何构建查询的信息?我宁愿避免弃用的设置。
有同样的问题。
您需要配置GuidRepresentationMode,直到以后默认为V3
BsonDefaults.GuidRepresentationMode = GuidRepresentationMode.V3;
BsonDefaults.GuidRepresentationMode = GuidRepresentationMode.V3;
BsonSerializer.RegisterSerializer(new GuidSerializer(GuidRepresentation.Standard));
BsonDefaults.GuidRepresentationMode
被标记为已弃用,因为在驱动程序的未来版本中,我们将删除对 V2 模式的支持,届时我们还将删除 GuidRepresentationMode
属性 BsonDefaults
.
我正在使用 MongoDB C# 驱动程序版本 2.11.0。我知道过去每个驱动程序如何以不同方式处理 GUID,现在有一个通用标准。出于向后兼容的原因,默认情况下 C# 驱动程序中不使用它。目前的建议似乎是在序列化程序上设置 GuidRepresentation。全局或每个映射的 Guid 属性 单独。
没关系,我面临的问题是针对集合的查询不遵循序列化设置,并且只有在使用已弃用的 MongoDefaults 设置时才能正常工作。使用正确的 GuidRepresentation 正确存储文档,但查询似乎尝试匹配 CSUUID 而不是 UUID,因此它永远不会与数据库中的文档匹配。
这是一张基本的 class 地图:
public static void RegisterClassMaps()
{
BsonClassMap.RegisterClassMap<Widget>(cm =>
{
cm.MapIdProperty(w => w.Id)
.SetSerializer(new GuidSerializer(GuidRepresentation.Standard));
cm.MapProperty(w => w.ParentId)
.SetSerializer(new GuidSerializer(GuidRepresentation.Standard));
cm.MapProperty(w => w.Name);
}
}
这是一个匹配 Guid 和字符串的简单查询。以下方法将始终 return null,因为它使用 CSUUID 而不是 UUID 构建查询。
private readonly IMongoCollection<Widget> _collection;
public async Task<Widget> FindByNameAsync(Guid parentId, string name)
{
var query = _collection.Find(w =>
w.ParentId == parentId &&
w.Name = name);
return await query.SingleOrDefaultAsync();
}
使用 AsQueryable() 而不是 Find() 具有相同的结果。两者都使用 CSUUID 而不是 UUID 构建查询。
我也尝试将全局 GuidSerializer 设置为使用 GuidRepresentation.Standard,但我得到了相同的结果。
BsonSerializer.RegisterSerializer(new GuidSerializer(GuidRepresentation.Standard));
如果我更改已弃用的 MongoDefaults 属性 的值,一切正常。
MongoDefaults.GuidRepresentation = GuidRepresantion.Standard;
我是否遗漏了有关如何构建查询的信息?我宁愿避免弃用的设置。
有同样的问题。
您需要配置GuidRepresentationMode,直到以后默认为V3
BsonDefaults.GuidRepresentationMode = GuidRepresentationMode.V3;
BsonDefaults.GuidRepresentationMode = GuidRepresentationMode.V3;
BsonSerializer.RegisterSerializer(new GuidSerializer(GuidRepresentation.Standard));
BsonDefaults.GuidRepresentationMode
被标记为已弃用,因为在驱动程序的未来版本中,我们将删除对 V2 模式的支持,届时我们还将删除 GuidRepresentationMode
属性 BsonDefaults
.