Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

129
Views
How to automatically add timestamps eg. createdAt/updatedAt in MongoDB nodejs driver

I'm working on a small application and I need to automatically add a timestamp of creating/updating my documents in the default Nodejs MongoDb driver. Right now, when I add a new document, it only has added the _id field. Is there a way to force the addition of the createdAt/updatedAt fields?

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

You can add createdAt manually when creating the object this way:

        await this.collection.insertOne({ businessAuth: _id, url, labels, createdAt: new Date()});

And updatedAT when updating the object this way:

video.updatedAt = new Date();
const saved = await this.collection.findOneAndUpdate({_id: video._id}, {$set: {...video}});
about 4 years ago · Juan Pablo Isaza Report

0

Drivers do not generally provide this functionality. ODMs do. In case of Node, mongoose is an ODM which does.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!