Prevent trigger from infinite loop


I stuck in a infinte loop with my trigger. does anyone know how to prevent from getting in the infinite loop?

I have a worktime document in a user realm (partition: user=userId). A admin-member should be allowed to edit the worktime from other user. For that I want to copy or update the worktime into their realms. The trigger runs on the Worktime-Collection so obviously the trigger ends in a loop…

Here my trigger function:

exports = async function(changeEvent) {
  const db ="project").db("test");
  const worktimeCollection = db.collection("Worktime");
  const teamCollection ="project").db("test").collection("Team");
  var worktime = JSON.parse(JSON.stringify(changeEvent.fullDocument));
  const docId = changeEvent.documentKey._id;
  if (changeEvent.operationType == "insert") {
    const team = await teamCollection.findOne({ _id: new BSON.ObjectId(worktime.teamId) });
    team.membersAdmin.forEach(userId => {
      if (userId !== worktime.createdBy) {
        worktime._id = new BSON.ObjectId();
        worktime._partition = `user=${userId}`;
        worktime._wid = docId;
        worktimeCollection.insertOne(worktime).then(() => {
          console.log('Document added');
  if (changeEvent.operationType == "update") {
    worktimeCollection.updateMany({ _wid: docId }, worktime, { upsert: true }).then(() => {
      console.log('Document updated');
  if (changeEvent.operationType == "delete") {
    worktimeCollection.deleteMany({ _wid: docId }).then(() => {
      console.log('Document deleted');

thank you

I’ve hit a similar problem in the past.

What I did was to include some extra information in the document so that I could tell that the write had been made by the trigger.

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