如何在 C# 中使用 CreateDocumentQuery 查询 cosmos 数据库
How to query on cosmos database using CreateDocumentQuery in C#
我将以下 2 行 json 数据存储为 cosmos db 中的 2 个文档:
{
"Lists": [
{
"number":"1",
"Name": "Doctor"
},
{
"number":"2",
"Name": "Lawyer"
}
],
"Brands": [18,25],
"type":"Msg"
}
{
"Lists": [
{
"number":"3",
"Name": "Engineer"
},
{
"number":"4",
"Name":"Labour"
}
],
"Brands": [16,27],
"type": "Call"
}
如何查询 cosmos 数据库集合以进行过滤 Brands.Contains(16) && Lists.number.Contains(3)
i:e 上面第2行json。
我想在 JObject 或任何其他逻辑的帮助下实现这一点,这样无需为上述 json 创建模型 Class 我就可以过滤集合。
下面是查询逻辑,但是如何在下面的 IDocumentQuery 中编写上面的过滤器?
IDocumentQuery<JObject> query = client.CreateDocumentQuery<JObject>(
UriFactory.CreateDocumentCollectionUri(Constants.C4CosmosDatabaseId, collectionId),
_feedOptions)
.AsDocumentQuery();
如果您使用 Microsoft.Azure.Cosmos Version="3.19.0",则尝试
string number = "3";
int brand = 16;
var query = $"SELECT c.Lists, c.Brands FROM c JOIN t IN c.Lists WHERE CONTAINS(t.number, {number}, false) and ARRAY_CONTAINS(c.Brands, {brand }, false)";
FeedResponse<JObject> response = await _container
.GetItemQueryIterator<JObject>(new QueryDefinition(query))
.ReadNextAsync();
我将以下 2 行 json 数据存储为 cosmos db 中的 2 个文档:
{
"Lists": [
{
"number":"1",
"Name": "Doctor"
},
{
"number":"2",
"Name": "Lawyer"
}
],
"Brands": [18,25],
"type":"Msg"
}
{
"Lists": [
{
"number":"3",
"Name": "Engineer"
},
{
"number":"4",
"Name":"Labour"
}
],
"Brands": [16,27],
"type": "Call"
}
如何查询 cosmos 数据库集合以进行过滤 Brands.Contains(16) && Lists.number.Contains(3) i:e 上面第2行json。 我想在 JObject 或任何其他逻辑的帮助下实现这一点,这样无需为上述 json 创建模型 Class 我就可以过滤集合。
下面是查询逻辑,但是如何在下面的 IDocumentQuery 中编写上面的过滤器?
IDocumentQuery<JObject> query = client.CreateDocumentQuery<JObject>(
UriFactory.CreateDocumentCollectionUri(Constants.C4CosmosDatabaseId, collectionId),
_feedOptions)
.AsDocumentQuery();
如果您使用 Microsoft.Azure.Cosmos Version="3.19.0",则尝试
string number = "3";
int brand = 16;
var query = $"SELECT c.Lists, c.Brands FROM c JOIN t IN c.Lists WHERE CONTAINS(t.number, {number}, false) and ARRAY_CONTAINS(c.Brands, {brand }, false)";
FeedResponse<JObject> response = await _container
.GetItemQueryIterator<JObject>(new QueryDefinition(query))
.ReadNextAsync();