共用方式為


建立觸發程序

作業 Create Trigger 會在集合中建立新的觸發程式。

要求

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

標題

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

主體

屬性 必要 類型 描述
id 必要 String 它用來識別觸發程序的唯一名稱。 識別碼不能超過 255 個字元。
body 必要 String 它是觸發程式的主體。
triggerOperation 必要 String 它是叫用觸發程式的作業類型。 可接受的值為: AllCreateReplaceDelete。
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"  
}  
  

回應

標題

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

狀態碼

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

HTTP 狀態碼 Description
201 Created 作業成功。
400 不正確的要求 JSON 內文無效。 檢查是否缺少大括號或引號。
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": 1449689248,  
    "_self": "dbs\/Sl8fAA==\/colls\/Sl8fALN4sw4=\/triggers\/Sl8fALN4sw4BAAAAAAAAcA==\/",  
    "_etag": "\"0600fbe4-0000-0000-0000-566880a00000\""  
}  
  

範例

POST https://contosomarketing.documents.azure.com/dbs/volcanodb/colls/volcano1/triggers HTTP/1.1  
x-ms-session-token: 41  
x-ms-date: Wed, 09 Dec 2015 19:27:28 GMT  
authorization: type%3dmaster%26ver%3d1.0%26sig%3dHmx5WSnyLdbJnDB1Ixme8uGbUSl%2f96HkCgSsSnsSq%2fI%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=41  
Content-Length: 1380  
Expect: 100-continue  
Connection: Keep-Alive  
  
{  
    "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 201 Created  
Cache-Control: no-store, no-cache  
Pragma: no-cache  
Content-Type: application/json  
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: "0600fbe4-0000-0000-0000-566880a00000"  
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: 41  
x-ms-session-token: 42  
x-ms-current-write-quorum: 3  
x-ms-current-replica-set-size: 4  
x-ms-request-charge: 6.29  
x-ms-serviceversion: version=1.5.57.3  
x-ms-activity-id: b407dfec-b6ec-4aa5-9833-eadad231fcd4  
x-ms-gatewayversion: version=1.5.57.3  
Date: Wed, 09 Dec 2015 19:27:29 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": 1449689248,  
    "_self": "dbs\/Sl8fAA==\/colls\/Sl8fALN4sw4=\/triggers\/Sl8fALN4sw4BAAAAAAAAcA==\/",  
    "_etag": "\"0600fbe4-0000-0000-0000-566880a00000\""  
}  
  

另請參閱