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

113
Views
MongoDB nested aggregation lookup

I wont to connect two collection so as to workouts._id had an assigned value exercise.name.

first collection "workouts":

{
        "_id": {
            "$oid": "60ffbd531b1cba41e8a7fde3"
        },
        "id_user": {
            "$oid": "60fd50ee1e9d064a5cee0bec"
        },
        "date": {
            "$date": "2021-05-20T22:00:00.000Z"
        },
        "name": "wielki bicek",
        "workouts": [{
            "_id": {
                "$oid": "60e07e588a2f1946f895475f"
            },
            "parts": [{
                "_id": {
                    "$oid": "60ffbd531b1cba41e8a7fde5"
                },
                "weight": "30",
                "repeating": "10"
            }]
        }, {
            "_id": {
                "$oid": "60e07e588a2f1946f895475f"
            },
            "parts": [{
                "_id": {
                    "$oid": "60ffbd531b1cba41e8a7fde9"
                },
                "weight": "30",
                "repeating": "10"
            }, {
                "_id": {
                    "$oid": "60ffbd531b1cba41e8a7fdea"
                },
                "weight": "30",
                "repeating": "10"
            }]
        }],
        "__v": 0
    }

second collection "exercises":

{
    "_id": {
        "$oid": "60e07e588a2f1946f895475f"
    },
    "name": "Wyciskanie sztangi",
    "image": "uploads\\2021-07-03T15-12-24.173Z-test.gif",
    "type": "force",
    "groupMuscle": {
        "$oid": "60daf099c1aaa20b4cfdf0fa"
    },
    "instruction": "Opierając tułów znazewnątrz.",
    "__v": 0
}

I have one collection that I want to run nested aggregation on this, but I don't know how to run this operation.

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

0

  • $unwind unwind workouts
  • $lookup lookpup exercises
  • $addFileds get first item
  • $addFileds get only name field
  • $project remove useless field
  • $group group by original id
db.workouts.aggregate([
  {
    "$unwind": "$workouts"
  },
  {
    "$lookup": {
      "from": "exercises",
      pipeline: [
        {
          "$project": {
            name: 1
          }
        }
      ],
      "localField": "workouts._id",
      "foreignField": "_id",
      "as": "names"
    }
  },
  {
    "$addFields": {
      "workouts.i": {
        "$first": "$names"
      }
    }
  },
  {
    "$addFields": {
      "workouts.name": "$workouts.i.name"
    }
  },
  {
    "$project": {
      "names": 0,
      "workouts.i": 0
    }
  },
  {
    "$group": {
      "_id": {
        "_id": "$_id",
        "id_user": "$id_user",
        "date": "$date",
        "name": "$name"
      },
      "workouts": {
        $push: "$workouts"
      }
    }
  }
])

mongoplayground

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!