$ project无法正常工作

时间:2015-01-06 18:56:15

标签: mongodb mongodb-query mongodb-java

我在mongo有一个集合如下:

{
"_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
        }
    }
]

}

现在我想按日期对所有内容进行分组并得到Sentiment.Value的总和,我有一个java代码,如下所示:

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());

结果如下:

[
{
    "_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
}

现在我想要的是隐藏_id并显示日期和值,所以我将代码更改为以下内容:

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());

现在我希望_id隐藏但值和日期可见,但我不知道为什么我得到以下结果:

[
{
    "value": 29
},
{
    "value": 21
},
{
    "value": 20
}

有人可以帮忙吗?

1 个答案:

答案 0 :(得分:1)

更改相关的project对象以排除_id,并将_id字段标记为Date字段。

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

当你这样做时,

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

project.put("Date",1)无效,因为从$project阶段进入$group阶段的文档没有Date字段,但是他们选择了日期他们的_id字段。

project.put("Type",1)无效,原因是原始文档包含Type字段,但在归档后不会进入$project阶段的文档。