1

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?

HMdeveloper
  • 2,772
  • 9
  • 45
  • 74

1 Answers1

1

Change your relevant project object to exclude _id and project the _id field as Date field.

  DBObject project = new BasicDBObject("_id",0);
  project.put("Date","$_id");
  project.put("value",1);

When you do,

 DBObject project = new BasicDBObject("_id",0);
 project.put("Date",1);
 project.put("value",1);
 project.put("Type",1);

project.put("Date",1), makes no effect because the documents that enter the $project stage from the $group stage do not have a Date field, but they have the dates selected in their _id fields.

project.put("Type",1) is invalid because, the original documents have the Type field but not the documents that enter the $project stage once they are grouped.

BatScream
  • 19,260
  • 4
  • 52
  • 68
  • Thanks you solved my problem just a quick question : since Date is dateformat in mongo it is returned like this: "Date" : { "$date" : "2014-10-19T04:00:00.000Z"} and when I try to get the result in java with this code : for (DBObject result : output.results()) { System.out.println(result.get("Date").toString()); I get the date converted like this :Sun Oct 19 01:00:00 ADT 2014 is there any way that I can get it like the original one(2014-10-19T04:00:00.000Z)? – HMdeveloper Jan 06 '15 at 19:44
  • @HamedMinaee I could answer this as part of this comment but it is a bit broad. Sorry for that. If i include that in my answer, it just becomes out of context from the original question. Can you please ask a new question with that. It would be helpful to others too who face the same issue. – BatScream Jan 06 '15 at 19:47
  • 1
    @HamedMinaee - But you could use a date formatting API, like `simpledateformat` to achieve that. – BatScream Jan 06 '15 at 19:49
  • Sure actually the original question is here can you please take a look: :http://stackoverflow.com/questions/27803876/converting-the-date-returned-from-mongo-to-this-format-2015-10-25/27804007?noredirect=1#comment44017874_27804007 – HMdeveloper Jan 06 '15 at 19:49
  • Hello again BatScream , there is a question here which I am really stuck at, Can you please take a look : http://stackoverflow.com/questions/28255100/subtraction-in-mongo-query-does-not-work – HMdeveloper Jan 31 '15 at 19:43