vishal gupta
vishal gupta

Reputation: 11

Azure Functions unable to update Multiple Cosmos Document

Have been trying since a few days and seems like a bug in Azure Functions. I am trying to use a timer trigger, read docs from Cosmos DB (input Binding) and update a few properties for every document (Cosmos DB Output Binding). No Matter what I try the new properties are not pushed to Cosmos DB. I am ensuring the id remains the same.

My function.json below:

{
  "bindings": [
    {
      "name": "myTimer",
      "type": "timerTrigger",
      "direction": "in",
      "schedule": "0 */10 * * * *"
    },
    {
      "name": "DocumentInput",
      "type": "cosmosDB",
      "databaseName": "MyDB1",
      "collectionName": "MyCC1",
      "sqlQuery": "SELECT * FROM c",
      "partitionKey": "/key",
      "connectionStringSetting": "CosmosDBConnectionExp",
      "direction": "in"
    },
    {
      "name": "DocumentOutput",
      "type": "cosmosDB",
      "databaseName": "MyDB1",
      "collectionName": "MyCC1",
      "createIfNotExists": false,
      "partitionKey": "/key",
      "connectionStringSetting": "CosmosDBConnectionExp",
      "direction": "out"
    }
  ]
}

My function is below:

const PushDataChanges = async (context) => {
    
    var cosmosOutput = context.bindings.DocumentOutput;
    var cosmosInput = context.bindings.DocumentInput;
   
    cosmosOutput = cosmosInput;
 

    for(var i = 0; i < cosmosOutput.length; i++) {
   
        var document = cosmosOutput[i];
        var docid = cosmosOutput[i].id;
        var now = new Date().toISOString();

        document.id = docid; //Making sure the id key is same so upsert or update happens.
        document.experiment.lastProcessedTime = "now";
        document.experiment.updateStatus = "This is now updated";
    }

    context.done();

}


module.exports = function (context, Timer) {
    
    context.log('Starting Delta Data Extraction and Sentiment Analysis');
    PushDataChanges(context);
     
};

Any help would be greatly appreciated.

Upvotes: 0

Views: 613

Answers (1)

vishal gupta
vishal gupta

Reputation: 11

This is what finally what worked for me after a lot of trial and error ! cosmos Bindings for Azure functions are pretty poorly documented. It took me a lot of time to infer from given example in documentation that shows how to update only one document. The below code gives a way to update/upsert for multiple cosmos DB documents through Azure Functions using Java script.

Function code:

const PushDataChanges = async (context) => {
    
    var inputdoc = context.bindings.DocumentInput;
   
    for(var i = 0; i < inputdoc.length; i++) {

        console.log(inputdoc.length);

        var now2 = new Date().toISOString();

        inputdoc[i].experiment.updateStatus = "This is updated now";
        inputdoc[i].experiment.lastProcessedTime = now2;
        context.bindings.DocumentOutput = inputdoc;
    }


context.done;
}

module.exports = function (context, Timer) {
    
    context.log('Starting the Azure Function');

    PushDataChanges(context);

    context.done();
     
};

Below are my Bindings:

{
  "bindings": [
    {
      "name": "myTimer",
      "type": "timerTrigger",
      "direction": "in",
      "schedule": "0 0 */8 * * *"
    },
    {
      "type": "cosmosDB",
      "name": "DocumentInput",
      "databaseName": "gsk-next-internal-datastore",
      "collectionName": "experiment",
      "sqlQuery": "SELECT * FROM c",
      "connectionStringSetting": "CosmosDBConnectionExp",
      "direction": "in"
    },
    {
      "type": "cosmosDB",
      "name": "DocumentOutput",
      "databaseName": "gsk-next-internal-datastore",
      "collectionName": "experiment",
      "createIfNotExists": false,
      "connectionStringSetting": "CosmosDBConnectionExp",
      "direction": "out"
    }
  ]
}

Upvotes: 1

Related Questions