【发布时间】:2016-07-29 12:24:13
【问题描述】:
我使用 twitter api 'statuses/user_timeline' 将我自己的 twitter tweets 集合存储在 mongodb 中。我正在尝试获取我使用 MongoDb MapReduce 方法在我发布的推文中获得的 转推 计数,但无法获得它。谁能帮帮我。
示例数据:这是存储在 mongodb 中的文档格式
{
"_id" : ObjectId("570664d7a9c29761168b4587"),
"created_at" : "Thu Sep 17 01:17:28 +0000 2015",
"id" : NumberLong("644319222886039556"),
"id_str" : "644319222886039556",
"text" : "Be silent or let your words be worth more than you silence.",
"entities" : {
"hashtags" : [ ],
"symbols" : [ ],
"user_mentions" : [ ],
"urls" : [ ]
},
"truncated" : false,
"source" : "<a href=\"http://twitter.com\" rel=\"nofollow\">Twitter Web Client</a>",
"in_reply_to_status_id" : null,
"in_reply_to_status_id_str" : null,
"in_reply_to_user_id" : null,
"in_reply_to_user_id_str" : null,
"in_reply_to_screen_name" : null,
"user" : {
// Here is the user information who tweeted
"id" : NumberLong(xxxxxxxxxxxxxxxxx),
"id_str" : "xxxxxxxxx",
"name" : "Haridarshan Gorana",
"screen_name" : "haridarshan2901"
},
"geo" : null,
"coordinates" : null,
"place" : null,
"contributors" : null,
"is_quote_status" : false,
"retweet_count" : NumberLong(1),
"favorite_count" : NumberLong(0),
"favorited" : false,
"retweeted" : false,
"lang" : "en"
}
代码:
$map = new \MongoCode("function() { emit(this.id_str, this.retweet_count); }");
$out = "retweets";
$reduce = new \MongoCode('function(key, values) {
var retweets = 0;
for(i=0;i<values.length;i++){
if( values[i].retweet_count > 0 ){
retweets += values[i].retweet_count;
}
}
return retweets;
}');
$verbose = true;
$cmd = array(
"map" => $map,
"reduce" => $reduce,
"query" => $query,
"out" => "retweets",
"verbose" => true
);
$result = $db->command($cmd);
print_r($result);
这给了我这个错误
致命错误:在 null 上调用成员函数 command()
我尝试在 mongo 客户端上运行相同的代码
var mapFunction1 = function() {
emit(this.id_str, this.retweet_count);
}
var reduceFunction1 = function(id, values) {
var retweet = 0;
for(i=0;i<values.length;i++){
if(values[i].retweet_count > 0) {
retweet += values[i].retweet_count;
}
}
return retweet;
}
db.tweets.mapReduce(
mapFunction1,
reduceFunction1,
{
query: {
user: { id: xxxxxxxxx }
},
out: "retweets",
verbose: true
}
)
控制台输出
{
"result" : "retweets",
"timeMillis" : 12,
"timing" : {
"mapTime" : 0,
"emitLoop" : 8,
"reduceTime" : 0,
"mode" : "mixed",
"total" : 12
},
"counts" : {
"input" : 0,
"emit" : 0,
"reduce" : 0,
"output" : 0
},
"ok" : 1
}
【问题讨论】:
-
您能否在edit 您的帖子中包含一些示例文档和您的预期结果,以显示您希望通过 MapReduce 操作实现的目标?我有一种强烈的感觉,您的问题可以使用聚合框架轻松解决,并具有一些出色的性能增强。
标签: javascript mongodb mapreduce mongodb-query aggregation-framework