Note that there are some explanatory texts on larger screens.

plurals
  1. POExtract values from an array in mongoDB to dataframe using rmongodb
    primarykey
    data
    text
    <p>I'm querying a database containing entries as displayed in the example. All entries contain the following values:</p> <ul> <li><code>_id</code>: unique id of <code>overallitem</code> and <code>placed_items</code></li> <li><code>name</code>: the name of te <code>overallitem</code></li> <li><code>loc</code>: location of the <code>overallitem</code> and <code>placed_items</code></li> <li><code>time_id</code>: time the <code>overallitem</code> was stored</li> <li><code>placed_items</code>: array containing <code>placed_items</code> (can range from zero: <code>placed_items : [],</code> to unlimited amount. </li> <li><code>category_id</code>: the category of the <code>placed_items</code></li> <li><code>full_id</code>: the full id of the <code>placed_items</code></li> </ul> <p>I want to extract the <code>name</code>, <code>full_id</code> and <code>category_id</code> on a per <code>placed_items</code> level given a <code>time_id</code> and <code>loc</code> constraint</p> <p>Example data:</p> <pre><code>{ "_id" : "5040", "name" : "entry1", "loc" : 1, "time_id" : 20121001, "placed_items" : [], } { "_id" : "5041", "name" : "entry2", "loc" : 1, "time_id" : 20121001, "placed_items" : [ { "_id" : "5043", "category_id" : 101, "full_id" : 901, }, { "_id" : "5044", "category_id" : 102, "full_id" : 902, } ], } { "_id" : "5042", "name" : "entry3", "loc" : 1, "time_id" : 20121001, "placed_items" : [ { "_id" : "5045", "category_id" : 101, "full_id" : 903, }, ], } </code></pre> <p>The expected outcome for this example would be:</p> <pre><code>"name" "full_id" "category_id" "entry2" 901 101 "entry2" 902 102 "entry3" 903 101 </code></pre> <p>So if <code>placed_items</code> is empty, do put the entry in the dataframe and if <code>placed_items</code> containts <code>n</code> entries, put <code>n</code> entries in dataframe</p> <p>I tried to work out an RBlogger example to create the desired dataframe. </p> <pre><code>#Set up database mongo &lt;- mongo.create() #Set up condition buf &lt;- mongo.bson.buffer.create() mongo.bson.buffer.append(buf, "loc", 1) mongo.bson.buffer.start.object(buf, "time_id") mongo.bson.buffer.append(buf, "$gte", 20120930) mongo.bson.buffer.append(buf, "$lte", 20121002) mongo.bson.buffer.finish.object(buf) query &lt;- mongo.bson.from.buffer(buf) #Count count &lt;- mongo.count(mongo, "items_test.overallitem", query) #Note that these counts don't work, since the count should be based on #the number of placed_items in the array, and not the number of entries. #Setup Cursor cursor &lt;- mongo.find(mongo, "items_test.overallitem", query) #Create vectors, which will be filled by the while loop name &lt;- vector("character", count) full_id&lt;- vector("character", count) category_id&lt;- vector("character", count) i &lt;- 1 #Fill vectors while (mongo.cursor.next(cursor)) { b &lt;- mongo.cursor.value(cursor) order_id[i] &lt;- mongo.bson.value(b, "name") product_id[i] &lt;- mongo.bson.value(b, "placed_items.full_id") category_id[i] &lt;- mongo.bson.value(b, "placed_items.category_id") i &lt;- i + 1 } #Convert to dataframe results &lt;- as.data.frame(list(name=name, full_id=full_uid, category_id=category_id)) </code></pre> <p>The conditions work and the code works if I would want to extract values on an <code>overallitem</code> level (i.e. <code>_id</code> or <code>name</code>) but fails to gather the information on a <code>placed_items</code> level. Furthermore, the dotted call for extracting <code>full_id</code> and <code>category_id</code> does not seem to work. Can anyone help?</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