I am using MongoDB 3.2 and I am having requirement of grouping the document by timstamp as quarterly and half-yearly. The document structure is like
{
"_id" : ObjectId("59312c59bf501118aea587b2"),
"timestamp" : ISODate("2012-01-01T01:00:00Z"),
"value" : 20,
"uniqueId" : ObjectId("59312c59bf501118aea58a6d")
},
{
"_id" : ObjectId("59312c59bf501118aea587b3"),
"timestamp" : ISODate("2012-02-01T01:00:00Z"),
"value" : 20,
"uniqueId" : ObjectId("59312c59bf501118aea58a6d")
},
{
"_id" : ObjectId("59312c59bf501118aea587b4"),
"timestamp" : ISODate("2012-05-01T01:00:00Z"),
"value" : 20,
"uniqueId" : ObjectId("59312c59bf501118aea58a6d")
},
{
"_id" : ObjectId("59312c59bf501118aea587b5"),
"timestamp" : ISODate("2012-06-01T01:00:00Z"),
"value" : 20,
"uniqueId" : ObjectId("59312c59bf501118aea58a6d")
}
I need to group the document by timestamp quarterly or half yearly and I need to sum the value . The result for quarterly should looks like below
{
"timestamp" : ISODate("2012-01-01T01:00:00Z"),
"value" : 40
},
{
"timestamp" : ISODate("2012-05-01T01:00:00Z"),
"value" : 90
}
Can any help how can I achieve this and also for the half-yearly.