Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mongolite and R

Tags:

r

mongolite

I have the code below, why it is not working? Thanks.

library(mongolite)
library(jsonlite)

id <- c('35325073','35325078')

id_list_JSON <- toJSON(id)

test3 <- m1$find('{"_id": {"$in": id_list_JSON}}', fields = '{"_id" : 1, "f.fid" : 1, "ud" : 1}')

Error: Invalid JSON object: {"_id": {"$in": id_list_JSON}}

like image 617
Chris Lin Avatar asked Mar 10 '26 13:03

Chris Lin


1 Answers

@Marc B is correct in that you have to build the string to use the values contained in id_list_JSON.

In R you can use paste0 to do this

test3 <- m1$find(paste0('{"_id": {"$in": ', id_list_JSON, '}}'), 
                 fields = '{"_id" : 1, "f.fid" : 1, "ud" : 1}')

Where the paste0 line gives

paste0('{"_id": {"$in": ', id_list_JSON, '}}')

# "{\"_id\": {\"$in\": [\"35325073\",\"35325078\"]}}"

Which should then work in your query.

like image 111
SymbolixAU Avatar answered Mar 12 '26 02:03

SymbolixAU



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!