title: R and MongoDB: Array is stored as an object with indexes as keys
body: <p>I'm pulling in JSON data from a provider and adding it to mongodb using R. I plan on using R and Shiny to display the data in the future. I'm currently having an issue right now though where I place the data into a JSON Object and insert it into MongoDB. It adds the object but places the data one level lower than where I would really like it.</p>
  
  <p>Here is how the data comes in:</p>
  
  <pre><code>prettify(jsonKill)
  [
      {
          "id" : {
              "timestamp" : 1409785080,
              "machine" : 11966932,
              "pid" : 3144,
              "increment" : 11720074,
              "creationTime" : "2014-09-03T22:58:00Z"
          },
      ...
  ]
  </code></pre>
  
  <p>Here is my code that adds it to mongodb:</p>
  
  <pre><code>library('jsonlite')
  library('rmongodb')
  
  m &lt;- mongo.create()
  ns &lt;- 'database.collection'
  killObject &lt;- fromJSON('http://omitted.because.nda:8000/api/omit')
  x &lt;- nrow(killObject)
  for(i in 1:x){
    jsonKill &lt;- toJSON(killObject[i:i,])
    bson &lt;- mongo.bson.from.JSON(jsonKill)
    mongo.insert(m, ns, bson)
    paste("Inserting Record: ", i)
  }
  cursor &lt;- mongo.find(m, ns, bson)
  while(mongo.cursor.next(cursor)){
    value &lt;- mongo.cursor.value(cursor)
    list &lt;- mongo.bson.to.list(value)
    str(list)
  }
  </code></pre>
  
  <p>Here is the result:</p>
  
  <pre><code>{
      "_id" : ObjectId("54081299d5ec83d046d05766"),
      "1" : {
          "id" : {
              "timestamp" : 1409756219,
              "machine" : 2364985,
              "pid" : 9076,
              "increment" : 1079972,
              "creationTime" : "2014-09-03T14:56:59Z"
          },
      ...
  }
  </code></pre>
  
  <p>What I'm aiming for is to do <code>db.collection.find({"id.pid" : $gt1})</code> or an index with <code>mongo.index.create(m, ns, {"id.pid"}, mongo.index.unique)</code> something to that effect, not necessarily the <code>id</code> key, but one or more of the keys not displayed here.</p>