I have a MongoDb
database with around 1000 records, I had the timestamp
option set to false
and none of them has the createdAt
and updatedAt
attributes. Now i’ve set the timestamp
to true
and I want to update all the objects with createdAt
timestamp
to match a value within the object that is under date
attribute, is that possible?
For example:
Review.findMany({}, $set: { createdAt: '<OBJECT>.date' }});
I want to update the createdAt
with the value of the same object in date
field.
2
Answers
Found the solution
So what I wanted to achieve in the first place was just to add
createdAt
attribute to all documents. The reason I want to do this manually is because I forgot to add thetimestamp
key to the Scheme, and already have around 1000 records in database.This is what I did:
Created a new attribute in my object's Scehma called
createdDate
. (JavaScript)Iterating all documents and updating the
createdDate
to the date (After conversion fromdate
).Connecting to Database using
Mongo Shell
(Assisted by this link).Navigating to the collection I want to edit.
Renaming
createdDate
tocreatedAt
with this command:Removing
createdDate
from Schema.Hope it helps someone!
Try this one: