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

374
Views
Mongoose get all documents where date and number of weeks have current date

I have this type of documents in mongoDB.

{
     _id:620df4541571891a71dfa372
     user_id:61cc109f967b757d484fbf3b
     instrument_type:"binance"
     sub_instrument:"BTC/USDT"
     start_date:2022-02-17T00:00:00.000+00:00
     type_quantity:2
     prediction:"High"
     points:12
     price:null
     run_at:null
     createdAt:2022-02-17T07:08:04.633+00:00
     updatedAt:2022-02-17T07:08:04.633+00:00
}

Here I have "start_date" which is starting day. "type_quantity" which is number of weeks from start day.

Example: If "start_date" is 2022-02-17T00:00:00.000+00:00 and "type_quantity" is 2. Then, the dates per week will become 2022-02-17, 2022-02-24 and 2022-03-03. And If current date is one of these dates then, get this document.

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

You can use $dateAdd in an aggregation query like this:

Note here only is necessary a $match stage with $expr to get documents where:

  • Current date ($$NOW) is greater or equal than $start_date.
  • Current date ($$NOW) is lower or equal than calculated date adding X weeks.

i.e. the date found is between $start_date and start_date + X weeks.

db.collection.aggregate([
  {
    "$match": {
      "$expr": {
        "$and": [
          {
            "$gte": [
              "$$NOW",
              "$start_date"
            ]
          },
          {
            "$lte": [
              "$$NOW",
              {
                "$dateAdd": {
                  "startDate": "$start_date",
                  "unit": "week",
                  "amount": "$type_quantity"
                }
              }
            ]
          }
        ]
      }
    }
  }
])

Example here adding more documents.

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!