title: Processing result sets of queries with package 'rmongodb'
body: <p>How would you go about processing a result set (<strong>length > 1</strong>) of a MongoDB query when using the <a href="http://cran.at.r-project.org/web/packages/rmongodb/index.html" rel="nofollow">rmongodb</a> package and when your final object should be a <code>list</code>? </p>
  
  <p>I try to avoid R-typical "pass-by-value" copying inefficiencies which occur when simply appending the <code>list</code> object while stepping through the result set. But in order to do that, I guess I would need to know how many "records" the query returned in total, don't I? That way I could span an empty list and just have that filled when stepping through the result set - or even better, I could use <code>lapply()</code> and the like.</p>
  
  <p>Here's a little example</p>
  
  <h2>Example Content</h2>
  
  <p>The example is taken from the <a href="http://www.mongodb.org/display/DOCS/Querying+and+nulls" rel="nofollow">MongoDB Website</a> and implemented via <a href="http://cran.at.r-project.org/web/packages/rmongodb/index.html" rel="nofollow">rmongodb</a></p>
  
  <pre><code>mongo &lt;- mongo.create(db="test")
  ns &lt;- "test.foo"
  buf &lt;- mongo.bson.buffer.create()
  mongo.bson.buffer.append(buf, "x", 1)
  mongo.bson.buffer.append(buf, "y", 1)
  x.1 &lt;- mongo.bson.from.buffer(buf)
  buf &lt;- mongo.bson.buffer.create()
  mongo.bson.buffer.append(buf, "x", 2)
  mongo.bson.buffer.append(buf, "y", "string")
  x.2 &lt;- mongo.bson.from.buffer(buf)
  buf &lt;- mongo.bson.buffer.create()
  mongo.bson.buffer.append(buf, "x", 3)
  mongo.bson.buffer.append(buf, "y", NULL)
  x.3 &lt;- mongo.bson.from.buffer(buf)
  buf &lt;- mongo.bson.buffer.create()
  mongo.bson.buffer.append(buf, "x", 4)
  x.4 &lt;- mongo.bson.from.buffer(buf)
  
  mongo.insert.batch(mongo, ns, list(x.1, x.2, x.3, x.4))
  </code></pre>
  
  <h2>Query</h2>
  
  <pre><code>cursor &lt;- mongo.find(mongo, ns, query=list(y=NULL))
  
  # Alternatively
  buf &lt;- mongo.bson.buffer.create()
  mongo.bson.buffer.append(buf, "y", NULL)
  query &lt;- mongo.bson.from.buffer(buf)
  cursor &lt;- mongo.find(mongo, ns, query)
  </code></pre>
  
  <h2>Processing Query Result</h2>
  
  <p>That's the best that I could come up with:</p>
  
  <pre><code>out &lt;- NULL
  while (mongo.cursor.next(cursor)) {
      out &lt;- c(out, list(mongo.bson.to.list(mongo.cursor.value(cursor))))
  }
  out
  </code></pre>
  
  <p>Yet what I'm looking for is something like <code>as.list(cursor)</code> or something like this:</p>
  
  <pre><code># Say I could find out the length of the result set:
  cursor.length &lt;- length(cursor
  out &lt;- lapply(cursor.length, function(x) {
      mongo.cursor.value(cursor[[x]])
  )}
  
  # Alternative:
  out &lt;- vector("list", cursor.length)
  for (x in 1:cursor.length) {
      out[[x]] &lt;- mongo.cursor.value(cursor[[x]])
  )}
  </code></pre>
  
  <p>Is this possible? Unfortunately, I'm not really familiar with C/C++ pointer which I think are used by the package.</p>