共用方式為


取代觸發程式

在特定觸發程式資源上執行 PUT 作業會取代整個觸發程式資源。 所有使用者可設定的屬性,包括識別碼、本文、triggerOperation 和 triggerType,都必須在本文中提交,才能執行取代。

要求

方法 要求 URI Description
PUT https://{databaseaccount}.documents.azure.com/dbs/{db-id}/colls/{coll-id}/triggers/{trigger-name} {databaseaccount} 是您訂用帳戶下建立的 Azure Cosmos DB 帳戶名稱。 {db-id} 值是取代觸發程式之資料庫的使用者產生名稱/識別碼,而不是系統產生的識別碼 (在資料庫的 rid) 。 {coll-id} 值是取代觸發程式之集合的名稱。 {trigger-name} 值是要取代的觸發程式名稱。

標題

如需所有 Cosmos DB 要求所使用的標頭,請參閱常見的 Azure Cosmos DB REST 要求標頭

主體

屬性 必要 類型 描述
id 必要 String 它用來識別觸發程序的唯一名稱。 識別碼不能超過 255 個字元。
body 必要 String 這是觸發程式的主體。
triggerOperation 必要 String 這是叫用觸發程式的作業類型。 可接受的值為: AllInsertReplaceDelete
triggerType 必要 String 它會指定觸發程式何時引發。 可接受的值為: PrePost 作業後 張貼 觸發程式時,會在作業之前引發預先觸發程式。
{  
    "body": "function updateMetadata() {\r\n    var context = getContext();\r\n    var collection = context.getCollection();\r\n    var response = context.getResponse();\r\n    var createdDocument = response.getBody();\r\n\r\n    // query for metadata document\r\n    var filterQuery = 'SELECT * FROM root r WHERE r.id = \"_metadata\"';\r\n    var accept = collection.queryDocuments(collection.getSelfLink(), filterQuery,\r\n      updateMetadataCallback);\r\n    if(!accept) throw \"Unable to update metadata, abort\";\r\n\r\n    function updateMetadataCallback(err, documents, responseOptions) {\r\n      if(err) throw new Error(\"Error\" + err.message);\r\n           if(documents.length != 1) throw 'Unable to find metadata document';\r\n           var metadataDocument = documents[0];\r\n\r\n           // update metadata\r\n           metadataDocument.createdDocuments += 1;\r\n           metadataDocument.createdNames += \" \" + createdDocument.id;\r\n           var accept = collection.replaceDocument(metadataDocument._self,\r\n               metadataDocument, function(err, docReplaced) {\r\n                  if(err) throw \"Unable to update metadata, abort\";\r\n               });\r\n           if(!accept) throw \"Unable to update metadata, abort\";\r\n           return;          \r\n    }",  
    "id": "PostTrigger-UpdateMetaAll",  
    "triggerOperation": "All",  
    "triggerType": "Post"  
}  
  

回應

標題

如需所有 Cosmos DB 回應所傳回的標頭,請參閱 常見的 Azure Cosmos DB REST 回應標頭

狀態碼

下表列出此作業所傳回的常見狀態碼。 如需狀態碼的完整清單,請參閱 HTTP 狀態碼

HTTP 狀態碼 Description
200 確定 取代作業成功。
400 不正確的要求 JSON 內文無效。 檢查是否缺少大括號或引號。
404 找不到 要取代的觸發程式不再是資源,也就是已刪除觸發程式。
409 衝突 現有觸發程式已採用為觸發程式提供的識別碼。

主體

屬性 Description
_擺脫 它是系統產生的屬性。 資源識別碼 (_rid) 是資源模型上每個資源堆疊的階層式唯一識別碼。 在內部用來放置和導覽觸發程序資源。
_Ts 它是系統產生的屬性。 代表資源的上次更新時間戳記。 值為時間戳記。
_自我 它是系統產生的屬性。 代表資源的唯一可定址 URI。
_Etag 它是系統產生的屬性,指定開放式並行控制所需的資源 etag。
{  
    "body": "function updateMetadata() {\r\n    var context = getContext();\r\n    var collection = context.getCollection();\r\n    var response = context.getResponse();\r\n    var createdDocument = response.getBody();\r\n\r\n    // query for metadata document\r\n    var filterQuery = 'SELECT * FROM root r WHERE r.id = \"_metadata\"';\r\n    var accept = collection.queryDocuments(collection.getSelfLink(), filterQuery,\r\n      updateMetadataCallback);\r\n    if(!accept) throw \"Unable to update metadata, abort\";\r\n\r\n    function updateMetadataCallback(err, documents, responseOptions) {\r\n      if(err) throw new Error(\"Error\" + err.message);\r\n           if(documents.length != 1) throw 'Unable to find metadata document';\r\n           var metadataDocument = documents[0];\r\n\r\n           // update metadata\r\n           metadataDocument.createdDocuments += 1;\r\n           metadataDocument.createdNames += \" \" + createdDocument.id;\r\n           var accept = collection.replaceDocument(metadataDocument._self,\r\n               metadataDocument, function(err, docReplaced) {\r\n                  if(err) throw \"Unable to update metadata, abort\";\r\n               });\r\n           if(!accept) throw \"Unable to update metadata, abort\";\r\n           return;          \r\n    }",  
    "id": "PostTrigger-UpdateMetaAll",  
    "triggerOperation": "All",  
    "triggerType": "Post",  
    "_rid": "Sl8fALN4sw4BAAAAAAAAcA==",  
    "_ts": 1449689654,  
    "_self": "dbs\/Sl8fAA==\/colls\/Sl8fALN4sw4=\/triggers\/Sl8fALN4sw4BAAAAAAAAcA==\/",  
    "_etag": "\"060022e5-0000-0000-0000-566882360000\""  
}  
  

範例

PUT https://contosomarketing.documents.azure.com/dbs/volcanodb/colls/volcano1/triggers/PostTrigger-UpdateMetaAll HTTP/1.1  
x-ms-session-token: 42  
x-ms-date: Wed, 09 Dec 2015 19:34:14 GMT  
authorization: type%3dmaster%26ver%3d1.0%26sig%3dZyzGdsA62guUndVJ4UOhLYaGcoszBFqlYOyXYFpuBwI%3d  
Cache-Control: no-cache  
User-Agent: contoso/1.0  
x-ms-version: 2015-08-06  
Accept: application/json  
Host: contosomarketing.documents.azure.com  
Cookie: x-ms-session-token=42  
Content-Length: 1556  
Expect: 100-continue  
  
{  
    "body": "function updateMetadata() {\r\n    var context = getContext();\r\n    var collection = context.getCollection();\r\n    var response = context.getResponse();\r\n    var createdDocument = response.getBody();\r\n\r\n    // query for metadata document\r\n    var filterQuery = 'SELECT * FROM root r WHERE r.id = \"_metadata\"';\r\n    var accept = collection.queryDocuments(collection.getSelfLink(), filterQuery,\r\n      updateMetadataCallback);\r\n    if(!accept) throw \"Unable to update metadata, abort\";\r\n\r\n    function updateMetadataCallback(err, documents, responseOptions) {\r\n      if(err) throw new Error(\"Error\" + err.message);\r\n           if(documents.length != 1) throw 'Unable to find metadata document';\r\n           var metadataDocument = documents[0];\r\n\r\n           // update metadata\r\n           metadataDocument.createdDocuments += 1;\r\n           metadataDocument.createdNames += \" \" + createdDocument.id;\r\n           var accept = collection.replaceDocument(metadataDocument._self,\r\n               metadataDocument, function(err, docReplaced) {\r\n                  if(err) throw \"Unable to update metadata, abort\";\r\n               });\r\n           if(!accept) throw \"Unable to update metadata, abort\";\r\n           return;          \r\n    }",  
    "id": "PostTrigger-UpdateMetaAll",  
    "triggerOperation": "All",  
    "triggerType": "Post"  
}  
  
HTTP/1.1 200 Ok  
Cache-Control: no-store, no-cache  
Pragma: no-cache  
Content-Type: application/json  
Content-Location: https://contosomarketing.documents.azure.com/dbs/volcanodb/colls/volcano1/triggers/PostTrigger-UpdateMetaAll  
Server: Microsoft-HTTPAPI/2.0  
Strict-Transport-Security: max-age=31536000  
x-ms-last-state-change-utc: Sun, 29 Nov 2015 19:20:18.154 GMT  
etag: "060022e5-0000-0000-0000-566882360000"  
x-ms-resource-quota: triggers=25;  
x-ms-resource-usage: triggers=1;  
x-ms-schemaversion: 1.1  
x-ms-alt-content-path: dbs/volcanodb/colls/volcano1  
x-ms-content-path: Sl8fALN4sw4=  
x-ms-quorum-acked-lsn: 42  
x-ms-session-token: 43  
x-ms-current-write-quorum: 3  
x-ms-current-replica-set-size: 4  
x-ms-request-charge: 12.57  
x-ms-serviceversion: version=1.5.57.3  
x-ms-activity-id: 614411ac-2abb-4770-a1f9-3533d7339ec6  
Set-Cookie: x-ms-session-token=43; Domain=contosomarketing.documents.azure.com; Path=/dbs/volcanodb/colls/volcano1  
x-ms-gatewayversion: version=1.5.57.3  
Date: Wed, 09 Dec 2015 19:34:15 GMT  
Content-Length: 1565  
  
{  
    "body": "function updateMetadata() {\r\n    var context = getContext();\r\n    var collection = context.getCollection();\r\n    var response = context.getResponse();\r\n    var createdDocument = response.getBody();\r\n\r\n    // query for metadata document\r\n    var filterQuery = 'SELECT * FROM root r WHERE r.id = \"_metadata\"';\r\n    var accept = collection.queryDocuments(collection.getSelfLink(), filterQuery,\r\n      updateMetadataCallback);\r\n    if(!accept) throw \"Unable to update metadata, abort\";\r\n\r\n    function updateMetadataCallback(err, documents, responseOptions) {\r\n      if(err) throw new Error(\"Error\" + err.message);\r\n           if(documents.length != 1) throw 'Unable to find metadata document';\r\n           var metadataDocument = documents[0];\r\n\r\n           // update metadata\r\n           metadataDocument.createdDocuments += 1;\r\n           metadataDocument.createdNames += \" \" + createdDocument.id;\r\n           var accept = collection.replaceDocument(metadataDocument._self,\r\n               metadataDocument, function(err, docReplaced) {\r\n                  if(err) throw \"Unable to update metadata, abort\";\r\n               });\r\n           if(!accept) throw \"Unable to update metadata, abort\";\r\n           return;          \r\n    }",  
    "id": "PostTrigger-UpdateMetaAll",  
    "triggerOperation": "All",  
    "triggerType": "Post",  
    "_rid": "Sl8fALN4sw4BAAAAAAAAcA==",  
    "_ts": 1449689654,  
    "_self": "dbs\/Sl8fAA==\/colls\/Sl8fALN4sw4=\/triggers\/Sl8fALN4sw4BAAAAAAAAcA==\/",  
    "_etag": "\"060022e5-0000-0000-0000-566882360000\""  
}  
  

另請參閱