根据 created_at 字段排序推文
Order tweet according to created_at field
当使用 Twitter Stream API 检索推文时,每条推文与其
一起下载
created_at
字段,格式中的String
字段,例如
Mon Sep 01 00:00:00 +0000 2014
我将这些推文存储在 MongoDB 数据库中。现在我想 根据推文的日期对 推文进行排序,但是如果我要求 MongoDB 根据 created_at
字段(一个字符串)对推文进行排序:
db.collection.find({},{created_at:1}).sort({created_at:1})
日期将按照字典顺序排列,这不是我想要的。
如何修改查询以便根据日期而不是字符串对它们进行排序?我尝试使用 ISODate
个对象,但由于该字段是字符串,所以这不起作用。
谢谢。
通过使用 MapReduce:
db.rawTweets.mapReduce(
// map
function() {
emit(
// "Thu Jul 17 03:21:42 +0000 2014"
new Date(Date.parse(this.created_at.replace(/(\+\S+) (.*)/, ' '))).toLocaleDateString(),
1
);
},
// reduce
function(key, values) {
return Array.sum(values)
},
{
query: {},
out: "rawTweetsCount"
}
)
当使用 Twitter Stream API 检索推文时,每条推文与其
一起下载created_at
字段,格式中的String
字段,例如
Mon Sep 01 00:00:00 +0000 2014
我将这些推文存储在 MongoDB 数据库中。现在我想 根据推文的日期对 推文进行排序,但是如果我要求 MongoDB 根据 created_at
字段(一个字符串)对推文进行排序:
db.collection.find({},{created_at:1}).sort({created_at:1})
日期将按照字典顺序排列,这不是我想要的。
如何修改查询以便根据日期而不是字符串对它们进行排序?我尝试使用 ISODate
个对象,但由于该字段是字符串,所以这不起作用。
谢谢。
通过使用 MapReduce:
db.rawTweets.mapReduce(
// map
function() {
emit(
// "Thu Jul 17 03:21:42 +0000 2014"
new Date(Date.parse(this.created_at.replace(/(\+\S+) (.*)/, ' '))).toLocaleDateString(),
1
);
},
// reduce
function(key, values) {
return Array.sum(values)
},
{
query: {},
out: "rawTweetsCount"
}
)