title: mongodb how to use aggregate like populate
body: <p>The code example is in <strong><em>Mongo Playground</em></strong></p>
  
  <blockquote>
    <p><a href="https://mongoplayground.net/p/W4Qt4oX0ZRP" rel="nofollow noreferrer">https://mongoplayground.net/p/W4Qt4oX0ZRP</a></p>
  </blockquote>
  
  <p>Assume the following documents</p>
  
  <pre class="lang-js prettyprint-override"><code>[
    {
      _id: "5df1e6f75de2b22f8e6c30e8",
      user: {
        name: "Tom",
        sex: 1,
        age: 23
      },
      dream: [
        {
          label: "engineer",
          industry: "5e06b16fb0670d7538222909",
          type: "5e06b16fb0670d7538222951",
  
        },
        {
          label: "programmer",
          industry: "5e06b16fb0670d7538222909",
          type: "5e06b16fb0670d7538222951",
  
        }
      ],
      works: [
        {
          name: "any engineer",
          company: "5dd7fd51b0ae1837a08d00c8",
          skill: [
            "5dc3998e2cf66bad16efd61b",
            "5dc3998e2cf66bad16efd61e"
          ],
  
        },
        {
          name: "any programmer",
          company: "5dd7fd9db0ae1837a08d00e2",
          skill: [
            "5dd509e05de2b22f8e67e1b7",
            "5dd509e05de2b22f8e67e1bb"
          ],
  
        }
      ]
    }
  ]
  </code></pre>
  
  <p>I tried to use <code>aggregate</code> <code>$lookup</code> <code>$unwind</code></p>
  
  <pre class="lang-js prettyprint-override"><code>db.coll.aggregate([
    {
      $unwind: {
        path: "$dream",
  
      }
    },
    {
      $lookup: {
        from: "industry",
        localField: "dream.industry",
        foreignField: "_id",
        as: "dream.industry"
      },
  
    },
    {
      $unwind: {
        path: "$dream.industry",
  
      }
    },
    {
      $lookup: {
        from: "type",
        localField: "dream.type",
        foreignField: "_id",
        as: "dream.type"
      },
  
    },
    {
      $unwind: {
        path: "$dream.type",
  
      }
    },
    {
      $unwind: {
        path: "$works",
  
      }
    },
    {
      $lookup: {
        from: "company",
        localField: "works.company",
        foreignField: "_id",
        as: "works.company"
      },
  
    },
    {
      $unwind: {
        path: "$works.company",
  
      }
    },
    {
      $lookup: {
        from: "skill",
        localField: "works.skill",
        foreignField: "_id",
        as: "works.skill"
      },
  
    },
  
  ])
  </code></pre>
  
  <p>Executing the above code did not get the desired result!</p>
  
  <h2>This is what i expect</h2>
  
  <pre class="lang-js prettyprint-override"><code>{
    _id: "5df1e6f75de2b22f8e6c30e8",
    user: {
      name: 'Tom',
      sex: 1,
      age: 23
    },
    dream: [
      {
        label: 'engineer',
        industry: {
          _id: "5e06b16fb0670d7538222909",           // Industry doc _id
          name: 'IT',
          createdAt: "2019-12-28T01:35:44.070Z",
          updatedAt: "2019-12-28T01:35:44.070Z"
        },
        type: {
          _id: "5e06b16fb0670d7538222951",           // Type doc _id
          name: 'job',
          createdAt: "2019-12-28T01:35:44.070Z",
          updatedAt: "2019-12-28T01:35:44.070Z"
        },
      },
      {
        label: 'programmer',
        industry: {
          _id: "5e06b16fb0670d7538222909",           // Industry doc _id
          name: 'IT',
          createdAt: "2019-12-28T01:35:44.070Z",
          updatedAt: "2019-12-28T01:35:44.070Z"
        },
        type: {
          _id: "5e06b16fb0670d7538222951",           // Type doc _id
          name: 'job',
          createdAt: "2019-12-28T01:35:44.070Z",
          updatedAt: "2019-12-28T01:35:44.070Z"
        }
      }
    ],
    works: [
      {
        name: 'any engineer',
        company: {
          _id: "5dd7fd51b0ae1837a08d00c8",          // Company doc _id
          name: 'alibaba',
          area: 'CN',
        },
        skill: [
          { 
            _id: "5dc3998e2cf66bad16efd61b",        // Skill doc _id
            name: 'Java' 
          }, 
          { 
            _id: "5dc3998e2cf66bad16efd61e",        // Skill doc _id
            name: 'Php' 
          }, 
        ]
      },
      {
        name: 'any programmer',
        company: {
          _id: "5dd7fd9db0ae1837a08d00e2",           // Company doc _id
          name: 'microsoft',
          area: 'EN',
        },
        skill: [
          { 
            _id: "5dd509e05de2b22f8e67e1b7",           // Skill doc _id
            name: 'Golang' 
          },
          { 
            _id: "5dd509e05de2b22f8e67e1bb",         // Skill doc _id
            name: 'Node.js'
          }
        ]
      },
    ]
  }
  </code></pre>
  
  <p>The expected result is <code>dream</code> is an array, <code>works</code> is an array, and <code>dream.industry</code> changed from ObjectId to document, <code>dream.type</code> changed from ObjectId to document, <code>works.company</code> changed from ObjectId to document </p>
  
  <p>When I use populate, I can do it easily</p>
  
  <pre class="lang-js prettyprint-override"><code>Model.find()
   .populate('dream.industry')
   .populate('dream.type')
   .populate('works.company')
   .populate('works.skill')
   .lean()
  </code></pre>
  
  <p>I refer to the following questions</p>
  
  <blockquote>
    <ol>
    <li><a href="https://stackoverflow.com/questions/43249218/mongoose-aggregate-lookup-array">mongoose aggregate lookup array</a> (Almost the same as my question, But not resolved)</li>
    <li><a href="https://stackoverflow.com/questions/34967482/lookup-on-objectids-in-an-array">$lookup on ObjectId&#39;s in an array</a></li>
    </ol>
  </blockquote>
  
  <p>hope to get everyone's help, thank you!</p>