title: mongoose find by nested object ids return the entire document
body: <p>This is my document structure:</p>
  
  <pre><code>{
  "_id": "590dc8b17e52f139648b9b94",
  "parent": [
      {
          "_id": "590dc8b17e52f139648b9b95",
          "child": [
              {
                  "_id": "590dc8b17e52f139648b9b8f"
              },
              {
                  "_id": "590dc8b17e52f139648b9b90"
              }
          ]
      },
      {
          "_id": "590dc8c57e52f139648b9b9b",
          "child": [
              {
                  "_id": "590dc8c57e52f139648b9b96"
              }
          ]
      }
  ]}
  </code></pre>
  
  <p>I'm trying to return only the information about the nested object which i'm looking for with its _id.</p>
  
  <pre><code>router.get('/child/:id', (req, res) =&gt; {
      Project.findOne({ 'parent.child._id': req.params.id },
              (err, results) =&gt; {
                      if (err)
                              res.status(500).send(err);
        res.status(200).json(results.parent[0].child.id(req.params.id));
  
              }
      )});
  </code></pre>
  
  <p>the problem is that <strong>results</strong> contains the entire document which may contain multiple instances of <strong>parent</strong>
  So obviously my code only works if the known <strong>child</strong> _id it's in the first parent.</p>
  
  <p>How can i fix it? 
  many thanks</p>
  
  <p>so apparently the unwind and match solution is good but for mongoose you need to explicitly say that the id is an ObjectId type.</p>
  
  <pre><code> {$match:
  { "parent.child._id": new mongoose.Types.ObjectId(id) }
              }
  </code></pre>
  
  <p><a href="https://stackoverflow.com/questions/14551387/cant-use-match-with-mongoose-and-the-aggregation-framework">found it here</a></p>