如何在 Azure CosmosDB/DocumentDB 中查询字典内容?



我正在从C#代码在Azure CosmosDB中存储对象。应用程序启动时未完全定义这些对象的属性,因此可以在运行时添加或删除某些对象。这就是为什么我在模型类中有一个字典类型的属性"属性":

public Dictionary<string, string> Attributes { get; }

但是,如何针对此属性的内容编写查询呢?例如,我想编写一个查询,例如:

documentQueryable
.Where(doc => doc.Attributes.ContainsKey("City") && doc.Attributes["City"] == "NY");

但是,不支持此操作:

Microsoft.Azure.Documents.Linq.DocumentQueryException: Method 'ContainsKey' is not supported., documentdb-dotnet-sdk/1.22.0 Host/32-bit MicrosoftWindowsNT/10.0.14393.0

由于 Cosmos DB 是无架构的,因此无需检查密钥是否存在。如果将代码更改为以下内容,它应该按预期工作:

documentQueryable.Where(doc =>doc.Attributes["City"] == "NY");

以下内容应该有效,您可以从here

documentQueryable.Where(doc => doc.Attributes["City"] == "NY");

最新更新