title: How to create a mongoDB aggregate query in python for documents which contain nested list of documents?
body: <p>I have a mongoDB collection with documents that look like:</p>
  
  <pre><code>{
      "_id" : "132743",
      "RECORD_DATA" : [ 
          {
              "FIELD_TYPE" : "Primary",
              "DATA" : "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec blandit leo sit amet nisi ultricies bibendum. Aenean efficitur pharetra diam, non pretium nisi blandit eu. Maecenas eget dolor sed ipsum semper posuere id eget purus. Ut tempor massa vel porta euismod. Vivamus et elementum justo. Aliquam porta, ipsum at semper pulvinar, turpis ipsum congue orci, a fringilla turpis arcu eget lorem. Vestibulum consectetur sapien neque, at vulputate mi congue vitae.",
              "DETAIL" : "XYZ"
          },  
          {
              "FIELD_TYPE" : "Secondary",
              "DATA" : "Nullam congue aliquam risus. Aenean semper ut elit id viverra. Mauris tincidunt non justo et tempor. Donec non tempus sapien. Curabitur facilisis risus tortor, nec bibendum libero feugiat sed. Curabitur eu quam ac mi sodales ultricies. Cras posuere tincidunt faucibus.",
              "DETAIL" : "XYZ"
          },
          {
              "FIELD_TYPE" : "Secondary",
              "DATA" : "Lorem ipsum dolor sit amet, consectetur adipiscing elit. ",
              "DETAIL" : "ABC"
          }, 
          {
              "FIELD_TYPE" : "Secondary",
              "DATA" : "Maecenas volutpat facilisis tortor sed pellentesque. Quisque tristique sem sit amet ipsum convallis porttitor. Vestibulum a tempus ex. Donec molestie tortor est, sed malesuada sapien maximus vel.",
              "DETAIL" : "ABC"
          },
          {
              "FIELD_TYPE" : "optional1",
              "DATA" : "Curabitur faucibus dolor nisl, at venenatis tortor fermentum at. Vestibulum sodales posuere neque id aliquet. Aliquam dignissim ex quis lacus fermentum, at consectetur nunc viverra. ",
              "DETAIL" : "XYZ"
          }, 
          {
              "FIELD_TYPE" : "optional2",
              "DATA" : "Cras vulputate lacinia elit, eu fringilla neque imperdiet eget. Nam placerat venenatis felis at pharetra. Praesent vestibulum ligula sit amet elit dignissim suscipit. ",
              "DETAIL" : "QWE"
          }
      ]
  }
  </code></pre>
  
  <p>I want to extract the single field from <code>RECORD_DATA</code> which has <code>FIELD_TYPE</code> set to <code>Primary</code> along with <code>_id</code> for each document in collection. My final output should look like:</p>
  
  <pre><code>{
      "_id" : "132743",
      "DATA" : "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec blandit leo sit amet nisi ultricies bibendum. Aenean efficitur pharetra diam, non pretium nisi blandit eu. Maecenas eget dolor sed ipsum semper posuere id eget purus. Ut tempor massa vel porta euismod. Vivamus et elementum justo. Aliquam porta, ipsum at semper pulvinar, turpis ipsum congue orci, a fringilla turpis arcu eget lorem. Vestibulum consectetur sapien neque, at vulputate mi congue vitae."
  }
  </code></pre>
  
  <p>I tried aggregation Query:</p>
  
  <pre><code>MongoDatabase.db[collection].aggregate([{$project:{'DATA':'$RECORD_DATA.DATA'}},{$match:{'RECORD_DATA.FIELD_TYPE':"Primary"}}])
  </code></pre>
  
  <p>This is not giving me any output. Can anybody help me create a query for it?</p>