簡體   English   中英

Cosmos DB 預觸發器如何從另一行獲取值

[英]Cosmos DB pre trigger how to fetch value from another row

在 Cosmos DB 中,我有觸發器,這是預觸發器。 此觸發器的方法如下所示:

 function CalculateFields(){
        var context = getContext(); 
        var request = context.getRequest(); 
        // this is the current request 
        var itemToCreate = request.getBody(); 

        if(itemToCreate["fileType"] == "DivisionConfig")
        {
           itemToCreate["column1"] = 2 * item1["column3"];
        }

我想從另一行中獲取值,由 id 選擇。 itemToCreate是當前項目,如何獲取 id = 1 的項目? 我可以在這種方法中做到這一點,或者當我調用這個觸發器時,我必須在 C# 代碼中傳遞變量:

using (CosmosClient client = new CosmosClient(Endpoint, Key))
            {
                var container = client.GetContainer(DatabaseId, CollectionId);

                ItemResponse<T> response2 = await container.CreateItemAsync(item, new PartitionKey("Mypk"), new ItemRequestOptions { PreTriggers = new List<string> { "CalculateFields" } });
                return response2;
            }

item1["column3"]是同一集合中的另一行。

預觸發器只能對項目本身進行操作,不能接受參數。 我不清楚您在這里要做什么,但也許可以考慮使用 Post-Trigger。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM