Is there any way to pass a value from a Trigger to a Function?

Is it possible to pass a variable from a trigger to a function? I have a use case where I would like to set up the same scheduled trigger on a number of databases, all using exactly the same function (except the database name in the function that the function will connect to). I don’t want to use a Value with a list of databases to iterate over, as the triggers will be for databases in different timezones etc.

So can the function get any calling information from a trigger, or can they only retrieve values using context.values.get(“valueName”)? Is the name of the trigger in a header anywhere in context.request?

Have a look at this “context” page: Context — Atlas App Services (mongodb.com)

I haven’t tried it myself but context.request might be useful. or you may embed the details in the payload.

My current workaround is to have one application per database, and the application name is the same as the database name. So when a scheduled trigger needs to know which database to connect to, I can use a snippet like:

  // Get the application name from the context
  // e.g. dev2
  const appName = context.app.name;

  // Connect to the database with the same name as the application
  // e.g mongodb-atlas/dev2
  const db = context.services
    .get("mongodb-atlas")
    .db(appName);

When the trigger runs on a schedule, the app name is available, and I use that as the database name. Just means adhering to a naming standard.

Pretty sure the context.request is empty for a scheduled trigger.

what about “change events”? it seems it carries the name of database and collection that was changed.

your function would have this shape: exports = function (changeEvent) {

changeEvent is only available in a database trigger, not a scheduled trigger.

Forgive my naive approaches. I missed “scheduled” part. Can you please edit the title to have “Scheduled Trigger”. It would give a better concept to the next person to join.

As for my understanding, though, they seem to work just to run a function at specified times and delegates the work of selecting database/collection to the function, calling it with no arguments. I scheduled a function and used function(...args){ and logged args with JSON.stringify(args), the result is [{}] suggesting truth to my assumption. Seems you have to implement your own logic to differentiate databases with the “Date.now()”