title: Unwind Multiple Document Arrays Into New Documents
body: <p>Today I run into a situation I need to sync a mongoDB collection to vertica (SQL Database) where my object keys will be the columns of the table in SQL.
  I use mongoDB aggregation framework, first to query, manipulate and project the wanted result document and then I sync it to vertica.</p>
  
  <p>The schema I want to aggregate looks like this:</p>
  
  <pre><code>{
    userId: 123
    firstProperty: {
      firstArray: ['x','y','z'],
      anotherAttr: 'abc'
    },
    anotherProperty: {
      secondArray: ['a','b','c'],
      anotherAttr: 'def'
    }  
  }
  </code></pre>
  
  <p>Since array values are not related with other arrays value, what I need is that each value of nested array, will be in a separate result document.
  For that I use the following aggregation pipe:</p>
  
  <pre><code>db.collection('myCollection').aggregate([
              {
                  $match: {
                      $or: [
                          {'firstProperty.firstArray.1': {$exists: true}},
                          {'secondProperty.secondArray.1': {$exists: true}}
                      ]
                  }
              },
              {
                  $project: {
                      userId: 1,
                      firstProperty: 1,
                      secondProperty: 1
                  }
              }, {
                  $unwind: {path:'$firstProperty.firstAray'}
              }, {
                  $unwind: {path:'$secondProperty.secondArray'},
              }, {
                  $project: {
                      userId: 1,
                      firstProperty: '$firstProperty.firstArray',
                      firstPropertyAttr: '$firstProperty.anotherAttr',
                      secondProperty: '$secondProperty.secondArray',
                      seondPropertyAttr: '$secondProperty.anotherAttr'
                  }
              }, {
                  $out: 'another_collection'
              }
          ])
  </code></pre>
  
  <p>What I expect is the following result:</p>
  
  <pre><code>{
    userId: 'x1',
    firstProperty: 'x',
    firstPropertyAttr: 'a'
  }
  {
    userId: 'x1',
    firstProperty: 'y',
    firstPropertyAttr: 'a'
  }
  {
    userId: 'x1',
    firstProperty: 'z',
    firstPropertyAttr: 'a'
  }
  {
    userId: 'x1',
    secondProperty: 'a',
    firstPropertyAttr: 'b'
  }
  {
    userId: 'x1',
    secondProperty: 'b',
    firstPropertyAttr: 'b'
  }
  {
    userId: 'x1',
    secondProperty: 'c',
    firstPropertyAttr: 'b'
  }
  </code></pre>
  
  <p>Instead I get something like that:</p>
  
  <pre><code>{
    userId: 'x1',
    firstProperty: 'x',
    firstPropertyAttr: 'b'
    secondProperty: 'a',
    secondPropertyAttr: 'b'
  }
  {
    userId: 'x1',
    firstProperty: 'y',
    firstPropertyAttr: 'b'
    secondProperty: 'b',
    secondPropertyAttr: 'b'
  }
  {
    userId: 'x1',
    firstProperty: 'z',
    firstPropertyAttr: 'b'
    secondProperty: 'c',
    secondPropertyAttr: 'b'
  }
  </code></pre>
  
  <p>What exactly am I missing, and how can I fix it?</p>