title: MongoDB function returns [unknown type] result
body: <p>I am using Mongo v3.2 and I've written a stored function which iterates over almost 6,000 documents (each document is ~29kB). The parameters are an integer array 'A' (length: 2048) and another integer 'n'. The function compares 'A' with A-like lists present in every document to find cosine similarity and return a list of 'n' dictionaries. (I have also filtered out documents which don't have A-like list)</p>
  
  <p>Every time I fire the function from mongo shell it gives me 2 result screens. The 1st one has array 'A' and the 2nd one <strong>[unknown type]</strong>. I tried using batchSize(5) and still same result. But when I run the same query and put limit(100) it gives me the expected result format.</p>
  
  <p>I think the function is breaking because the number of documents and size of each document is a little big. If this is the case (or not), how can I fix it?</p>
  
  <p>(Edit) The code:</p>
  
  <pre><code>function(A1, n) {   
     var list = [];   
     var cursor = db.product.find({A2:{$exists: true}}, no_cursor_timeout=true);   
     cursor.forEach(function(doc)   {
          var A2 = doc.A2;
          /*
          formula: (a = A1, b = A2)
           num = a(0)*b(0) + a(1)*b(1) ... +a(2047)*b(2047)
           den = sqrt([ a(0)^2 + a(1)^2 ... ]*[ b(0)^2 + b(1)^2 ...]) =&gt; L2 normal form of both arrays
           similarity = num / den
           distance = 1 - similarity
          */  
          var result = { "field1": val1,    "distance": distance  };  
          list.push(result);   
     });   
    list.sort(function(a, b){return a.distance- b.distance});   
    var newlist = list.slice(0,n);   
    cursor.close();
    return (newlist);}
  </code></pre>