Creating an Index within a scheduled trigger

I have a scheduled trigger that depends on an index, I would like to ensure that the index exists before running the trigger. I’ve attempted to do it this way:

const collection = context.services.get("mycluster").db("mydb").collection("mycollection");
collection.createIndex({ "StartTime": 1 });

But I am getting an error saying createIndex does not exist. Is there any way to ensure an index exists within a scheduled trigger?

Hi @Greg_Fitzpatrick-Bel,

Creating an index via createIndex commanf is not supported by the collection Realm API. Run of runCommand is not available as well.

Having said that, if you are using a dedicated cluster you can use the rolling index create atlas API via a context.http request from your function.

https://docs.atlas.mongodb.com/reference/api/rolling-index-create-one/

The following blog can show case how to use atlas api via a trigger:

Please bear in mind that a trigger is bound to 90s execution time. A trick can be to have a flow collection saving a state of the task and updating the state after each run. Then a database based follow-up trigger can be run until completion which will stop the execution chain.

Let me know if that helps.

Best regards
Pavel