title: Using mongodb aggregate to match documents and get all values for a field
body: <p>In Mongo 3.4</p>
  
  <p>I have a collection with documents in the format:</p>
  
  <p>Type1:</p>
  
  <pre><code>{
  "Level1": {
      "@version": "genR",
      "@revision": "aux",
      "Level2": {
          "container": {
              "type": "ARRAY",
              "categories": [
                  {
                      "category": [
                          {
                              "Type": "STRING",
                              "Value": "Currency"
                          },
                          {
                              "Type": "STRING",
                              "Value": "EUR"
                          }
                      ]
                  },
                  {
                      "category": [
                          {
                              "Type": "STRING",
                              "Value": "Portfolio"
                          },
                          {
                              "Type": "STRING",
                              "Value": "ABCDEF"
                          }
                      ]
                  },
               ]
            }
        }
    }
  }
  </code></pre>
  
  <p>Type 2:</p>
  
  <pre><code> {
  "Level1": {
      "@version": "genR",
      "@revision": "aux",
      "Level2": {
          "container": {
              "type": "ARRAY",
              "categories": [
                  {
                      "category": [
                          {
                              "Type": "STRING",
                              "Value": "Currency"
                          },
                          {
                              "Type": "STRING",
                              "Value": "EUR"
                          }
                      ]
                  },
                  {
                      "category": [
                          {
                              "Type": "STRING",
                              "Value": "Portfolio"
                          },
                          {
                              "Type": "STRING",
                              "Value": "ABCDEF"
                          }
                      ]
                  },
                  {
                      "category": [
                          {
                              "Type": "STRING",
                              "Value": "Short Description"
                          },
                          {
                              "Type": "STRING",
                              "Value": "Cash Only"
                          }
                      ]
                  },
               ]
            }
        }
    }
  }
  </code></pre>
  
  <p>How do i write an aggregate statement so that I get ALL the Currency Values, ONLY from the documents where Portfolio matches a certain value.</p>
  
  <p>I have been using pymongo's aggregate framework as below:</p>
  
  <pre><code>pipeline = [{"$unwind":"$Level1.Level2.container.categories"},{"$unwind":"$Level1.Level2.container.categories.category"},{"$match":{"Level1.Level2.container.categories.category.Value":"Portfolio"}}]
  pprint(db.command('aggregate',collection,pipeline=pipeline))
  </code></pre>
  
  <p>But no results. Pymongo is a little confusing. Even if someone can point the general approach, it would really help.</p>
  
  <p>The expected response assuming 4 matching documents (each with varying number of category items) is:</p>
  
  <pre><code>{'Currency': [{'Level1': {'Level2': {'container': {'categories': {'category': {'Value': 'EUR'}}}}}},
            {'Level1': {'Level2': {'container': {'categories': {'category': {'Value': 'EUR'}}}}}},
            {'Level1': {'Level2': {'container': {'categories': {'category': {'Value': 'USD'}}}}}},
            {'Level1': {'Level2': {'container': {'categories': {'category': {'Value': 'EUR'}}}}}}]}
  </code></pre>