I send data towards an Azure IoTHub. This data is then retreived and processed by a IoTHub EventHub function.
This function retreives the data and insert this data into a Azure Cosmos DB.
In the IoTHub EventHub function you have to declare the Cosmos database and the Cosmos collection before the function can run.
The problem is that I want to use the a dynamic collection name. This name depends on the data which is send towards the IoTHub. Is this possible? Can I declare the collection name when the function is running?
With the scripts below it is possible to send towards one collection
function.json:
{
"bindings": [
{
"type": "eventHubTrigger",
"name": "IoTHubMessages",
"direction": "in",
"path": "poc_funceventhubname",
"connection": "POCIoTHub_events_IOTHUB",
"cardinality": "many",
"consumerGroup": "functions"
},
{
"type": "documentDB",
"name": "outputDocument",
"databaseName": "VALUES",
"collectionName": "POCVALUES",
"createIfNotExists": true,
"connection": "pocCosmos_DOCUMENTDB",
"direction": "out"
}
],
"disabled": false
}
index.js:
module.exports = function (context, IoTHubMessages) {
var v;
var output = [];
IoTHubMessages.forEach(message => {
v = message.v;
context.log(`v = ${v}`);
for(var i = 0; i < message.REGS.length; i++) {
var obj = message.REGS[i];
output[i] = {
"vi": v,
"pi": obj[0],
"ts": obj[2],
"vl": obj[1]
};
context.bindings.outputDocument = output;
}
});
context.done();
};
Summery:
I want to use a variable collectionName, that will declared in the index.js?
How do I declare the collectionName in de function.json and can I declare this variable in de index.js?