douyujun0152 2018-12-13 17:05
浏览 474
已采纳

在MongoDB中保存纳秒级时间戳(由Go生成,使用Node.js保存)

I am trying to store Unix timestamps in nanoseconds into the Date type field of a Mongodb database.

The unix timestamps in nanoseconds are generated in a Go program (with time.Now().UnixNano()), submitted to Kafa to be read by a Node.js script which uses mongoose to write the payload object containing the unix timestamp in nanoseconds to the Mongodb store.

However this is throwing an error

UnhandledPromiseRejectionWarning: ValidationError: trade validation failed: timestamp: Cast to Date failed for value "1544720051987010000" at path "timestamp"

Is there a better way to store unix timestamps in nanosecond in Mongodb?

  • 写回答

1条回答 默认 最新

  • dongzong8110 2018-12-13 17:45
    关注

    I am trying to store Unix timestamps in nanoseconds into the Date type field of a Mongodb database. Nyxynyx

    That is not going to work.


    Internally, Date objects are stored as a signed 64-bit integer representing the number of milliseconds since the Unix epoch (Jan 1, 1970).

    MongoDB Reference: Date()


    For example,

    package main
    
    import (
        "fmt"
        "time"
    )
    
    func main() {
        timestamp := time.Now().UnixNano() / int64(time.Millisecond)
        fmt.Println(timestamp)
    }
    

    Playground: https://play.golang.org/p/21S_DeVA4jv

    Output:

    1257894000000
    

    To store Unix nanoseconds, use the long data type.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用