title: Performing MongoDB's cursor.forEach() in ruby
body: <p>I've just started experimenting with Ruby's Sinatra a couple of days ago, I'm trying to query a MongoDB, the <a href="http://docs.mongodb.org/manual/reference/method/db.collection.find/" rel="nofollow"><code>find_one()</code></a> method works very well, but when trying to get more than one document (i.e when using <a href="http://docs.mongodb.org/manual/reference/method/db.collection.findOne/" rel="nofollow"><code>find()</code></a>) a cursor is returned, I'm used to using the <a href="http://docs.mongodb.org/manual/reference/method/cursor.forEach/#cursor.forEach" rel="nofollow"><code>cursor.forEach()</code></a> method to iterate through all the returned documents, but as I am new to ruby, I am having a hard time figuring it out.</p>
  
  <p>Would be great if you can point me in the right direction, also if you know of a Mongo/Ruby command dictionary or cheat sheet, I would really appreciate it.</p>
  
  <p>Some code to help with the matter:</p>
  
  <pre><code>#The following code is intentionally formatted the way it is, (i.e the case 
  #insensitive, the way I'm calling the database), all that is irrelevant, 
  #but there to show you what I'm doing; I might be screwing up somewhere.
  
  #works fine, returns JSON of required document
  settings.mongo_db['col'].find_one({"key" =&gt; /#{value}/i}).to_json
  
  #returns cursor, need to iterate
  settings.mongo_db['col'].find({"key" =&gt; /#{value}/i}).to_json
  </code></pre>
  
  <p>Your replies/thoughts are much appreciated.</p>