Search code examples
rrmongodb

Add a $hint to rmongodb query


Does anyone know how to add a hint argument to a mongo query when using the rmongodb package? Note: hint is now deprecated.

Currently I'm using the mongo.find.all query for it's simplicity rather than separate cursor, buffer commands.

mongo.find.all(mongo = mongo, 
               ns = "ops.weather", 
               query = "{\"where.zip_code\":\"60603\", \"what.currently.time\":{\"$gte\":1430936418}}",
               sort = mongo.bson.empty(), 
               fields = list(what.currently.time = 1L,
                             what.currently.precipIntensity = 1L,
                             what.currently.temperature = 1L,
                             what.currently.windSpeed = 1L,
                             what.currently.windBearing = 1L,
                             where.zip_code = 1L,
                             where.latitude = 1L,
                             where.longitude = 1L,
                             what.observation_type = 1L),
               limit = 0L,
               skip = 0L,
               options = 0L,
               data.frame = TRUE)

In mongo the query would look something like this, without all the fields in the full example above:

db.weather.find({"where.zip_code" : "60603","what.currently.time" : {"$gte" : 1430936418}}).hint("where.zip_code_1_what.currently.time_1")

The hint clearly improves query performance when used in Mongo, so it would be useful to implement in the ongoing R process.

Current sessionInfo()

> sessionInfo()
R version 3.2.1 (2015-06-18)
Platform: i386-w64-mingw32/i386 (32-bit)
Running under: Windows 7 (build 7601) Service Pack 1

locale:
[1] LC_COLLATE=English_United States.1252  LC_CTYPE=English_United States.1252   
[3] LC_MONETARY=English_United States.1252 LC_NUMERIC=C                          
[5] LC_TIME=English_United States.1252    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] rmongodb_1.8.0

loaded via a namespace (and not attached):
[1] plyr_1.8.3       tools_3.2.1      rstudioapi_0.3.1 Rcpp_0.12.0      jsonlite_0.9.16 

Solution

  • After doing a bit of research, I found that you can supply the hint as a $hint argument to the query. In mongodb it would look like this:

    db.weather.find( {$query: {...}, $hint: {"where": 1, ...}})
    

    And you can do the same in rmongodb. Change you query to something like this:

    query = 
      "{\"$query\":
         {\"where.zip_code\":\"60603\", \"what.currently.time\":{\"$gte\":1430936418}},
        \"$hint\":
          \"where.zip_code_1_what.currently.time_1\"}"
    

    I tested this and it worked on my test dataset without errors. Let me know if this worked for you.