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

162
Views
MongoDB - Get data in date sections from an array

I am new to MongoDB, I want to get data in sections from the array.

{
   "name":"User name",
   "messages":[
      {
         "message":"Message 1",
         "date":"2022-05-12T00:00:00.000Z"
      },
      {
         "message":"Message 2",
         "date":"2022-05-12T00:00:00.000Z"
      },
      {
         "message":"Message 3",
         "date":"2022-05-13T00:00:00.000Z"
      },
      {
         "message":"Message 4",
         "date":"2022-05-13T00:00:00.000Z"
      },
      {
         "message":"Message 5",
         "date":"2022-05-13T00:00:00.000Z"
      }
   ]
}

The results I want are

[
   {
      "date":"2022-05-12T00:00:00.000Z",
      "messages":[
         {
            "message":"Message 1",
            "date":"2022-05-12T00:00:00.000Z"
         },
         {
            "message":"Message 2",
            "date":"2022-05-12T00:00:00.000Z"
         }
      ]
   },
   {
      "date":"2022-05-13T00:00:00.000Z",
      "messages":[
         {
            "message":"Message 3",
            "date":"2022-05-13T00:00:00.000Z"
         },
         {
            "message":"Message 4",
            "date":"2022-05-13T00:00:00.000Z"
         },
         {
            "message":"Message 5",
            "date":"2022-05-13T00:00:00.000Z"
         }
      ]
   }
]

I'm not familiar with MongoDB aggregation, I have looked at many questions on stack overflow but didn't find any that helped me.

Is this possible to do? If yes any help would be greatly appreciated. :)

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

0

  1. $unwind - Deconstruct messages array to multiple documents.
  2. $group - Group by messages.date and add messages document into messages array.
  3. $project - Decorate output documents.
db.collection.aggregate([
  {
    $unwind: "$messages"
  },
  {
    $group: {
      _id: "$messages.date",
      messages: {
        $push: "$messages"
      }
    }
  },
  {
    $project: {
      _id: 0,
      date: "$_id",
      messages: 1
    }
  }
])

Sample Mongo Playground

about 4 years ago · Juan Pablo Isaza Report

0

You can use aggregation to get your desired output.

db.collection.aggregate([
  {
    $unwind: "$messages"
  },
  {
    $group: {
      _id: "$messages.date",
      messages: {
        $push: "$messages"
      }
    }
  },
  {
    $project: {
      _id: 0,
      date: "$_id",
      messages: 1
    }
  }
])
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!