mongo查询中的减法不起作用?

时间:2015-01-31 18:34:48

标签: mongodb 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" : 18,
"quality" : 0.4,
"intensity" : 10,
"happiness" : 0,
"calmness" : 0,
"kindness" : 0,
"sureness" : 0,
"Hashtags" : [ 
    "harp", 
    "nknkn"
],
"authorID" : NumberLong(49067869),
"authorName" : "Fran Walker",
"authorFollowers" : 93,
"authorFollowing" : 133,
"authorFavourites" : 50,
"authorTweets" : 13667,
"authorVerified" : false,
"screenName" : "snickeringcrow",
"profileImageURL" : "http://pbs.twimg.com/profile_images/2180546952/smilinkitty.asp_-_Copy_normal.jpg",
"profileLocation" : "",
"timezone" : "Eastern Time (US & Canada)",
"gender" : "M",
"Entities" : [ 
    {
        "id" : 6,
        "name" : "Harper, Stephen",
        "frequency" : 0,
        "partyId" : 6
    }
],
"Topics" : [ 
    {
        "id" : 8,
        "name" : "Employment",
        "frequency" : 1,
        "Subtopics" : [ 
            {
                "id" : 34,
                "name" : "Economic",
                "frequency" : 1
            }
        ]
    }, 
    {
        "id" : 11,
        "name" : "Economy",
        "frequency" : 1,
        "Subtopics" : [ 
            {
                "id" : 43,
                "name" : "Economic",
                "frequency" : 1
            }
        ]
    }
   ]
  }

我正试图按日期分组并获得每组的情绪总和除以(每组-1中的项目数)。正如你所看到的那样-1我不能使用mongo的avg函数所以我必须手动完成如下:

DBCollection collectionG;
    collectionG = db.getCollection("TweetCachedCollection");
    ArrayList<EntityEpochData> results = new ArrayList<EntityEpochData>();
    List<DBObject> stages = new ArrayList<DBObject>();
    ArrayList<DBObject> andArray = null;
    DBObject groupFields = new BasicDBObject("_id", "$Added");
    groupFields.put("value",
            new BasicDBObject("$sum", "$" + sType.toLowerCase()));
    groupFields.put("count", new BasicDBObject("$sum", 1));

    DBObject groupBy = new BasicDBObject("$group", groupFields);
    stages.add(groupBy);
    DBObject project = new BasicDBObject("_id", 0);
    project.put("count", new BasicDBObject("$subtract", new Object[] {
            "$count", 1 }));
    project.put("value", new BasicDBObject("$divide", new Object[] {
            "$value", "$count" }));
    project.put("Date", "$_id");
    stages.add(new BasicDBObject("$project", project));

    DBObject sort = new BasicDBObject("$sort", new BasicDBObject("Date", 1));
    stages.add(sort);
    AggregationOutput output = collectionG.aggregate(stages);

现在一切正常,除了:

让我们说计数是3但是如果我加上它我希望计数的数字是2并且它在减法之后但是当它到达下一行时,它仍然是指数仍然是指3。

如果有更多解释,例如sum是6和count 3我想要sum /(count-1)返回2但它返回3 !!!! 所以似乎这一行返回2:

project.put("count",new BasicDBObject("$subtract", new Object[] {"$count", 1 }));

但下一行仍然将6除以3而不是2:

project.put("value", new BasicDBObject("$divide", new Object[] {
            "$value", "$count" }));

似乎最后一行中的计数仍然是指旧的值而不是更新的一个......

任何人都可以帮助我吗?

更新

我自己认为如果我先减去减法然后进行除法它会起作用但我不知道怎么做?

1 个答案:

答案 0 :(得分:3)

您需要对$project对象稍作修改。您需要使用从1中减去count时获得的对象,而不是使用之前的count值。

DBObject project = new BasicDBObject("_id", 0);
DBObject countAfterSubtraction = new BasicDBObject("$subtract", 
                                     new Object[] {"$count", 1});
DBObject value = new BasicDBObject("$divide", 
                            new Object[] {"$value",countAfterSubtraction});
project.put("value", value);
project.put("Date", "$_id");
stages.add(new BasicDBObject("$project", project));

上述代码适用于records >= 2的群组。如果只有一个记录,只有一个记录,减法后的计数将为零,导致除以零错误。

因此,您可以修改代码,包含$cond,以检查减法后的计数是0,如果是,则默认为1,否则保留减去count

的值
DBObject project = new BasicDBObject("_id", 0);
DBObject countAfterSubtraction = new BasicDBObject("$subtract", 
                                       new Object[] {"$count", 1});
DBObject eq  = new BasicDBObject("$eq",
                        new Object[]{countAfterSubtraction,0});
DBObject cond = new BasicDBObject("$cond",
                         new Object[]{eq,1,countAfterSubtraction});
DBObject value = new BasicDBObject("$divide", 
                                new Object[] {"$value",cond});
project.put("value", value);
project.put("Date", "$_id");
stages.add(new BasicDBObject("$project", project));