2

How can I sort dueDate using aggregate?

todos: [
    {
        task: {
            type: String,
            trim: true,
            required: 'Please Enter your Task',
        },
        dueDate: Date,
        dueTime: String,
    },
],

I tried these things but it didn't work.

db.server.aggregate(
    { $unwind: '$todos' },
    { $sort: { 'todos.dueDate': -1 } },
    { $group: { _id: '$_id', todos: { $push: '$todos' } } },
    { $project: { 'todos.dueDate': '$dueDate' } }
);

And

db.server.aggregate(
    { $unwind: '$todos' },
    { $sort: { 'todos.dueDate': -1 } },
    { $group: { "_id": "$_id", todos: { $push: '$todos' } } },
);

I am not able to understand what I am doing wrong in the aggregation.

Example:-

Sample Input

{
  _id: 603ba275cc571e2404e0dd1b,
  task: 'task 1',
  dueDate: 2021-02-27T18:30:00.000Z,
  dueTime: ''
},{
  _id: 603ba285cc571e2404e0dd1c,
  task: 'task 2',
  dueDate: 2021-03-30T18:30:00.000Z,
  dueTime: '07:32 PM'
},{
  _id: 603ba290cc571e2404e0dd1d,
  task: 'task 3',
  dueDate: 2021-03-08T18:30:00.000Z,
  dueTime: '07:32 PM'
},{
  _id: 603ba3a7fea412537c295056,
  task: 'task4',
  dueDate: 2021-03-01T18:30:00.000Z,
  dueTime: '07:37 PM'
}

Sample Output


{
  _id: 603ba275cc571e2404e0dd1b,
  task: 'task 1',
  dueDate: 2021-02-27T18:30:00.000Z,
  dueTime: ''
},{
  _id: 603ba3a7fea412537c295056,
  task: 'task4',
  dueDate: 2021-03-01T18:30:00.000Z,
  dueTime: '07:37 PM'
},{
  _id: 603ba290cc571e2404e0dd1d,
  task: 'task 3',
  dueDate: 2021-03-08T18:30:00.000Z,
  dueTime: '07:32 PM'
},{
  _id: 603ba285cc571e2404e0dd1c,
  task: 'task 2',
  dueDate: 2021-03-30T18:30:00.000Z,
  dueTime: '07:32 PM'
}
3
  • So you don't actually want to group by the date? Commented Feb 28, 2021 at 15:17
  • no, i just want it to return the sorted dueDate Commented Feb 28, 2021 at 15:18
  • the dates are store in this format Tue Mar 09 2021 00:00:00 GMT+0530 (India Standard Time) Commented Feb 28, 2021 at 15:20

1 Answer 1

1

If your goal is to group the tasks elements into separate groups by their dueDate and then sort those groups, here's what you can do:

db.collection.aggregate([
  {
    "$unwind": "$todos"
  },
  {
    $group: {
      _id: "$todos.dueDate",
      todos: {
        $push: "$$ROOT"
      }
    }
  },
  {
    $sort: {
      "_id": -1
    }
  },
  
])

Here's a working example on mongoplayground: https://mongoplayground.net/p/n3cfrLvR2WM


After OP edited the question and clarified what the expceted result is, this can be simplified to:

db.collection.aggregate([
  {
    "$unwind": "$todos"
  },
  {
    $sort: {
      "todos.dueDate": 1
    }
  }
])

Mongoplayground: https://mongoplayground.net/p/YVCAiXrJJPv

Sign up to request clarification or add additional context in comments.

6 Comments

See edit .. note that you should always include expected results to your question to make it easier to help you ..
I dont know why but it is still now working
Well, it works as expected on the mongoplayground, right? Or is this not what you want?
Ya, it was working on the mongoplayground but it is not working in my project :(
Note that you need to use "todos.dueDate": 1 if you want the record to be sorted ascending. Is that the problem?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.