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 ?
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.
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
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.