group by无法正常工作

时间:2015-01-21 17:13:22

标签: mongodb-query mongodb-java

我有一个这样的集合:

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

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

1 个答案:

答案 0 :(得分:0)

你错过了这一部分:

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