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

185
Views
mongodb inner loop on same collection

I am looking for a query that finds documents that lower than the specific date and also will give me the row that has a ref key equals to that id

for the following data (1 collection) and the date param is "01-10-2019" (Oct 2019):(so will search first only lte : 01-10-2020

   {
     {
      "id": 1, 
      "ref": null,
      "date": "20-09-2020" (SEP 20)
    },
    {
      "id": 2, 
      "ref": 1,
      "date": "today"
    },
    {
      "id": 3, 
      "ref" null,
       "date": "today"
    
    }
}

the result will be : id 1 and 2 , because 1 is meet the date criteria and id 2 has ref key that equal to id 1. id 3 won't be received because it's not meet the date criteria

   {
     {
      "id": 1, 
      "ref": 2,
      "date": "one month before"
    },
    {
      "id": 2, 
      "ref": 1,
      "date": "today"
    },
 
}

the issue is that I need to go on above 100-200k rows and every time work on let's say 1000 docs

how can I do that in an efficient way?

I thought about something like: 1-

 col.find({date:{$lte:given_Date}}

2- run on each of the result and find

 col.findOne({ref:given_id}}
over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

You can do it using aggregation framework

db.collection.aggregate([
  {
    "$addFields": {//Add a field to convert your dates
      "date1": {
        "$cond": {
          "if": {
            "$eq": [
              "$date",
              "today"
            ]
          },
          "then": new Date(),
          "else": {
            "$toDate": "$date"
          }
        }
      }
    }
  },
  {
    "$lookup": {//Join
      "from": "collection",
      "localField": "id",
      "foreignField": "ref",
      "as": "output"
    }
  },
  {
    "$match": {//match condition
      "date1": {
        "$lte": ISODate("2020-10-01T00:00:00Z")
      }
    }
  }
])
  1. Store a proper date than string format
  2. Do not find current day by today - how do you change it when day passes!!
  3. If the above two suggestions incorporated, you could avoid $addFields.

playground

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!