0
votes

At https://docs.microsoft.com/en-us/azure/azure-functions/functions-bindings-documentdb#input-sample-for-single-document

the following binding is given:

{
  "name": "inputDocument",
  "type": "documentDB",
  "databaseName": "MyDatabase",
  "collectionName": "MyCollection",
  "id" : "{queueTrigger}",
  "connection": "MyAccount_COSMOSDB",     
  "direction": "in"
}

I am using azure function, with service bus topic Trigger, and document db as Output, I am thinking to use documentDb as input with id similar to mentioned above. I am using nodejs.

There is no description for above part at the above link, I think above means, we get a document from azure documentdb, and since the id is given as queueTrigger, if there is a message in service bus queue with id1, then we get a document with same id1 that is in documentdb.

My requirement is I need to know if there is a document in documentdb, with same id as in service bus topic (not queue), how to go about it.

The whole aim is to update a record in documentdb, retaining some of the earlier attributes of the document, instead of replacing an existing document in document db, by using the message that comes from service bus topic.

Appreciated any help on any discussion on similar situations as above and how you solved, even if it is not using bindings.

Update: More info: The "id" : "{queueTrigger}", is not working for service bus as trigger. I din't find the MS documentation of "{queueTrigger}", so tried "{serviceBusTrigger}" but "id" : "{serviceBusTrigger}" also did not work.

I would need similar to "id" : "{queueTrigger}", for service bus as trigger and documentdb as input.

1

1 Answers

0
votes

Here is the binding that does the trick:

{
  "bindings": [
    {
      "name": "myMsg",
      "type": "serviceBusTrigger",
      "direction": "in",
      "topicName": "mytopic",
      "subscriptionName": "mysubscription",
      "connection": "myconnection",
      "accessRights": "Manage"
    },
    {
      "type": "documentDB",
      "name": "inputDocument",
      "databaseName": "testdb",
      "collectionName": "test",
      "connection": "mydocdb",
      "direction": "in",
      "id": "{myMsg}"
    }
  ],
  "disabled": false
}

Then the document will be available for your function to process:

module.exports = function(context, myMsg) {
    context.log('Processing message', myMsg);
    context.log(context.bindings.inputDocument);
    context.done();
};

Alternatively, if your message is JSON of e.g. the following format:

{ "docid": "31c5c592-94a3-4922-82e4-fcda8366280c" }

You can also bind to docid property:

{
  "bindings": [
    {
      "name": "myMsg",
      "type": "serviceBusTrigger",
      "direction": "in",
      "topicName": "mytopic",
      "subscriptionName": "mysubscription",
      "connection": "myconnection",
      "accessRights": "Manage"
    },
    {
      "type": "documentDB",
      "name": "inputDocument",
      "databaseName": "testdb",
      "collectionName": "test",
      "connection": "mydocdb",
      "direction": "in",
      "id": "{docid}"
    }
  ],
  "disabled": false
}

Read more about Binding Expressions