Mongo add timestamp field from existing date field

mongodb, timestamp

Solution

Do a find for all the documents, limiting to just the id and timeCreated fields. Then loop over that and generate the timestampCreated value, and do an update on each.

Problem

I currently have a collection with documents like the following: ``` { foo: 'bar', timeCreated: ISODate("2012-06-28T06:51:48.374Z") } ``` I would now like to add a `timestampCreated` key to the documents in this collection, to make querying by time easier. I was able to add the new column with an `update` and `$set` operation, and set the timestamp value but I appears to be setting the current timestamp using this: ``` db.reports.update({}, { $set : { timestampCreated : new Timestamp(new Date('$.timeCreated'), 0) } }, false, true); ``` I however have not been able to figure out a way to add this column and set it's value to the timestamp of the existing 'timeCreated' field.

Original source

Related problems