【问题标题】:group by does not work properlygroup by 无法正常工作
【发布时间】:2015-01-21 17:13:22
【问题描述】:

我有一个这样的收藏:

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

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

【问题讨论】:

    标签: mongodb-query mongodb-java


    【解决方案1】:

    你错过了这部分:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-18
      • 1970-01-01
      • 2021-04-06
      • 2014-06-18
      • 1970-01-01
      相关资源
      最近更新 更多