3

R version: 3.4.2, Ubuntu 16.04 LTS (4.10.0-37-generic), Mongo DB version 3.4.10, Mongolite v1.2.

Based off of Mongolite manual, I am trying to add an index based off my data in R. I would like this index to take up the value based on a variable's value in the data.

I have created my Mongo DB, and access it in R using:

margo<-mongo(collection="test",db="test1",url="mongodb://localhost", verbose=TRUE)

I was able to use the variable name approach to run find queries as per other answers on stackoverflow (updating an entry in mongolite)

margo$find(query=paste0('{"FileName": "',test1$FileName,'"}'))

However, I cannot figure out how to add a variable-based index. For example if my variable is val=1 and I am trying to create an index named "trya", then when I try

margo$index(add=paste0('{"trya":"', val,'"}'))

I get an error message:

Error: Unknown index plugin '1' Obviously, if I do

margo$index(add=('{"trya": 1}'))

this works just fine. Newbie to mongo usage, so my apologies if I am missing something obvious about variable formatting.

Update: I was able to use

margo$index(toJSON(list("trya" = val),auto_unbox=TRUE)) 

to add numeric indices (following from github mongolite report on importing compound indices). If val is a string variable, this does not work (same error message).

Vinnie
  • 35
  • 4
  • after your update, are you still having issues? – SymbolixAU Nov 08 '17 at 21:03
  • The more I think about, I cannot see why/how a string can be an indexer. So in that sense, yes, I don't have any more issues on that subject. But I am still not sure how to use "add" with a variable indexer. Is that not possible? Is the way I resolved it, the only way around the problem? – Vinnie Nov 09 '17 at 22:40

0 Answers0