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

123
Views
Having trouble in nested array of abjects

I've used Mongodb aggregation, $facet as I wanted to count every value of "reli" and "prov" from the collection.

This is my code to get results from db.

const keyy = await db.aggregate([
 $facet: { "reli": [
  { $group: { _id: '$reli', count: { $sum: 1 } } } ],
"prov": [
 { $group: { _id: '$prov', count: { $sum: 1 } } }
            ],
])

Output Looks like this:

 [
    {
            "reli": [
                        {
                            "_id": "abcdef",
                            "count": 6
                        },
                        {
                            "_id": "ghij",
                            "count": 1
                        },
                    ],
    
            "prov": [
                    {
                        "_id": "hello",
                        "count": 63
                    },
                    {
                        "_id": "hey",
                        "count": 9
                    },
                   ]
     
    
    }
    ]

But I want That my Expected output is :

 [
    {
            "reli":[
              {abcdef: 6},
              {ghij: 1}
                 ],
    
           "prov":[
              {"hello": 63},
              {"hey": 9}
                 ]
            
    }  
    ]
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You can iterate using $map and generate the new array with the values you want using $arrayToObject like this:

This query simply overwrite values reli and prov with the result of the map. That result is an array compound by objects where the key k is the _id value and the value v is the count value.

db.collection.aggregate([
  {
    "$project": {
      "reli": {
        "$map": {
          "input": "$reli",
          "in": {
            "$arrayToObject": [
              [
                {
                  k: "$$this._id",
                  v: "$$this.count"
                }
              ]
            ]
          }
        }
      },
      "prov": {
        "$map": {
          "input": "$prov",
          "in": {
            "$arrayToObject": [
              [
                {
                  k: "$$this._id",
                  v: "$$this.count"
                }
              ]
            ]
          }
        }
      }
    }
  }
])

Example here

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!