3

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!

Community
  • 1
  • 1
  • 1
    possible duplicate of [Aggregate MongoDB results by ObjectId date](http://stackoverflow.com/questions/18691689/aggregate-mongodb-results-by-objectid-date) – chridam Mar 30 '15 at 09:15
  • 1
    this is not a duplicate question. Mine is about he aggregation framework and the one @chridam posted is about map reduce in mongo – user1493741 Mar 30 '15 at 19:26
  • 1
    It is a duplicate question, if you read the context the OP asked _I'd like to see how I can do it using MongoDB's aggregation framework (or even MapReduce)_ and the answers provided in that question still apply to this one as well: _There is no way to accomplish what you're asking with mongodb's aggregation framework, because there is no aggregation operator that can turn ObjectId's into something date-like (there is a [JIRA ticket](https://jira.mongodb.org/browse/SERVER-9406), though). You should be able to accomplish what you want using map-reduce, however_ – chridam Mar 30 '15 at 19:31

1 Answers1

4

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

db.movies.aggregate([
    {
        "$project":
        {
            "date": { $toDate: "$_id" }
        }
    }
])