如何获取日期以 MongoDB 中的日期而不是 Int64 形式存储?
How do I get a Date to store as a Date in MongoDB instead of an Int64?
我正在使用新的 MongoDB Scala 驱动程序。当我存储 java.util.Date 时,它作为 Int64 而不是 MongoDB 日期存储在 MongoDB 中。我有一些代码如下所示:
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?
如果您使用的是最新的MongoDB Scala Driver v1.1。
不要使用 Json.obj 来构建文档,请尝试使用 Document class。
BsonTransformer will transform java.util.Date to BsonDateTime
例如:
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 Scala 驱动程序。当我存储 java.util.Date 时,它作为 Int64 而不是 MongoDB 日期存储在 MongoDB 中。我有一些代码如下所示:
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?
如果您使用的是最新的MongoDB Scala Driver v1.1。 不要使用 Json.obj 来构建文档,请尝试使用 Document class。
BsonTransformer will transform java.util.Date to BsonDateTime
例如:
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")
}
希望对您有所帮助。