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

177
Views
How to group an array of objects in mongodb

im new to mongodb and Im having some trouble to group an array from a document like this:

_id: ObjectId('5d7afa2609d6ed000dffe1de')
email: "email@email.com"
transactions: [
    {
        "date": "2020-06-10T00:00:00.000+00:00",
        "shares": 100,
        "price": 20,
        "type": "buy",
        "equity_id": "petr4"
    },
    {
        "date": "2020-07-10T00:00:00.000+00:00",
        "shares": 200,
        "price": 10,
        "type": "sell",
        "equity_id": "petr4"
    },
    {
        "date": "2020-06-10T00:00:00.000+00:00",
        "shares": 250,
        "price": 30,
        "type": "buy",
        "equity_id": "vale3"
    }, ...
]

I would like to group these transactions by date and obtain an document like this:

_id: ObjectId('5d7afa2609d6ed000dffe1de')
email: "email@email.com"
transactionsByDay: {
    "2020-06-10": [
        {
            "shares": 100,
            "price": 20,
            "type": "buy",
            "equity_id": "petr4"
        },
        {
            "shares": 250,
            "price": 30,
            "type": "buy",
            "equity_id": "petr4"
        }
    ],
    "2020-07-10": [
        {
            "shares": 200,
            "price": 10,
            "type": "sell",
            "equity_id": "petr4"
        }
    ], ...
}

I tried an aggregation using a group operator but the result didn't came as expected. Can someone give me a help to solve this?

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

0

This can be achieved in two steps

let arr = [
    {
        "date": "2020-06-10T00:00:00.000+00:00",
        "shares": 100,
        "price": 20,
        "type": "buy",
        "equity_id": "petr4"
    },
    {
        "date": "2020-07-10T00:00:00.000+00:00",
        "shares": 200,
        "price": 10,
        "type": "sell",
        "equity_id": "petr4"
    },
    {
        "date": "2020-06-10T00:00:00.000+00:00",
        "shares": 250,
        "price": 30,
        "type": "buy",
        "equity_id": "vale3"
    }
]

let transactionsByDay = arr.reduce((a,e,i) => {
  if(e.type == 'buy') a['buy'].push(e)
  else if(e.type == 'sell') a['sell'].push(e)
  return a
},{buy : [], sell: []})


Object.keys(transactionsByDay).map(id => {
  transactionsByDay[`${transactionsByDay[id][0].date.slice(0,10)}`] = [...transactionsByDay[id]];
  delete transactionsByDay[id];
})

console.log(transactionsByDay)

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!