我试图找到在 Azure ComsmosDB SQL 查询中执行 If/Case 类型语句的正确语法。这是我拥有的文件
{
"CurrentStage": "Stage2",
"Stage1": {
"Title": "Stage 1"
},
"Stage2": {
"Title": "Stage 2"
},
"Stage3": {
"Title": "Stage 3"
}
Run Code Online (Sandbox Code Playgroud)
}
我想要做的是创建一个看起来像的查询
Select c.CurrentStage,
if (CurrentStage == 'Stage1') { c.Stage1.Title }
else if (CurrentStage == 'Stage2') { c.Stage2.Title }
else if (CurrentStage == 'Stage3') { c.Stage3.Title } as Title
From c
Run Code Online (Sandbox Code Playgroud)
显然,我拥有的文档和查询比这个复杂得多,但这让您对我正在尝试做的事情有了大致的了解。根据文档中的其他一些字段,我在选择中有 1 个字段是可变的。
我正在尝试为 COMOS 中的几个属性定义索引,但我对自动索引有点困惑。根据 Cosmos DB文档:
默认情况下,Azure Cosmos DB 会自动为容器中所有项目的每个属性编制索引,而无需定义任何架构或配置二级索引。
另外,请参考这个:
在某些情况下,您可能希望覆盖此自动行为以更好地满足您的要求。您可以通过设置容器的索引模式来自定义容器的索引策略,并包括或排除属性路径。
从以上几点我了解到,除非我们定义自定义索引策略,否则自动索引设置为true(这是有道理的)。但是,如果我们paths为索引定义了自己的包含和排除,否则它应该是false.
这可能意味着,如果我如下定义容器属性,则该Indexing Policy Automatic属性应false在 Cosmos DB 上设置为。
using Microsoft.Azure.Cosmos; //Azure Cosmos SDK v3.3.1
.
.
var containerProperties = new ContainerProperties
{
Id = "SOME_CONTAINER_NAME",
PartitionKeyPath = "/MY_PARTITION_KEY",
};
containerProperties.IndexingPolicy.IncludedPaths.Add(new IncludedPath {Path = "/\"{MY_PARTITION_KEY}\"/?"});
containerProperties.IndexingPolicy.ExcludedPaths.Add(new ExcludedPath {Path = "/*"});
Run Code Online (Sandbox Code Playgroud)
但是,我看到 CosmosDb 索引的上述配置被定义为automaticset to true。

Automaticproperty 和IncludedPaths,ExcludedPaths属性是否IndexingPolicy class …
您好,我想获取 cosmos DB 数据库中容器的所有项目。我可以看到已经有很多可用的方法,但我没有看到任何 getAllItems 或类似的方法。
有没有像使用 LINQ 之类的简单方法来做到这一点?
谢谢
我正在使用下面的存储过程从 cosmos db 集合中删除项目。
function bulkDeleteStoredProcedure(query) {
var collection = getContext().getCollection();
var collectionLink = collection.getSelfLink();
var response = getContext().getResponse();
var responseBody = {
deleted: 0,
continuation: true
};
// Validate input.
if (!query) throw new Error("The query is undefined or null.");
tryQueryAndDelete();
// Recursively runs the query w/ support for continuation tokens.
// Calls tryDelete(documents) as soon as the query returns documents.
function tryQueryAndDelete(continuation) {
var requestOptions = {continuation: continuation};
var isAccepted = collection.queryDocuments(collectionLink, query, requestOptions, function (err, retrievedDocs, responseOptions) …Run Code Online (Sandbox Code Playgroud) 我正在尝试查询一个数据库,其中存储的实体具有不同的属性,并且我想使用不同形状的查询从该信息中检索一些字段(这就是使用无模式数据库的要点),但我对 CosmosDB 没有运气。
由于我没有针对每种查询结果的类(我也不想要),因此我想将它们作为动态列表获取,但我不知道如何检索它们。我试过了:
var queryResultSetIterator = container.GetItemQueryIterator<dynamic>(queryDefinition, null, new QueryRequestOptions() { PartitionKey = new PartitionKey(idParticion), MaxItemCount = -1 });
List<dynamic> results = new List<dynamic>();
await foreach (dynamic item in queryResultSetIterator)
{
results.Add(item);
}
Run Code Online (Sandbox Code Playgroud)
但我得到的项目是 System.Text.Json.JsonDocument 类型,因此它无法按预期工作;
如果我尝试使用 ExpandoObjects,我无法使用新的 System.Text.Json 序列化程序反序列化结果,因为截至目前它不支持反序列化为 ExpandoObjects:
results.Add(JsonSerializer.Deserialize<ExpandoObject>(item));
Run Code Online (Sandbox Code Playgroud)
目前唯一有效的方法是使用旧的 Newtonsoft Json 序列化器,但在我看来,在应用程序中同时存在这两个库似乎有点过分了,所有这些都意味着。
关于如何从 CosmosDB 获取动态投影还有其他想法吗?
提前致谢!
我的 cosmos db 容器中有集合项。我的示例数据是
{
"objectID": "abc",
"id": "123",
"name": "gfh",
"description": "chock",
"brand": "hcn",
"size": 180,
"sizeUnits": "Grams",
"stores": []
},
{
"objectID": "123c",
"id": "0dfg",
"name": "shaek",
"description": "7ihk",
"brand": "fghcn",
"size": 768,
"sizeUnits": "Grams",
"stores": [ {
"id": 678",
"price": 2.2
},
{
"id": 678",
"price": 2.2}]
}
Run Code Online (Sandbox Code Playgroud)
等等...
我需要获取商店为空的所有详细信息。如何为此编写查询。
我们最近开始测试 Cosmos DB 作为我们奥尔良集群的持久存储。
我们的 Cosmos DB 方案是作为具有低延迟和大量替换、点读取和创建的键值存储。因此,我们的文档 ID 和分区键是相同的,因此基本上每个分区 1 个文档。
但是,在 Cosmos DB Insights 中,我们看到我们达到了 100% 标准化 RU 消耗:
当我们深入挖掘时,我们会看到一个热图,它在 PartitionKeyRangeID 0 上放置了 100% RU 消耗,而没有其他 PartitionKeyRanges 可用。

我的理解是,由于我们有与文档一样多的分区,我们应该会遇到这个问题。我也不确定 PartitionKeyRangeID 0 表示什么,因为我们应该至少有几千个分区
想象我们有一个这样的集合(示例取自 https://www.documentdb.com/sql/demo)
{
"_id" : "19015",
"description" : "Snacks, granola bars, hard, plain",
"servings" : [
{
"amount" : 1,
"description" : "bar",
"weightInGrams" : 21
},
{
"amount" : 1,
"description" : "bar (1 oz)",
"weightInGrams" : 28
},
{
"amount" : 1,
"description" : "bar",
"weightInGrams" : 25
}
]
}
Run Code Online (Sandbox Code Playgroud)
我如何在SQL api中查询CosmosDB以获取这样的结果?
{
"_id" : "19015",
"servings" : [
{
"description" : "bar"
},
{
"description" : "bar (1 oz)"
},
{
"description" : "bar"
} …Run Code Online (Sandbox Code Playgroud) 我在运行 azure cosmos db 的示例 python 脚本时遇到下一个错误:
Traceback (most recent call last):
File "CosmosGetStarted.py", line 1, in <module>
import azure.cosmos.cosmos_client as cosmos_client
ImportError: No module named azure.cosmos.cosmos_client
Run Code Online (Sandbox Code Playgroud)
我试图根据下一本手册做所有事情: https://learn.microsoft.com/en-us/azure/cosmos-db/create-sql-api-python
python azure raspberry-pi3 azure-cosmosdb azure-cosmosdb-sqlapi
我在 Cosmos DB 中有两个容器,我需要在它们之间进行内部联接。
示例:
VD_VENDAS
VD_ESTOQUE
VD_COMPRAS
Run Code Online (Sandbox Code Playgroud)
我需要在 VD_VENDAS 和 VD_ESTOQUE 之间进行内部联接。我尝试一下:
VD_VENDAS
VD_ESTOQUE
VD_COMPRAS
Run Code Online (Sandbox Code Playgroud)
但这不起作用。我该怎么做 ?