mongodb - Haskell,mongodb,日期

标签 mongodb haskell bson

我如何从 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/

相关文章:

haskell - Eq 和 Ord 实例不一致?

haskell - 如何理解Haskell的ST monad中的状态类型

mongodb - 在生产 MongoDB 上运行 db.stats() 时担心无效的 BSONObj 错误

c# - 如何将 Observable Collection 上的空数据解析为数据网格绑定(bind)?

java - Spring Reactive 与 MongoDB 因超过 1000 条记录而失败

javascript - 类型错误 : 'undefined' is not a function when using mongoDB sort operator

MongoDB geoNear 命令结果距离(以千米为单位)

node.js - ES6 - 如何导入 connect-mongo( session )?

haskell - 如何在 Haskell 中正确调用 GI.GtkscrolledWindowNew?

json - Bash - 将 MONGODB BSON 转换为 JSON