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

130
Views
check date existence on array of string

This is my dataset from MongoDB Collection:


{
  "_id": "60c0ace96e93993880efd337",
  "lv": [
   uid: 1,
   "createdate": "2021-12-15T12:30:01.935Z",
   "updatedAt": [
      "2021-12-15T12:31:11.635Z",
      "2021-12-15T12:31:51.955Z",
      "2021-12-16T12:30:01.935Z",
      "2021-12-16T12:30:01.935Z",
      "2021-12-17T12:30:01.935Z",
      "2021-12-18T12:30:01.935Z"
   ]
  ]
},
{
...
}

I want to filterout only the data which date range lies in createdate column or updatedAt column.

I am not able to get the desired result. Not getting the idea that where I am making the mistake in the query or coed.

What I have tried I will mention here.

let startA = new Date("2021-12-14");
const a = new Date(startA.setHours(startA.getHours() - 5));
const start = new Date(a.setMinutes(startA.getMinutes() - 30));

let endA = new Date("2021-12-17");
const b = new Date(endA.setHours(endA.getHours() - 5));
const end = new Date(b.setMinutes(endA.getMinutes() - 30));

const fetchData = await MyCollection.findOne(
  { 
    _id: ObjectId(req.body.id),
    'lv.createdate': { $gte: start, $lt: end },
    'lv.updatedAt': {
      $elemMatch: { $gte: start, $lt: end }
    }
  }
).lean().exec();

Any help or suggestion is really appreciated. Thanks in advance for the interaction.

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

0

Your input data is not valid. With some assumptions, the solution could be this one:

db.MyCollection.aggregate([
   {
      $set: {
         lv: {
            $map: {
               input: "$lv",
               as: "lv",
               in: {
                  $filter: {
                     input: "$$lv.updatedAt",
                     cond: {
                        $and: [
                           { $gte: ["$$this", start] },
                           { $lt: ["$$this", end] }
                        ]
                     }
                  }
               }
            }
         }
      }
   }
])

or this one:

db.MyCollection.aggregate([
   {
      $set: {
         "lv.updatedAt": {
            $filter: {
               input: "$lv.updatedAt",
               cond: {
                  $and: [
                     { $gte: ["$$this", start] },
                     { $lt: ["$$this", end] }
                  ]
               }
            }
         }
      }
   }
])
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!