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

93
Views
Mongoose convert field before find

In my mongodb database I have a collection named Bookings in the model there is a field named periodFrom (representing a date in a string) which is of type String.

I need to find the documents in this collection which have a periodFrom greater than today's date. I tried using virtuals but it did not work.

Here is an example of a few documents in my Bookings collection :

{"name": "A", "periodFrom": "29.04.2022 10:00", "place": "Tokyo"},
{"name": "B", "periodFrom": "30.06.2022 10:00", "place": "Paris"}  
{"name": "C", "periodFrom": "28.04.2022 10:00", "place": "Berlin"},
{"name": "D", "periodFrom": "01.07.2022 10:00", "place": "London"}

My bookingSchema :

const bookingSchema = new Schema({
name: {type: String}, 
periodFrom: {type: String},
place: {type: String}
})

I tried virtuals by adding at the end of my model booking.js

bookingSchema.virtual('periodFromDate').get(function () {
    return new Date(this.periodFrom.split(' ')[0].split('.')[2].toString() + "-" + this.periodFrom.split(' ')[0].split('.')[1].toString() + "-" + this.periodFrom.split(' ')[0].split('.')[0].toString())

const Booking = mongoose.model("Booking", bookingSchema);
});

I tried the request by doing the following :

let allCrew = await Booking.find({ periodFromDate: { $gte: Date.now() } })

I also tried this way but it was not working either:

let allCrew = await Booking.find({ periodFrom: { $gte: '2022.07.01 00:00' } })

So any ideas how I could find the documents that have a periodFrom greater than today's date if periodFrom is a string of a date in mongoose?

about 4 years ago · Juan Pablo Isaza
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!