How to configure mongo trigger for dev and prod cluster using Nodejs ?
I have some query regarding mongodb trigger. I have 2 cluster - ( DEV and PROD ) I want to create 2 trigger 1 for dev and 1 for prod. By default - I see Dev and Prod are getting selected under link data source, and it is disable I am not able to uncheck it.
I know I can remove linked data source from — App service —> Triggers —> Manage triggers.
But this is not the solution. Please help me. Do I need to create all together diff account for DEV and Prod ? I don’t think so it is the feasible solution.
I am pulling the data from 1 collection and inserting to another collections.
What is happening now - Pull the data from dev - but it is inserting to prod and dev both cluster. Because both are tagged to it.
More details Here is the code -
// register activity
exports = async function(changeEvent) {
try {
const sourceDatabase = "Abcd"; // Source database
const targetDatabase = "Abcd"; // Target database
const targetCollectionName = "activities"; // Target collection name
// Get references to the MongoDB collections in both databases
const sourceCollection = context.services.get(sourceDatabase).db(changeEvent.ns.db).collection(changeEvent.ns.coll);
const targetDB = context.services.get(targetDatabase).db(targetDatabase);
if (changeEvent.operationType === "insert") {
// Insert event: replicate the user document to the Activity collection
const userDocument = changeEvent.fullDocument;
console.log("User Document:", JSON.stringify(userDocument));
if (userDocument.custom && userDocument.custom.role === "customer") {
// Transform the user data to match the Activity schema
const activityData = {
user: userDocument._id, // Reference to the User
userId: userDocument.sub,
email: userDocument.email,
activities: {
premiumPlan: false,
firstVideoWatched: false,
currentVideo_3days_WatchStreak: 0, // Updated field name
longestVideo_7days_WatchStreak: 0, // Updated field name
animalExpertCount: 0,
categoryExperts: [],
},
};
try {
const targetCollection = targetDB.collection(targetCollectionName);
// Insert the transformed data into the Activity collection
targetCollection.insertOne(activityData);
} catch(err) {
console.log("error performing mongodb write: ", JSON.stringify(err));
}
}
}
} catch(err) {
console.log("error performing mongodb write: ", err.message);
}
};
I think its a mongo db bug
Stack overflow link - node.js - How to configure mongo trigger for dev and prod cluster using Nodejs? - Stack Overflow