title: iterate over a collection in meteor using nested loop of " forEach "
body: <p>I will start with a small description of the result I want to get:</p>
  
  <p>Let's imagine we have a collection called "<strong>Elements</strong>" which contains the 4 documents: 'a','b','c' and 'd'.
  I want to iterate over "<strong>Elements</strong>" and insert in a new collection called "<strong>Queries</strong>" the pairs : </p>
  
  <p>(a,b);(a,c);(a,d);(b,a);(b,c)...(d,a);(d,b);(d,c). => which means "<strong>Queries</strong>" will contain (in this example) 4*3 = 12 pairs of elements (documents) at the end.</p>
  
  <p>Here is the code I'm using (it's a method in meteor server triggered by a click on a button):</p>
  
  <pre><code>'Queries': function() {
      var allElements = Elements.find();
      allElements.forEach(function(myLeftElement){ //First forEach
          allElements.forEach(function(myRightElement){// Nested forEach
              if(myLeftElement._id !== myRightElement._id){
                  Queries.insert( {myLeftElement : myLeftElement._id, myRightElement : myRightElement._id} );
              }
          }); //End Of nested forEach
      }); //End Of First forEach
  }
  </code></pre>
  
  <p>In fact it works only for the first "myLeftElement" with all other elements "myRightElement" but stops there: 
  it inserts, into "Queries" collection, the pairs: [(a,b);(a,c) and (a,d)] and then stops.</p>
  
  <p>Since I am a beginner in web developement in general and in using Meteor in particular, I am seeking for your help.</p>
  
  <p>1) I need to understand why once the nested cursor method "forEach" stops, the whole function stops.</p>
  
  <p>2) How can I improve the code to get the result I really want: to every element of my collection "myLeftElement" there is a forEach method that creates pairs with all other elements "myRightElement". And then moves to the next "myLeftElement" and do the same till the end.</p>
  
  <p>Thanks,</p>