I have a collection in mongo as follow:
{
"_id" : ObjectId("5490b272f315dce7077204af"),
"Date" : ISODate("2014-10-19T04:00:00.000Z"),
"Type" : "Twitter",
"Entities" : [
{
"ID" : 2,
"Name" : "test1",
"Sentiment" : {
"Value" : 19,
"Neutral" : 1
},
"Quality" : {
"Value" : 0.1,
"Low" : 1
},
"Intensity" : {
"Value" : 0,
"Low" : 1
},
"Happiness" : {
"Value" : 0.5,
"Medium" : 1
}
},
{
"ID" : 4,
"Name" : "test1",
"Sentiment" : {
"Value" : 10,
"Neutral" : 1
},
"Quality" : {
"Value" : 0.1,
"Low" : 1
},
"Intensity" : {
"Value" : 0,
"Low" : 1
},
"Happiness" : {
"Value" : 0.5,
"Medium" : 1
}
}
]
}
Now I want to group by everything on Date and get the sum of Sentiment.Value and I have a java code as follow which works perfectly fine :
ArrayList<DBObject> andArray = andArrayEntityIdsEqualAndDateBetweenGraph(entityIds, startDate, endDate);
DBObject where = new BasicDBObject("$match", new BasicDBObject("$and", andArray));
DBObject unwind = new BasicDBObject("$unwind", "$Entities"); // "$unwind" converts object with array into many duplicate objects, each with one from array
collectionG = db.getCollection("GraphDataCollection");
DBObject groupFields = new BasicDBObject( "_id", "$Date");
groupFields.put("value", new BasicDBObject( "$sum", "$Entities.Sentiment.Value"));
DBObject groupBy = new BasicDBObject("$group", groupFields );
DBObject sort = new BasicDBObject("$sort", new BasicDBObject("_id", 1));
stages.add(where);
stages.add(unwind);
stages.add(groupBy);
stages.add(sort);
AggregationOutput output = collectionG.aggregate(stages);
System.out.println(output.results());
And the result is as follow:
[
{
"_id": {
"$date": "2014-10-19T04:00:00.000Z"
},
"value": 29
},
{
"_id": {
"$date": "2014-10-20T04:00:00.000Z"
},
"value": 20
},
{
"_id": {
"$date": "2014-10-21T04:00:00.000Z"
},
"value": 21
}
]
Now what I want is to hide _id and show just Date and value so I changed my code to the following :
DBObject where = new BasicDBObject("$match", new BasicDBObject("$and", andArray));
DBObject unwind = new BasicDBObject("$unwind", "$Entities"); // "$unwind" converts object with array into many duplicate objects, each with one from array
collectionG = db.getCollection("GraphDataCollection");
DBObject groupFields = new BasicDBObject( "_id", "$Date");
groupFields.put("value", new BasicDBObject( "$sum", "$Entities.Sentiment.Value"));
DBObject groupBy = new BasicDBObject("$group", groupFields );
DBObject sort = new BasicDBObject("$sort", new BasicDBObject("_id", 1));
stages.add(where);
stages.add(unwind);
stages.add(groupBy);
DBObject project = new BasicDBObject("_id",0);
project.put("Date",1);
project.put("value",1);
project.put("Type",1);
stages.add(new BasicDBObject("$project",project));
stages.add(sort);
AggregationOutput output = collectionG.aggregate(stages);
System.out.println(output.results());
Now I expect to have _id hidden but value and Date visible but I do not know why I get the following result instead:
[
{
"value": 29
},
{
"value": 21
},
{
"value": 20
}
]
Can anyone help?