title: MongoDb $lookup doing reads on foreign collection for each row
body: <p>First time using the lookup operator on Mongodb and I found something strange. I have 2 simple collections that look more or less like this:</p>
  
  <pre><code>Book
  {
      "_id": 1,
      "Title": "Some book name",
      "AuthorId": 1,
  }   
  
  Author
  {
      "_id": 1,
      "Name": "Some Author",
      "Location": "US"
  }
  enter code here
  </code></pre>
  
  <p>There is also an index on Book.AuthorId.</p>
  
  <p>So, what I wanted is to query all <code>Books</code> that have <code>Authors</code> in the US. So after reading the docs.. I came up with this query:</p>
  
  <pre><code>db.Book.aggregate([
      {
         $lookup: {
             from: "Author",
             localField: "AuthorId",
             foreignField: "_id",
             as: "author"
         }
      },
      {
          $unwind: "$author"
      },
      {
          $match:{ "author.Location": {$eq: "US"}}
      }
   ])
  </code></pre>
  
  <p>Which works great. But, just out of curiosity I turned on the profiling and looked what Mongo did when I executed the query above. For my surprise, I saw it did one Author lookup for each book it read. The whole point of using the <code>$lookup</code> operator was to not having to look all Authors before hand, so I'm kind of lost here. I'm not looking to group anything so.. having a <code>$group</code> before the lookup doesn't help me (at least I think so... enlighten me if I'm wrong)</p>
  
  <p>Here you can see the profiling after I run the query:
  <a href="https://i.stack.imgur.com/TmIAz.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/TmIAz.png" alt="enter image description here"></a></p>