简体   繁体   中英

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM