Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

135
Visualizações
How to sort Arrays of Object using aggregation in mongoDB

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'
}
over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

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

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda