Get current cluster name inside of trigger function

Is there a way to find out the current cluster name inside of a trigger function? I have a database trigger that updates a field in a document when certain fields are changed in said document - however this is a trigger I would like to deploy across a number of clusters. I can get the current database name from ns in a changeEvent, but I cannot access the cluster name, and I don’t want to have to maintain versions of the code per cluster.

Below is an abridged sample of a function - I can get the dbName, but I cannot get the cluster name - you can see in this example that it is hardcoded to Dev. How do I use the same function code in Dev, Demo, QA etc without hardcoding the cluster name or having to create a new project per cluster so that the cluster name is consistent?

exports = async function (changeEvent) {
  const { ns, updateDescription, fullDocument } = changeEvent;
  const dbName = ns.db;
  
  // Construct full name from partial names
  const { _id, firstName, middleNames, lastName } = fullDocument;
  let fullName = (firstName + " " + middleNames + " " + lastName).replace(/\s+/g, ' ').trim()

  // Connect to clients collection
  const clients = context.services
    .get("Dev")
    .db(dbName)
    .collection("clients");
    
  // Persist to client document
  const query = { _id: _id };
  const update = {
    $set: {
      fullname: fullName,
    },
  };
  const options = { upsert: true };
  await clients.updateOne(query, update, options);
}
1 Like

Well this is unusual - I had two applications - the default one created when I created my first trigger from Atlas, and another one I created directly in App Services.

In the Trigger application, I needed to refer to the data source by the cluster name “Dev”.

In the App Services created application - I was able to use the default “mongodb-atlas”. If I just ensure I create applications before creating the first trigger, my question is moot as the data source can be referred to by “mongodb-atlas”.