title: Unable to query all the documents of a reference model that match a string instead of an id
body: <p>So I am working on an app where I have two models, Posts and Categories. In posts there's a field which references the Categories model. Now there are multiple posts with the same category and I need to fetch those posts by passing a string which will match with the category name. I need to use aggregate here as there are thousands of posts. And it takes a lot of time to fetch 90k posts and then match with the category name to find all the posts. So I want to use aggregate to lookup the category with the category name then use that category's id to find all the posts.</p>
  <p>Suppose here are both the schemas:</p>
  <pre><code>**POSTS SCHEMA** 
  
  const postSchema = new mongoose.Schema( {
      text: {
          type: String
      },
      categories: [ {
          _id: false,
          categoryID: {
              type: mongoose.Schema.Types.ObjectId,
              ref: 'Categories',
          }
      } ]
  } );
  
  **CATEGORIES SCHEMA**
  
  const categorySchema = new mongoose.Schema( {
      category: {
          type: String,
      }
  } );
  </code></pre>
  <p>So now suppose I need to find all the posts that have a category called <code>Politics</code>. I can't fetch all the posts and then filter out based on category name as fetching takes a lot of time because of the total number of posts. So here's what I tried:</p>
  <pre><code>const aggregateObj = [ 
              {
                  &quot;$lookup&quot;: {
                      &quot;from&quot;: &quot;categories&quot;,
                      &quot;let&quot;: { 
                          &quot;category&quot;: &quot;$category&quot;
                      },
                      &quot;pipeline&quot;: [
                          { 
                              &quot;$match&quot;: {
                                  &quot;category&quot;: &quot;Politics&quot;
                              }
                          }
                      ],
                      &quot;as&quot;: &quot;category&quot;
                  }
              },
              {
                  $unwind: &quot;$category&quot;
              },
              {
                  &quot;$lookup&quot;: {
                      &quot;from&quot;: &quot;posts&quot;,
                      &quot;pipeline&quot;: [
                          { 
                              &quot;$match&quot;: {
                                  &quot;categories.categoryID&quot;: &quot;$_id&quot;
                              }
                          }
                      ],
                      &quot;as&quot;: &quot;posts&quot;
                  }
              }
  ];
      
  const posts= await Post.aggregate( aggregateObj ).exec();
  </code></pre>
  <p>I don't get any output as it keeps on loading without any errors. What am I doing wrong here?</p>