azureazure-functionscosmosdbtrigger

How to identify "insert" vs "update" diff in Azure Cosmos DB trigger?


I have created a function triggered by Azure Cosmos DB by following the documentation below, which is working:

Upon seeing logs, I am unable to identify if this trigger is for INSERT or UPDATE. I know that in AWS, when you add a lambda as the dynamo trigger, you could identify that easily. You can even see what was original record Vs updated one (for update).

Question

I am new to Azure, so it is possible that I am missing something in my function code.

Function Code

module.exports = async function (context, documents) {
    context.log('1');
    if (!!documents && documents.length > 0) {
        context.log('Document Id: ', documents[0].id); <-- SHOWS ID OF THE RECORD
        context.log('Document[0]', documents[0]); <-- ENTIRE RECORD
        context.log('Documents', documents); <-- ALL RECORDS 
    }
}

Context object from above

{
  invocationId: '3ee0136e-d005-4517-a11e-c43ec9ca7c67',
  traceContext: {
    traceparent: '00-4938136b13476845524c7ae5059d84f3-badfbc56b5ef0067-00',
    tracestate: '',
    attributes: {
      OperationName: 'CosmosTrigger1'
    }
  },
  executionContext: {
    invocationId: '3ee0136e-d005-4517-a11e-c43ec9ca7c67',
    functionName: 'CosmosTrigger1',
    functionDirectory: 'C:\\home\\site\\wwwroot\\CosmosTrigger1',
    retryContext: null
  },
  bindings: {
    documents: [
      [
        Object
      ]
    ]
  },
  log: [
    Function(anonymous)
  ]{
    error: [
      Function: error
    ],
    warn: [
      Function: warn
    ],
    info: [
      Function: info
    ],
    verbose: [
      Function: verbose
    ]
  },
  bindingData: {
    invocationId: '3ee0136e-d005-4517-a11e-c43ec9ca7c67'
  },
  bindingDefinitions: [
    {
      name: 'documents',
      type: 'cosmosDBTrigger',
      direction: 'in'
    }
  ],
  done: [
    Function(anonymous)
  ]
}

Function Log enter image description here


Solution

  • Unfortunately, you can't distinguish between the two types (insert & update). The trigger consumes the Change Feed, and you can't filter it for a specific type of operation.

    Today, you see all inserts and updates in the change feed. You can't filter the change feed for a specific type of operation.