title: Populate a single ObjectId reference after aggregating with mongoDb and mongoose
body: <p>I am trying to populate after aggregating and counting.</p>
  <p>These are mye models:</p>
  <p>UserLog</p>
  <pre><code>const UserLog = new Schema(
    {
      title: {
        type: String,
        required: true,
        trim: true,
      },
      article: {
        type: mongoose.Schema.ObjectId,
        ref: 'Article',
        required: true,
      },
      user: {
        type: mongoose.Schema.ObjectId,
        ref: 'User',
        required: true,
      },
    },
    { timestamps: true, toJSON: { virtuals: true }, toObject: { virtuals: true } }
  );
  </code></pre>
  <p>Article</p>
  <pre><code>const Article = new Schema(
    {
      title: {
        type: String,
        required: true,
        trim: true,
      },
      content: {
        type: String,
        required: true,
      },
    },
    { timestamps: true, toJSON: { virtuals: true }, toObject: { virtuals: true } }
  );
  </code></pre>
  <p>And I am running a query like this:</p>
  <pre><code>export const getCountByArticle = async () =&gt; {
    const articles = await UserLog.aggregate([
      {
        $group: { _id: '$article', count: { $sum: 1 } },
      },
    ]);
    return articles;
  };
  </code></pre>
  <p>The query returns this:</p>
  <pre><code>[
          {
              &quot;_id&quot;: &quot;5fcbc12dd895b528289723e6&quot;,
              &quot;count&quot;: 1
          },
          {
              &quot;_id&quot;: &quot;5fccefc375c47a3fd49c083d&quot;,
              &quot;count&quot;: 2
          }
      ],
  </code></pre>
  <p>The problem is that i want to &quot;populate&quot; the _id field with the actual article. I have tried $lookup, but that returns an array with all of the articles, and I dont want that. Unwind is not working for me either. Under is how i actually want the result to be.</p>
  <pre><code>[
          {
              &quot;_id&quot;: &quot;5fcbc12dd895b528289723e6&quot;,
              &quot;title&quot;: &quot;Title&quot;,
              &quot;content&quot;: &quot;contentcontentcontentcontentcontentcontentcontentcontentcontentcontentcontentcontentcontentcontentcontentcontentcontentcontentcontentcontentcontent&quot;,
              &quot;count&quot;: 1
          },
          {
              &quot;_id&quot;: &quot;5fccefc375c47a3fd49c083d&quot;,
              &quot;title&quot;: &quot;Title&quot;,
              &quot;content&quot;: &quot;contentcontentcontentcontentcontentcontentcontentcontentcontentcontentcontentcontentcontentcontentcontentcontentcontentcontentcontentcontentcontent&quot;,
              &quot;count&quot;: 2
          }
      ],
  </code></pre>
  <p>I really hope that someone can help me with this. Thanks in advance.</p>