0

datetime is my user-defined field where i m storing in isodate format. I want add a timestampCreated key to the documents in this collection. I am trying this but its giving error as "error:timestamp must be in number".

var cursor = db.getCollection('test').find();
while (cursor.hasNext()) { 
  var doc = cursor.next(); 
  db.getCollection('test').update({_id : doc._id}, {$set : 
{'timestampCreated' : new Timestamp(new Date('datetime'), 0) }});
}
Gagan Deep
  • 89
  • 1
  • 10
  • `'datetime'` is a "string". You want `new Date(doc.datetime)` and you also don't want the `TimeStamp()` since that has a different purpose. Just use the `Date`. – Neil Lunn May 10 '18 at 05:40
  • datetime is user-defined field and its in isodate format – Gagan Deep May 10 '18 at 06:11
  • I know. But you are passing in a "string" to the `Date` constructor instead of a "value" from the document itself. Hence my correction as is also shown on the linked questions and answers. – Neil Lunn May 10 '18 at 06:14

0 Answers0