Search code examples
rmongodbmongolite

Querying multiple ids from mongodb in R


Want to query multiple ids from mongodb in R using mongolite.

Something equivalent to this code, but in R ( source : How to get multiple document using array of MongoDb id? ).

db.feed.find({
  "_id" : {
    "$in" : 
      [ObjectId("55880c251df42d0466919268"), 
       ObjectId("55bf528e69b70ae79be35006")
      ]
   }
});

I tried with this but it is showing error :

library( mongolite )

chap_rslt_collectn = mongo( 'chapterresults', db = my_db_name, verbose = T, url = my_mongo_url )


tt = chap_collectn$find( "_id" : { "$in" : {'$oid': "5eb32a19c25a56031bdec249", "5e8a5301266dd92b6b96a4f0"} } )

Getting error like this :

> tt = chap_collectn$find( "_id" : { "$in" : {'$oid': "5eb32a19c25a56031bdec249", "5e8a5301266dd92b6b96a4f0"} } )
Error: unexpected ',' in "tt = chap_collectn$find( "_id" : { "$in" : {'$oid': "5eb32a19c25a56031bdec249","

Solution

  • Try the code below. The operator $oid, similar to ObjectId() in mongo, has to be applied to each ID

    qry <- '{"_id" :{"$in": [{"$oid":"5eb32a19c25a56031bdec249"},{"$oid":"5e8a5301266dd92b6b96a4f0"}]}}'
    tt = chap_collectn$find(query = qry)