Task Breaking that uses Javascript notation

Hey all, having an issue with the below query I create a task from, particularly in the variables I’m using to extract dates dynamically from. The query works properly, and the task I created works as it should. However, the query fails to adapt to a new month. For example, the task worked properly throughout April, but then failed on May 03 without prompting an error. Specifically, when the task failed to run in May, it had a unix timestamp for the last few days of April. Is Date().getDate() not supported for a query within a task?

Any help will be appreciated!
-Thanks Alex!


var startDate = new Date(new Date().setDate(new Date().getDate() - 7)) // Get date # of days ago
var endDate = new Date(new Date().setDate(new Date().getDate() - 0)) // Get date # of days ago


db.getCollection("Application").aggregate([
  {
    $match: {
      createdDateTime: { $gte: startDate, $lt: endDate },

    },
  },
  {
    $project: {
      createdDateTime: { $ifNull: ["$createdDateTime", ""] },
      Application_timestamp: { $ifNull: ["$applicationTimeStamp", ""] },


 
      
   
    },
  },
]);
1 Like

Is this an IntelliShell script task? If so, there should not be anything preventing you from using that syntax.

It might be worth it to pass the log.txt file to our support team (it contains MongoDB server hostnames so it should probably not be uploaded here), maybe that’ll tell us what went wrong there. :slight_smile:

1 Like

hey Rico, I ran the above script using intellishell, then exported via the gui to csv, which I made a task. I ran a few more test cases and it appears that the time stamp for the dynamic date variables is the same time stamp when the task is created, so studio 3t isn’t re-processing the script each time it tries to export. Anyone know any work arounds or other solutions to dynamically export csv results from a query?

thanks for the support- Alex

Indeed, when you save this as a normal export task, you unfortunately lose the ability of calculating timestamps like this, as we run the export task using the MongoDB Java driver which, by itself, can’t parse JS.

There are some possible solutions for this:

  1. Instead of calculating the dates using the JS syntax above, you could switch to date tags, which work in the Collection and Aggregation tab as well as in Export tasks. This is IMO the easiest way to deal with timestamps like “within the last 7 days” and is probably the best solution. :slight_smile:
  2. You could save this as an IntelliShell Script task, but then you’d have to write your own CSV serializer. Not the best solution, but would be feasible if 1. doesn’t work out for you.
  3. I strongly advise against this but want to list it for completeness’ sake: You could make the MongoDB server execute the date calculation. There are some operators for this, like $where or $function (new in version 4.4) within $expr, but these can decrease the query performance, as the JavaScript code is executed for each document that is inspected. Keep this in mind and only go with this if neither number 1 or 2 work (but really, at least one of them should :smiley:)
1 Like