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

167
Views
Merge two objects inside an array, one with a key nested deeper than the other in the same document in Mongo DB

Schema of the document

id is the key to join

{ 
    "foo" : [
        {
            "properties" : {
                "id" : 1
            }, 
        }, 
        {
            "properties" : {
                "id" : 2
            }, 
        }], 
    "bar" : [
        {
            "id" : 1,
            "metadata" : abc 
        },
        {
            "id" : 2,
            "metadata" : def 
        }
    ]
}

Goal

 { 
    "foo" : [
        {
            "properties" : {
                "id" : 1,
                "metadata" : abc 
            }, 
        }, 
        {
            "properties" : {
                "id" : 2,
                "metadata" : def 
            }, 
        }, 
}
over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

You can use the $lookup on the schema and with the help of $unwind and $project you will get the desired result.

Query:

db.foo.aggregate({
  $lookup: {
    from: "bar",
    localField: "properties.id",
    foreignField: "id",
    as: "properties"
  },
  
},
{
  $unwind: {
    path: "$properties",
    
  }
},
{
  $project: {
    _id: 0,
    properties: 1
  }
})

Output:

[
  {
    "properties": {
      "_id": ObjectId("5a934e000102030405000000"),
      "id": 1,
      "metadata": "abc"
    }
  },
  {
    "properties": {
      "_id": ObjectId("5a934e000102030405000001"),
      "id": 2,
      "metadata": "def"
    }
  }
]
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!