我如何从 haskell 向 mongodb 中插入或自动插入日期?从 mongo 日期类型转换为 haskell 数据类型的最佳方法是什么?
比如说,在我插入博客帖子记录(任何 haskell 网络框架)并且我想自动为每条记录添加日期戳的情况下。我该怎么办?问题更多是关于在 haskell 驱动程序中创建类型转换和 mongodb 日期类型。
最佳答案
您必须创建一个 bson 文档并将其存储在 mongodb 中。 最优雅的方法是为您自己的类型创建一个转换作为 bson 的实例。
来自sources :
instance Val UTCTime where
val = UTC
cast' (UTC x) = Just x
cast' _ = Nothing
instance Val POSIXTime where
val = UTC . posixSecondsToUTCTime . roundTo (1/1000)
cast' (UTC x) = Just (utcTimeToPOSIXSeconds x)
cast' _ = Nothing
(或者使用 timestamp::ObjectId -> UTCTime 免费获取 timestamo)
关于mongodb - Haskell,mongodb,日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11237307/