在rmongodb查询中添加$提示

时间:2015-07-23 22:21:41

标签: r rmongodb

有没有人知道如何在使用rmongodb包时向mongo查询添加hint参数?注意:hint现已弃用。

目前,我使用mongo.find.all查询是为了简单而不是单独的cursorbuffer命令。

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)

在mongo中,查询看起来像这样,没有上面完整示例中的所有字段:

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

当在Mongo中使用时,提示明显提高了查询性能,因此在正在进行的R过程中实现它会很有用。

当前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 

1 个答案:

答案 0 :(得分:1)

执行bit of research后,我发现您可以将提示作为$ hint参数提供给查询。 在mongodb中它看起来像这样:

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

你可以在rmongodb做同样的事情。 将您的查询更改为以下内容:

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

我对此进行了测试,它可以在我的测试数据集上运行而不会出错。如果这对您有用,请告诉我。