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}}
@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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With