I want to execute the following mongodb query:
db.clicks.aggregate([
{ $project: { mongoTimestamp: { $add: [new Date(0), "$createdAt"] } } },
{
$project: {
month_clicked: { $month: "$mongoTimestamp" },
year_clicked: { $year: "$mongoTimestamp" }
}
},
{
$group: {
_id: { year_clicked: "$year_clicked", month_clicked: "$month_clicked" },
clickCount: { $sum: 1 }
}
}
]);
However I can't seem to figure out how to pass "new Date(0)" as mongo expression from Spring Rest code. I have tried the following code :
AggregationExpression sumExp = new AggregationExpression() {
@Override
public org.bson.Document toDocument(AggregationOperationContext context) {
BasicDBObject bdbo = new BasicDBObject();
bdbo.append("$add", Arrays.<Object> asList("new Date(0)", "$createdAt"));
return new org.bson.Document("mongoTimestamp", bdbo);
}
};
But the new Date(0) portion is sent over as string "new Date(0)" vs plain new Date(0)
I am using Mongo 3.2.5 and can't upgrade to Mongo 4.