java - 插入新文档时未运行 Cosmos Db 触发器
问题描述
我正在使用 Azure Cosmos DB。我在 Azure Portal 中创建了一个简单的触发器,如下所示:
var context = getContext();
var request = context.getRequest();
// item to be created in the current operation
var itemToCreate = request.getBody();
itemToCreate["address"] = "test";
// update the item that will be created
request.setBody(itemToCreate);
不幸的是,当我插入新文档时,没有触发这个触发器。我还尝试将“触发类型”设置为“发布”。我错过了什么吗?
解决方案
好问题!我一直认为触发器会自动运行:)。
我相信只要插入文档,触发器就不会自动运行。您需要做的是指定在创建文档时要运行的触发器。
您需要做的是在发送创建文档请求时通过将触发器名称作为请求选项传递来注册触发器。
例如,请参阅此处的代码:https ://docs.microsoft.com/en-us/azure/cosmos-db/how-to-use-stored-procedures-triggers-udfs#pre-triggers (也复制如下)。注意PreTriggerInclude
in的使用RequestOptions
:
dynamic newItem = new
{
category = "Personal",
name = "Groceries",
description = "Pick up strawberries",
isComplete = false
};
Uri containerUri = UriFactory.CreateDocumentCollectionUri("myDatabase", "myContainer");
RequestOptions requestOptions = new RequestOptions { PreTriggerInclude = new List<string> { "trgPreValidateToDoItemTimestamp" } };
await client.CreateDocumentAsync(containerUri, newItem, requestOptions);
推荐阅读
- php - 如何将每个项目添加为新记录而不在购物车中的 laravel 中更新
- selenium-webdriver - Testng 无法记录测试失败
- android - 有安卓键盘监听器吗?
- terminal - 如何在不丢失分辨率的情况下让 neofetch girl 显示?
- c# - 如何在 c# Winforms 中从另一个窗体中捕获或获取 e.cancel 属性
- sql-server-2019 - 可以在不实际拆分为两个表的情况下垂直分区表吗?
- dockerfile - 如何在字符串 docker 中使用环境变量
- python - 我可以在 TRY 和 EXCEPT 子句中添加 IF 子句吗
- python - python-binance order_limit_buy() APIError(code=-1013): Filter failure: LOT_SIZE
- java - org.hibernate.tool.schema.spi.CommandAcceptanceException:执行 DDL 创建表索引时出错