如何获取要存储为MongoDB中而不是Int64上的日期的日期? [英] How do I get a Date to store as a Date in MongoDB instead of an Int64?

查看:76
本文介绍了如何获取要存储为MongoDB中而不是Int64上的日期的日期?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用新的MongoDB Scala驱动程序.当我存储java.util.Date时,它将作为Int64而不是MongoDB日期存储在MongoDB中.我有一些看起来像这样的代码:

I'm using the new MongoDB Scala Driver. When I store a java.util.Date, it is being stored in MongoDB as an Int64 instead of a MongoDB Date. I have some code that looks like this:

implicit val writer = new Writes[Forecast] {
  def writes(x: Forecast): JsValue = {
    Json.obj(
      // ...
      "issueDateTime" -> x.issueDateTime // which is a java.util.Date
      // ...
    )
  }
}

,但最终在MongoDB中使用的是Int64,而不是日期.如何获取日期到MongoDB中?

but what ends up in MongoDB is an Int64, not a Date. How do I get a Date into MongoDB?

推荐答案

如果您使用的是最新的

If you are using the latest MongoDB Scala Driver v1.1. Instead of using the Json.obj to build your document, try using Document class.

java.util.Date 例如:

val newdate = new Date()
val doc: Document = Document("test" -> newdate)
collection.insertOne(doc).results()

将导致:

{  "_id" : ObjectId("56665bf619a63d9e538b2851"), 
    "test" : ISODate("2015-12-08T04:26:29.999Z") 
}

希望有帮助.

这篇关于如何获取要存储为MongoDB中而不是Int64上的日期的日期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆