0

At https://learn.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.

Manohar Reddy Poreddy
  • 25,399
  • 9
  • 157
  • 140

1 Answers1

0

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

Mikhail Shilkov
  • 34,128
  • 3
  • 68
  • 107
  • Thanks for your time and reply. I have not tested it, but even if something like above works, it is not similar to "queueTrigger". The "queueTrigger" way is required because it is documented, and so Microsoft supports it for sure. Is there a link on microsoft on how you suggested, so I rely on the above solution? – Manohar Reddy Poreddy Nov 24 '17 at 09:24
  • Umm... It's exactly the same as `queueTrigger`, I just renamed it to `myMsg`. I can rename it back to `serviceBusTrigger` if that gives you more confidence. Added a link too. – Mikhail Shilkov Nov 24 '17 at 09:33
  • Your earlier answer started with "I am not sure how {queueTrigger} syntax works". I missed to read your updated answer, even this latest answer din't work for me. The link you shared I was reviewing already. Unfortunately, documentation from Microsoft has confusing naming convention ("queueTrigger"), after all it was not a trigger. What worked for me is different than all your answers, will write about it later. – Manohar Reddy Poreddy Nov 24 '17 at 14:39
  • In what way didn't it work for you? Works for me like charm. – Mikhail Shilkov Nov 24 '17 at 14:41
  • Not sure why it works for you. Ideally, it shouldn't. When I write an answer, you will see why. – Manohar Reddy Poreddy Nov 24 '17 at 14:44