LoginSignup
0
1

More than 5 years have passed since last update.

MongoDBでTwitterのuser_timelineのcreated_atをISODateに変換

Last updated at Posted at 2018-11-07

Twitterのuser_timelineのcreated_atをMongoDBで利用する際に便利なように変換します。
Twitterのcreated_atの形式は"Wed Nov 07 00:03:57 +0000 2018"の形式です。
ですが、{"created_at":{$gt:"Wed Nov 06 00:00:00 +0000 2018"}}のようなクエリーは使えません。

そこで(created_atを置き換えるのではなく)created_at_isodateという要素を追加するようにします。

stackoverflowにほぼ同じようなのがあったので参考(ほぼパクリ)にさせていただきました。

var bulk = db.timeline.initializeUnorderedBulkOp();
db.timeline.find({"created_at": {"$exists": true}}).forEach(function (doc) {
    var newDate = new Date(doc.created_at);
    bulk.find({ "_id": doc._id }).updateOne({
        "$set": { "created_at_isodate": newDate}
    });
})
bulk.execute();

20件分の実行結果はこんな感じです。

BulkWriteResult({
    "writeErrors" : [ ],
    "writeConcernErrors" : [ ],
    "nInserted" : 0,
    "nUpserted" : 0,
    "nMatched" : 20,
    "nModified" : 20,
    "nRemoved" : 0,
    "upserted" : [ ]
})

これを実行すると下記のようにcreated_at_isodateができました。

> db.timeline.find({},{"created_at":1,"created_at_isodate":1})
{ "_id" : ObjectId("5be24be15790a3000640fca0"), "created_at" : "Wed Nov 07 00:03:57 +0000 2018", "created_at_isodate" : ISODate("2018-11-07T00:03:57Z") }
{ "_id" : ObjectId("5be24be15790a3000640fca1"), "created_at" : "Wed Nov 07 00:02:31 +0000 2018", "created_at_isodate" : ISODate("2018-11-07T00:02:31Z") }
{ "_id" : ObjectId("5be24be15790a3000640fca2"), "created_at" : "Tue Nov 06 23:52:47 +0000 2018", "created_at_isodate" : ISODate("2018-11-06T23:52:47Z") }
{ "_id" : ObjectId("5be24be15790a3000640fca3"), "created_at" : "Tue Nov 06 23:50:28 +0000 2018", "created_at_isodate" : ISODate("2018-11-06T23:50:28Z") }
{ "_id" : ObjectId("5be24be15790a3000640fca4"), "created_at" : "Tue Nov 06 23:46:20 +0000 2018", "created_at_isodate" : ISODate("2018-11-06T23:46:20Z") }
{ "_id" : ObjectId("5be24be15790a3000640fca5"), "created_at" : "Tue Nov 06 23:41:35 +0000 2018", "created_at_isodate" : ISODate("2018-11-06T23:41:35Z") }
{ "_id" : ObjectId("5be24be15790a3000640fca6"), "created_at" : "Tue Nov 06 23:25:11 +0000 2018", "created_at_isodate" : ISODate("2018-11-06T23:25:11Z") }
{ "_id" : ObjectId("5be24be15790a3000640fca7"), "created_at" : "Tue Nov 06 23:23:26 +0000 2018", "created_at_isodate" : ISODate("2018-11-06T23:23:26Z") }
{ "_id" : ObjectId("5be24be15790a3000640fca8"), "created_at" : "Tue Nov 06 22:29:39 +0000 2018", "created_at_isodate" : ISODate("2018-11-06T22:29:39Z") }
{ "_id" : ObjectId("5be24be15790a3000640fca9"), "created_at" : "Tue Nov 06 21:01:41 +0000 2018", "created_at_isodate" : ISODate("2018-11-06T21:01:41Z") }
{ "_id" : ObjectId("5be24be15790a3000640fcaa"), "created_at" : "Tue Nov 06 14:30:15 +0000 2018", "created_at_isodate" : ISODate("2018-11-06T14:30:15Z") }
{ "_id" : ObjectId("5be24be15790a3000640fcab"), "created_at" : "Tue Nov 06 14:03:49 +0000 2018", "created_at_isodate" : ISODate("2018-11-06T14:03:49Z") }
{ "_id" : ObjectId("5be24be15790a3000640fcac"), "created_at" : "Tue Nov 06 14:03:32 +0000 2018", "created_at_isodate" : ISODate("2018-11-06T14:03:32Z") }
{ "_id" : ObjectId("5be24be15790a3000640fcad"), "created_at" : "Tue Nov 06 14:01:58 +0000 2018", "created_at_isodate" : ISODate("2018-11-06T14:01:58Z") }
{ "_id" : ObjectId("5be24be15790a3000640fcae"), "created_at" : "Tue Nov 06 12:49:20 +0000 2018", "created_at_isodate" : ISODate("2018-11-06T12:49:20Z") }
{ "_id" : ObjectId("5be24be15790a3000640fcaf"), "created_at" : "Tue Nov 06 12:09:54 +0000 2018", "created_at_isodate" : ISODate("2018-11-06T12:09:54Z") }
{ "_id" : ObjectId("5be24be15790a3000640fcb0"), "created_at" : "Tue Nov 06 12:04:40 +0000 2018", "created_at_isodate" : ISODate("2018-11-06T12:04:40Z") }
{ "_id" : ObjectId("5be24be15790a3000640fcb1"), "created_at" : "Tue Nov 06 12:02:54 +0000 2018", "created_at_isodate" : ISODate("2018-11-06T12:02:54Z") }
{ "_id" : ObjectId("5be24be15790a3000640fcb2"), "created_at" : "Tue Nov 06 12:01:03 +0000 2018", "created_at_isodate" : ISODate("2018-11-06T12:01:03Z") }
{ "_id" : ObjectId("5be24be15790a3000640fcb3"), "created_at" : "Tue Nov 06 12:00:13 +0000 2018", "created_at_isodate" : ISODate("2018-11-06T12:00:13Z") }

これで期間指定もできそうです。

#ちなみにISODateって今後もタイムゾーンには対応しないのかな...?Aggregation機能を使えばいいというのがあるのかもしれないけど...

db.timeline.aggregate([{$project:{"created_at":1,"created_at_isodate":{
    $dateToString: {
        format: "%Y-%m-%d %H:%M:%S %z",
        timezone: "Asia/Tokyo",
        date: "$created_at_isodate"
        }
    }}}]).forEach(
        function(d) {
            print(d.created_at_isodate);
        }
    )
2018-11-07 09:03:57 +0900
2018-11-07 09:02:31 +0900
2018-11-07 08:52:47 +0900
2018-11-07 08:50:28 +0900
2018-11-07 08:46:20 +0900
2018-11-07 08:41:35 +0900
2018-11-07 08:25:11 +0900
2018-11-07 08:23:26 +0900
2018-11-07 07:29:39 +0900
2018-11-07 06:01:41 +0900
2018-11-06 23:30:15 +0900
2018-11-06 23:03:49 +0900
2018-11-06 23:03:32 +0900
2018-11-06 23:01:58 +0900
2018-11-06 21:49:20 +0900
2018-11-06 21:09:54 +0900
2018-11-06 21:04:40 +0900
2018-11-06 21:02:54 +0900
2018-11-06 21:01:03 +0900
2018-11-06 21:00:13 +0900
0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1