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

124
Views
Grouping in MongoDB based on condition

I have a task schema -

const taskSchema = new mongoose.Schema({
      type: { type: String, default: '' },
      status: {type: String },
      due_date : {type: Date}
    });

status can be 'Completed' and 'Pending'.

type can be 'Call Back', 'Visit' and 'Meet'.

I have another status called 'Overdue' which is a special case of 'Pending' if the current date ( new Date() ) is greater than due_date, but it is stored in the database as 'Pending' only.

Sample data -

[
    {
        type: 'Call Back', 
        status: 'Pending', 
        due_date: 2021-08-18T05:40:59.007+00:00
    },
    {
        type: 'Site', 
        status: 'Pending', 
        due_date: 2021-09-18T05:40:59.007+00:00
    }, 
    {
        type: 'Call Back', 
        status: 'Completed', 
        due_date: ''
    }, 
    {
        type: 'Meet', 
        status: 'Pending', 
        due_date: 2021-11-18T05:40:59.007+00:00
    }
]

I want something like this -

{
    Pending: { 
        'Call Back' : 1, 
        'Meet': 0, 
        'Site' 1
    }, 
    Completed: { 
        'Call Back' : 1, 
        'Meet': 0, 
        'Site' 0 
    }, 
    Overdue: {
        'Call Back' : 0, 
        'Meet': 1, 
        'Site' 0 
    }
}

I am facing difficulty because I don't actually have the status 'Overdue' in my schema, but I need it while grouping.

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

0

You can use $addFields to conditionally override the status field to 'Overdue'. Then Proceed with the grouping and data wrangling as usual.

 {
    "$addFields": {
      "status": {
        "$cond": {
          "if": {
            $gt: [
              "$due_date",
              new Date()
            ]
          },
          "then": "Overdue",
          "else": "$status"
        }
      }
    }
}

Here is the Mongo playground for your reference.

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!