Mongodb 使用reactivemongo将新日期投影到聚合管道中

Mongodb 使用reactivemongo将新日期投影到聚合管道中,mongodb,scala,reactivemongo,Mongodb,Scala,Reactivemongo,我正试图在reactivemongo聚合管道内的投影中创建一个新的日期值 我见过其他一些例子,人们在mongo shell中创建了这样的功能,比如: db.runCommand({ "aggregate" : "collectionName", "pipeline": [ { $project: { exampleDate: new Date() } } ] }) 问题在于新日期()。使用reactivemongo,投影将是一个项目对象: Project( ("

我正试图在reactivemongo聚合管道内的投影中创建一个新的日期值

我见过其他一些例子,人们在mongo shell中创建了这样的功能,比如:

db.runCommand({
    "aggregate" : "collectionName", "pipeline": [
       { $project: { exampleDate: new Date() } } 
    ]
})
问题在于
新日期()
。使用reactivemongo,投影将是一个项目对象:

Project( ("exampleDate", BSONValue) )
其中,BSONValue可以是BSONString。但这会导致mongoDB忽略该字符串,因为结果将是:

{ "aggregate" : "collectionName", "pipeline": [ { $project: { exampleDate: "new Date()" } } ] }
有没有办法插入不带引号的
新日期()

注意:我也尝试过使用
BSONDateTime(0)
但这导致:

{ $date: 0 }

这使得mongoDB抛出一个无效的
$date
操作符异常。

由于
java.util.date
类的默认构造函数给出了调用构造函数的时间(也称为“now”),因此您似乎有兴趣使用相同的“now”时间构造
BSONDateTime
BSONDateTime
的参数应为
Long
值,该值包含UTC时间(以毫秒为单位)。您可以从
java.lang.System.currentTimeMillis
或从
java.util.Date.getTime
方法中获得。所以,我想你想要的是:

Project( ("exampleDate", BSONDateTime(System.currentTimeMillis()) )


不幸的是,这不起作用,因为mongo抛出了一个无效的
$date
操作符异常。您需要共享生成该异常的确切代码,以便进一步诊断。你能把这个问题简化成一个小项目并放到GitHub上吗?如果是这样的话,我也许能找出问题所在。@bILK你找到解决办法了吗,在投影阶段添加新的Date()
val now = new java.util.Date()
Project( ("exampleDate", BSONDateTime(now.getTime) )