title: How to match a mongodb subarray based on main document id?
body: <p>I have the following pipeline in my pymongo script:</p>
  
  <pre><code>pipeline = [
      {'$match': {'_id': '123456'}},
      {'$lookup': {
      'from': 'Case', 
      'localField': '_id', 
      'foreignField': 'RecordList.Record._id', 
      'as': 'CaseInfo'}
      },
     {'$unwind':'$CaseInfo'},
     {'$unwind':'$CaseInfo.RecordList'},
     {'$unwind':'$CaseInfo.RecordList.Record'},
     {'$match': {'CaseInfo.RecordList.Record._id': '123456'}}
  ]
  </code></pre>
  
  <p>I need to change the last line of code so that I don't need to specify the document id manually, but take it from the initial document.</p>
  
  <p>I have tried the following with no luck:</p>
  
  <pre><code>{'$match': {'CaseInfo.RecordList.Record._id': '_id'}}
  {'$match': {'CaseInfo.RecordList.Record._id': '$_id'}}
  </code></pre>
  
  <p>Could you please help me?</p>
  
  <p>Also, is this the most efficient way to accomplish this, or should I be using $project? I tried using it, but I don't know the structure of the document and I need every field in the documents. I'm not sure if there's a way to not specify a "1" in the $filter operator (since I don't know the key names)</p>
  
  <p>Thanks in advance</p>