Reputation: 5531
I have a collection called posts, initially they only had "text" and "author" field. Later I added a "date" field for later posts.
How I can add a date field for initial posts, for those who does not have date/created-at field.
I guess I need to use following two commands, but cant figure out how to make the connection (i.e. loop)?
db.posts.find({"date":{"$exists":false}}
ObjectId(_id).getTimestamp()
Upvotes: 1
Views: 56
Reputation: 4114
db.posts
.find({"date":{"$exists":false}) // looking for records without `date` field
.forEach(function (record) { // iterating through each record
record.date = null; // if you want to define current date use `new Date()`
db.posts.save(record); // saving updated record
});
Upvotes: 3