Arun N
Arun N

Reputation: 11

MongoDB --Aggregate the record count based on last 30 days

Hi I am trying to get the aggregated count for the last 30 days of records.

To achieve this I am using the below $match condition in Mongo aggregation pipeline

$match
{
"stageStartDate":{$gt: [new Date(ISODate().getTime() - 1000*60*60*24*30)]}
}

Error on Execution: Invalid Date Format at ISODate()

Please help me in solving this. I can't use Javascript, as I am directly calling this query in Jaspersoft reports.

Upvotes: 1

Views: 4322

Answers (1)

Andriy Simonov
Andriy Simonov

Reputation: 1288

This should work, just create a new date object from milliseconds difference

db.collection.aggregate([{$match: {stageStartDate: {$gt: new Date(new Date(ISODate().getTime() - 1000*60*60*24*30))}}}]);

Upvotes: 2

Related Questions