I tried to create a cron
job that gets data from one collection and do some operations using aggregate method and finally dump the output to another collection in same database (using $merge
). If I try the code for aggregate in mongo shell, then the $merge
happens successfully. But when it is done in a cron job, I could see that the aggregation is being performed, but the result is not getting dumped to the destined collection (which is supposed to be done by $merge
).
Here is the code I tried to implement
const CronJob = require('cron').CronJob;
const job = new CronJob( '* * * * *', () => {
console.log('Cron Job started');
dbColl.aggregate([
-----------------
some calculations
-----------------
{ $merge: { into: { db: 'db-name', coll: 'collection-name' } } }
], (err, docs) => {
if(!err && docs){
console.log('Cron Job ended');
console.log(docs); //I'm getting the correct output for the docs
}
})
})
Result
Cron Job started
Cron Job ended
{
---docs---
}
Note: There is no issue with aggregate function as I have tested it in mongo shell. The issue happens when the aggregate function is performed in a cron job
The merge option didn't work. But I used callback function of aggregated method and updated the result in that