LoginSignup
1
1

More than 5 years have passed since last update.

CasbahでJodaTimeをMongoDBに保存する

Posted at

Mongoでの日時はUTCで扱われる。

以下の2つを呼んでおく
import com.mongodb.casbah.commons.conversions.scala._
RegisterJodaTimeConversionHelpers()
保存されたドキュメントはこんな感じ
{
    "date": "20140227",
    "date_time": ISODate("2014-02-26T15: 00: 05Z"),
    "time": "00:00:05"
}
問い合わせ
val dtf = DateTimeFormat.forPattern("yyyyMMdd hh:mm:ss").withZone(DateTimeZone.forID("Asia/Tokyo"))
val fromTime = DateTime.parse("20140226 090000", dtf)
val toTime = DateTime.parse("20140228 0900000", dtf)
val query = $and("date_time" $gte fromTime, "date_time" $lte toTime))
クエリ
{
    "$and": [
        {
            "date_time": {
                "$gte": {
                    "$date": "2014-02-26T00:00:00.000Z"
                }
            }
        },
        {
            "date_time": {
                "$lte": {
                    "$date": "2014-02-28T00:00:00.000Z"
                }
            }
        }
    ]
}
1
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
1
1