Trigger fired function receives empty context.services with CREATE_COLLECTION event

Hello dear community :kissing_heart:

I’m trying to set up a trigger to mirror some parts of a database to some other cluster.database.
Everythig seem to work well for documents events : function gets triggered and receives fully fledged context object. I can use context.values.get("TARGET_CLUSTER").db(database).collection(changeEvent.ns.coll) 's methods, such as insertOne, replaceOne, etc.

Alas, when it comes to some collections events like create, I can witness that context.services = {}. Hence impossible to replicate collections creations/deletions.

I also tried to use nodejs mongodb module (added to trigger as a dependency) and go with usual const client = new MongoClient(connectionUri). Alas in that case I get error :

Error performing MongoDB operation: MongoDB Node.js Driver is not supported. Please visit our Atlas App Services documentation.

Also, Data API is Deprecated, then cannot help in the long term.
Finally, setting up a scheduled trigger for collections events is not an option, as it would potentially break replication (collection not yet scheduled/created yet, and documents to be
realtime replicated).

I may have missed some important bits in documentation ?

Great thanks for your wisdom !!

Hi @Ben_Soille ,

By itself, context.services has no intrinsic value, you can only call its get interface, it will always look like from inside the Function as {}

  • What’s your exact context.services.get("<service>") command?
  • Does the name match the Service Name you’ve set in the Data Sources?
  • Are you observing the whole database, not just one collection?
  • Can you post a sample of a create change event?

You cannot do that, because the internal engine that runs the Functions (and thus in turn Triggers) has already its version of a MongoDB Driver: it may have limitations (on purpose), but you cannot override it. If you need to work with the full capability of a MongoDB Driver, than you have to launch a process somewhere else, and use db.watch() instead of relying on Triggers.

Here is a create change event sample :

{
  "_id": {
    "_data": "XXXXXXXXXXXXXXXXXX"
  },
  "operationType": "create",
  "clusterTime": {
    "$timestamp": {
      "t": 1745597454,
      "i": 1
    }
  },
  "collectionUUID": {},
  "wallTime": "2025-04-25T16:10:54.252Z",
  "ns": {
      "db": "pax",
      "coll": "benTestColl"
    },
  "operationDescription": {
      "idIndex": {
        "v": 2,
        "key": {
          "_id": 1
        },
        "name": "_id_"
      }
    }
}

Thank you VERY MUCH for your answer !!!

I tried following syntax :

try {
  // Get the target service and database info
  // TARGET_CLUSTER_NAME is defined in values, and returns exact datasource name
  const serviceName = context.values.get("PAX_TARGET_CLUSTER");
  const database = "pax";
    
  // Get service from context
  const targetsn = context.services.get(serviceName);
  console.log(`Target SN: ${JSON.stringify(targetsn, null, 2)}`);

  // Get db connection
  const targetdb = targetsn.db(database);
  console.log(`Target DB: ${JSON.stringify(targetdb, null, 2)}`);

  // Actually perform a mongo action
  // changeEvent being the payload the function receives as an argument :
  await targetdb.dropCollection(changeEvent.ns.coll);
  
} catch(err) {
  console.log("Error performing MongoDB operation:", err.message);
}

And then output is :

Target SN: {
“version”: 1
}
Target DB: {}
Error performing MongoDB operation: ‘dropCollection’ is not a function

Indeed, context.services.get(serviceName) is {}

Hi @Ben_Soille,

That’s a different issue: only a limited subset of the MongoDB Driver capability is available to Functions, and dropCollection() isn’t among the supported commands. If you need to call privileged commands like dropCollection() or adminCommand(), then Triggers isn’t the right solution.

And again, trying JSON.stringify() on internal structures isn’t significant, them being non-null is the most you can check.

OK, thank you VERY MUCH for clarifying !
I indeed missed an important documentation part :slight_smile:

My implementation should be functionnally comprehensive enough, as a new document would create new collection in target DB.

Thank you again !!

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.