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

415
Views
Passing current time to Mongoose query

I've run into problem. I made field in my Mongoose schema with type "Date":

...
timeOfPassingQuestion: Date,
...

Now, I want to pass current time in hours, minutes, seconds and miliseconds and save it into that field. How should I format my Node variable so I can pass it without errors?

Edit: Also, I forgot to say that I wanna later see how much time user spent answering question by subtracting current time and time that I pulled from DB, timeOfPassingQuestion field.

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

This is the syntax to create a schema that supports a date field:

// Schema
{ //...
  someDate: Date,
}

// date object that you can use whenever you decide to set it
var dateObj = new Date();

This will create a JavaScript date object that you can then pass into your Mongoose object for the date field.

Or, if you will always want it on creation, put it directly in your mongoose schema

{  //...
   createdDate: { type: Date, default: Date.now },
}

In order to compare the time in the future, I suggest you use moment.js, then you can query the time difference like so:

moment(Model.createdDate).fromNow();

Sources:

Mongoose Schema

Moment.js fromNow

over 4 years ago · Santiago Trujillo 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!