我有一個集合如下:在mongo查詢中減法不起作用?
{
"_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我不能使用蒙戈的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並且計數爲3我想sum /(count-1)返回2,但它返回3 !!!! 如此看來,這行返回2:
project.put("count",new BasicDBObject("$subtract", new Object[] {"$count", 1 }));
但下一行還通過3個而不是2分6:
project.put("value", new BasicDBObject("$divide", new Object[] {
"$value", "$count" }));
看來,在最後一行數仍指舊值的計數,而不是更新一個...
任何人都可以幫助我嗎?
更新:
我自己想,如果我的舞臺減法,然後再去做工作分工,但我不知道該怎麼辦呢?
您是MongoDB的大師:) – 2015-02-02 01:35:25
是,蝙蝠是一個最好成績;) – Disposer 2015-02-02 21:05:28
@Disposer - 請不要爲難我的朋友:) ..幸福向你學習了。 – BatScream 2015-02-03 06:24:10