Create a new date in the aggregation pipeline, reusing

I am trying to create a new Date value in a projection in a composite pipeline reaction.

I saw other examples when people create this in the mongo shell, for example:

db.runCommand({
    "aggregate" : "collectionName", "pipeline": [
       { $project: { exampleDate: new Date() } } 
    ]
})

The challenge lies in new Date(). With the reaction to the projection will be the project object:

Project( ("exampleDate", BSONValue) )

where BSONValue may be BSONString. But this leads to the fact that mongoDB ignores such a line, as the result will be:

{ "aggregate" : "collectionName", "pipeline": [ { $project: { exampleDate: "new Date()" } } ] }

Is there any way to paste new Date()without quotes?

PS: I also tried using BSONDateTime(0) But this leads to:

{ $date: 0 }

What makes mongoDB raise an invalid statement exception $date.

+4
source share
1 answer

java.util.Date ( "" ), , BSONDateTime "" . BSONDateTime Long, UTC . java.lang.System.currentTimeMillis java.util.Date.getTime. , , :

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

val now = new java.util.Date()
Project( ("exampleDate", BSONDateTime(now.getTime) )
0

Source: https://habr.com/ru/post/1568419/


All Articles