Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to access MongoDB's aggregation framework from the rmongodb R package
    primarykey
    data
    text
    <p>I'm trying to access the mongodb aggregation framework from rmongodb. It should be available through mongo.command() but I cannot get the bson correct.</p> <p>Here is a replicable example. In R, set up a test collection:</p> <pre><code>mongo &lt;- mongo.create() db &lt;- "test" ns &lt;- "test.people" buf &lt;- mongo.bson.buffer.create() mongo.bson.buffer.append(buf, "name", "John") mongo.bson.buffer.append(buf, "age", 22L) b &lt;- mongo.bson.from.buffer(buf) mongo.insert(mongo, ns, b); buf &lt;- mongo.bson.buffer.create() mongo.bson.buffer.append(buf, "name", "John") mongo.bson.buffer.append(buf, "age", 35L) b &lt;- mongo.bson.from.buffer(buf) mongo.insert(mongo, ns, b); buf &lt;- mongo.bson.buffer.create() mongo.bson.buffer.append(buf, "name", "Fred") mongo.bson.buffer.append(buf, "age", 27L) b &lt;- mongo.bson.from.buffer(buf) mongo.insert(mongo, ns, b); Now, on the mongo console, the following works: use test db.runCommand({ aggregate : "people", pipeline : [ { $group : { _id : '$name', total : { $sum : 1 } } } ] }) </code></pre> <p>It seems like the following in R would achieve the same effect:</p> <pre><code>mongo.command(mongo, "test", list( aggregate="people", pipeline=list( group=list( "_id"="$name", total=list("$sum"=1) ) ) )) </code></pre> <p>This does not work.</p> <p>For this particular aggregation, I am aware that there are other ways to do it. I'm just providing it as a simple example but I want to get to more complex aggregations in the future.</p> <p>Also, I am aware of another R package for mongodb that I might be able to look into to do the trick, but I'm pretty heavily invested into using this package right now.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload