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

130
Views
How to aggregate on nested embedded object in MongoDB
{
  "_id": 1,
  "title": {
    header1:{ "name":"A" , age:10, "active":true},
    header2:{ "name":"B" , age:15, "active":false},
    header3:{ "name":"C" , age:20, "active":true},
    header4:{ "name":"D" , age:30, "active":true},
    header5:{ "name":"E" , age:35, "active":false},
    header6:{ "name":"F" , age:40, "active":true},
    header7:{ "name":"G" , age:45, "active":false},
  }
}

How can I show only those headers whose active value is false?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

  1. $project

    1.1 Convert key-value pair to array via $objectToArray for headers field . 1.2 $filter with $$this.v.active which is false.

  2. $project

    2.1 Convert headers to key-value pair via $arrayToObject and assign to title field.

db.collection.aggregate([
  {
    $project: {
      _id: 1,
      headers: {
        $filter: {
          "input": {
            $objectToArray: "$title"
          },
          "cond": {
            "$eq": [
              "$$this.v.active",
              false
            ]
          }
        }
      }
    }
  },
  {
    $project: {
      _id: 1,
      title: {
        "$arrayToObject": "$headers"
      }
    }
  }
])

Sample Mongo Playground

over 4 years ago · Santiago Trujillo 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!