Hi @Pavel_Duchovny ,
Thank you for taking the time to look at my issue. Yes, I don’t have any experience with JS so the Trigger function code was mostly just a copy and paste from this resource. The cron schedule is set to +3 hours in my IST zone, which I think should correspond with midnight of the broker server time in Bucharest.
I made some changes to the code based on your feedback and here is the updated code of the trigger function.
exports = function() {
/*
A Scheduled Trigger will always call a function without arguments.
Documentation on Triggers: https://docs.mongodb.com/realm/triggers/overview/
*/
const mongodb = context.services.get('mongodb-atlas');
const db = mongodb.db("MT5_AccountMetrix");
const FloatingEquity = db.collection("FloatingEquity");
const test_item = FloatingEquity.find().sort({"equity":-1}).limit(1);
console.log(test_item);
const FloatingEquityDailyReport = db.collection("FloatingEquityDailyReport");
// const generatedObjectId = new BSON.ObjectId();
//Generate Daily Report
return FloatingEquity.aggregate([
{
$match: {
createdAt:{
$gte: new Date(new Date().setHours(0,0,0,0)), //This DateTime needs to be in 'Europe/Bucharest' TimeZone of Mt5 broker. Is this correct?
$lt : new Date(new Date().setHours(23,59,59,999)), //This DateTime needs to be in 'Europe/Bucharest' TimeZone of Mt5 broker. Is this correct?
},
},
},
{
$group:{
_id: null,
min_equity: {$min : "$equity"},
max_equity: {$max : "$equity"},
timestamp_min_equity : {}, //What should go here?
timestamp_max_equity: {} //what should go here?
},
},
{
$merge : {
into : "FloatingEquityDailyReport", // Output Collection name
on: "", //What should go here?
}
},
console.log("MinEquity:", min_equity),
]).next()
// .then(dailyReport => {FloatingEquityDailyReport.insertOne(dailyReport);})
.catch(err => console.error("Failed to Generate FloatingEquityDataReport:", err));
}
The current hazards I encounter are as follows:
1.) How do I print variables in the mongoDB realm function editor? I tried using console.log to print the test_item and the min_equity values but it does not work?
2.) Am I doing the $match condition to filter the documents between the start and end of the day as corresponding to Bucharest time zone? How do i set the timezone in $gte and $lt line?
3.) How do I retrieve the timestamp_min_equity which corresponds to the time field in the document ID which contains the min_equity value? Also the same for timestamp_max_equity?
4.) How do i use $merge operator to push the aggregated values into the target collection FloatingEquityDailyReport?
For the purpose of helping to save the time for all of us instead of doing back and forth code iterations, May i kindly request you to provide the correct solution by editing my JS code?
Best Regards,
Dilip
