Aggregation not working on Realm App

Hi,
i have a very simple collection of document each document has few fields like s, a, q, p and values etc. i made an aggregation from the online framework and it is working very good. the last step of the aggregation is to merge the result into a new collection. this is the my aggregation:

[
  {
    '$addFields': {
      'a': {
        '$toDecimal': '$a'
      }, 
      'A': {
        '$toDecimal': '$A'
      }, 
      'B': {
        '$toDecimal': '$B'
      }, 
      'b': {
        '$toDecimal': '$b'
      }, 
      'O': {
        '$toLong': '$O'
      }, 
      'C': {
        '$toLong': '$C'
      }, 
      'E': {
        '$toLong': '$E'
      }, 
      'c': {
        '$toDecimal': '$c'
      }, 
      'h': {
        '$toDecimal': '$h'
      }, 
      'p': {
        '$toDecimal': '$p'
      }, 
      'P': {
        '$toDecimal': '$P'
      }, 
      'q': {
        '$toDecimal': '$q'
      }, 
      'Q': {
        '$toDecimal': '$Q'
      }, 
      'o': {
        '$toDecimal': '$o'
      }, 
      'v': {
        '$toDecimal': '$v'
      }, 
      'n': {
        '$toDecimal': '$n'
      }, 
      'l': {
        '$toDecimal': '$l'
      }, 
      'w': {
        '$toDecimal': '$w'
      }
    }
  }, {
    '$match': {
      'E': {
        '$gt': 1, 
        '$lt': 10000000000000000000
      }
    }
  }, {
    '$project': {
      's': '$s', 
      'E': '$E', 
      'O': '$O', 
      'C': '$C', 
      'o': '$o', 
      'h': '$h', 
      'l': '$l', 
      'c': '$c', 
      'p': '$p', 
      'P': '$P', 
      'w': '$w', 
      'b': '$b', 
      'B': '$B', 
      'a': '$a', 
      'A': '$A', 
      'Q': '$Q', 
      'v': '$v', 
      'q': '$q', 
      '_id': 0
    }
  }, {
    '$sort': {
      'E': 1
    }
  }, {
    '$group': {
      '_id': '$s', 
      'data': {
        '$push': '$$ROOT'
      }
    }
  }, {
    '$addFields': {
      's': '$_id', 
      'sns': '$_id', 
      'ms': 1, 
      'me': 99999999999, 
      'mo': {
        '$first': '$data.c'
      }, 
      'mh': {
        '$max': '$data.c'
      }, 
      'ml': {
        '$min': '$data.c'
      }, 
      'mc': {
        '$last': '$data.c'
      }
    }
  }, {
    '$project': {
      '_id': 0
    }
  }, {
    '$merge': {
      'into': 'Minutes'
    }
  }
]

I have a trigger which runs every 1 minute and trigger a function this is my function code;

exports = async function() {
  const mongodb = context.services.get("mongodb-atlas");
  const minutesCollection = mongodb.db("Prices").collection("Minutes");
  const SecondsCollection = mongodb.db("Prices").collection("Seconds");
  var currentTimeStamp = new Date().getTime();
  var currentMilliSeconds = Date.now() % 60000;
  var lastMinuteStarted = currentTimeStamp - currentMilliSeconds - 60000;
  var lastMinuteEnded = currentTimeStamp - currentMilliSeconds;
  
  const agg = [
  {
    '$addFields': {
      'a': {
        '$toDecimal': '$a'
      }, 
      'A': {
        '$toDecimal': '$A'
      }, 
      'B': {
        '$toDecimal': '$B'
      }, 
      'b': {
        '$toDecimal': '$b'
      }, 
      'O': {
        '$toLong': '$O'
      }, 
      'C': {
        '$toLong': '$C'
      }, 
      'E': {
        '$toLong': '$E'
      }, 
      'c': {
        '$toDecimal': '$c'
      }, 
      'h': {
        '$toDecimal': '$h'
      }, 
      'p': {
        '$toDecimal': '$p'
      }, 
      'P': {
        '$toDecimal': '$P'
      }, 
      'q': {
        '$toDecimal': '$q'
      }, 
      'Q': {
        '$toDecimal': '$Q'
      }, 
      'o': {
        '$toDecimal': '$o'
      }, 
      'v': {
        '$toDecimal': '$v'
      }, 
      'n': {
        '$toDecimal': '$n'
      }, 
      'l': {
        '$toDecimal': '$l'
      }, 
      'w': {
        '$toDecimal': '$w'
      }
    }
  }, {
    '$match': {
      'E': {
        '$gt': lastMinuteStarted, 
        '$lt': lastMinuteEnded
      }
    }
  }, {
    '$project': {
      's': '$s', 
      'E': '$E', 
      'O': '$O', 
      'C': '$C', 
      'o': '$o', 
      'h': '$h', 
      'l': '$l', 
      'c': '$c', 
      'p': '$p', 
      'P': '$P', 
      'w': '$w', 
      'b': '$b', 
      'B': '$B', 
      'a': '$a', 
      'A': '$A', 
      'Q': '$Q', 
      'v': '$v', 
      'q': '$q', 
      '_id': 0
    }
  }, {
    '$sort': {
      'E': 1
    }
  }, {
    '$group': {
      '_id': '$s', 
      'data': {
        '$push': '$$ROOT'
      }
    }
  }, {
    '$addFields': {
      's': '$_id', 
      'sns': '$_id', 
      'ms': lastMinuteStarted, 
      'me': lastMinuteEnded, 
      'mo': {
        '$first': '$data.c'
      }, 
      'mh': {
        '$max': '$data.c'
      }, 
      'ml': {
        '$min': '$data.c'
      }, 
      'mc': {
        '$last': '$data.c'
      }
    }
  }, {
    '$project': {
      '_id': 0
    }
  }, {
    '$merge': {
      'into': 'Minutes'
    }
  }
];

  var result = await SecondsCollection.aggregate(agg);

  console.log('This is result', result);
};

it doesnt matter if i run it manually or it gets triggered it does nothing where as the expected result is a single document should be pushed (inserted) into the “Minutes” collection.
Thanks

The function i am running is on system context. i changed it from user to system.

and also i dont know why it needed .toArray() at the end to work.

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