group by 无法正常工作

group by does not work properly

我有一个 collection 这样的:

{
"_id" : ObjectId("5491d65bf315c2726a19ffe0"),
"tweetID" : NumberLong(535063274220687360),
"tweetText" : "19 RT Toronto @SunNewsNetwork: WATCH: When it comes to taxes, regulations, and economic freedom, is Canada more \"American\" than America? http://t.co/D?",
"retweetCount" : 1,
"source" : "<a href=\"http://twitter.com\" rel=\"nofollow\">Twitter Web Client</a>",
"Date" : ISODate("2014-11-19T04:00:00.000Z"),
"Added" : ISODate("2014-11-19T04:00:00.000Z"),
"tweetLat" : 0,
"tweetLon" : 0,
"url" : "http://t.co/DH0xj0YBwD ",
"sentiment" : 26,
"quality" : 0.3373574014694046,
"intensity" : 10,
"happiness" : 0,
"Entities" : [ 
    {
        "id" : 4,
        "name" : "Harper, Stephen",
        "frequency" : 0
    }
]
}

我在 java 中有以下代码:

DBCollection collectionG;
    collectionG = db.getCollection("TweetCachedCollection");
    ArrayList<EntityEpochData> results = new ArrayList<EntityEpochData>();
    List<DBObject> stages = new ArrayList<DBObject>();
    DBObject groupFields = new BasicDBObject("_id", "$Added");
    groupFields.put("value", new BasicDBObject("$avg", "$sentiment"));
    stages.add(groupFields);
    DBObject project = new BasicDBObject("_id", 0);
    project.put("value", 1);
    stages.add(new BasicDBObject("$project", project));
    DBObject sort = new BasicDBObject("$sort",
            new BasicDBObject("Added", 1));
    stages.add(sort);
    AggregationOutput output = collectionG.aggregate(stages);
    System.out.println(output.results());

但是当我 运行 这段代码时,我得到以下错误:

"errmsg" : "exception: A pipeline stage specification object must contain exactly one field." , "code" : 16435 

但是我按照正确的顺序来构建我的管道...我很困惑!!! 有人可以帮忙吗?

你错过了这部分:

DBObject groupBy = new BasicDBObject("$group", groupFields );