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

277
Views
MongoDB - Find single dates before and after today

I'm trying to write a query to find 2 individual documents dated before and after today, I need the values returned as a cursor as this is a Meteor publication (Perhaps not relevant).

I am currently using this

const today = new Date(); 
const before = db.lesson.findOne({lsn_id:kjsflf, lsn_dt:{$lte:today}}, {limit:1});
const after = db.lesson.findOne({lsn_id:kjsflf, lsn_dt:{$gte:today}}, {limit:1});

Ideally the result would be something like this:

return db.lesson.find({BEST QUERY EVER});
over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

If you are trying to combine these two queries you can try using MongoDB's $or:

    db.lesson.find({
     lsn_id: kjsflf,
     $or: [
       { lsn_dt: { $lte: today }},
       { lsn_dt: { $gte: today }}
     ]}, { limit: 2 });

This works well if there only exists one and only one document before "today" and one and only one document after "today". If there are other documents it will retrieve the first two documents it finds.

https://docs.mongodb.com/manual/reference/operator/query/or/

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!