Mongodb: add a date field/column to documents which do not have a date field -


i have collection called posts, had "text" , "author" field. later added "date" field later posts.

how can add date field initial posts, not have date/created-at field.

i guess need use following 2 commands, cant figure out how make connection (i.e. loop)?

db.posts.find({"date":{"$exists":false}} objectid(_id).gettimestamp() 

db.posts   .find({"date":{"$exists":false}) // looking records without `date` field   .foreach(function (record) { // iterating through each record       record.date = null; // if want define current date use `new date()`       db.posts.save(record); // saving updated record   }); 

Comments