Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mongoid to date in aggregation framework

I am creating a projection and am trying to figure out how to convert a mongoid to a timestamp IN THE projection. It is apparent that we can use .getTimestamp() to just get the timestamp of a mongoid, but I don't know how to make a new field in a projection. I tried this and it fails.

db.collection.aggregate( 
[
        {$unwind: 
            "$arrayToUnwind"
        },
        {$project: 
            {
                timeOfId: "$_id".getTimeStamp()
            }
        },
]

)

Any help please?! I want to make an ISO timestamp out of this projection.

Thanks!

like image 394
user1493741 Avatar asked May 20 '26 09:05

user1493741


1 Answers

https://docs.mongodb.com/manual/reference/operator/aggregation/toDate/

db.movies.aggregate([
    {
        "$project":
        {
            "date": { $toDate: "$_id" }
        }
    }
])
like image 176
Ihor Klymiuk Avatar answered May 23 '26 09:05

Ihor Klymiuk