title: Retrieving multiple mongo documents from cursor.next() for better performance
body: <p>I have the following code to gather arrays (stored as pickled documents in a Mongo collection) and stack them into a 2D numpy matrix:</p>
  
  <pre><code>db = MongoClient()['db']
  total = db.command('collStats', 'collection')['count']
  collection = db['collection']
  
  X, Y = np.array([]), np.array([])
  pipe = [{'$sample': {'size': total}}]
  cursor = collection.aggregate(pipe, allowDiskUse=True)
  
  for i in range(1000):
      try:
          xy = pickle.loads(cursor.next()['array'])
          if not np.size(X):
              X, Y = xy[0:-1], xy[-1]
          else:
              X, Y = np.vstack((X, xy[0:-1])), np.vstack((Y, xy[-1]))
      except StopIteration:
          break
  </code></pre>
  
  <p>which is extremely slow. How can I optimize this, say, to read multiple documents at once from Mongo? Any other optimization tips would be useful as well</p>