Mongolite query returns invalid json because of quotes

358 Views Asked by At

I have a large amount of articles in a mongo db and I want to load only a part of them in a R session (using mongolite), as I already have a subset of all the titles (there is a field "title" in my collection). Thus, I'd like to use $in in order to load the subset of articles I want, with something like:

mongo.connection$find(paste0('{"title": {"$in": ["',
                             paste(titles, collapse = '", "'),
                             '"]}}'))

where titles is a vector of the titles of the articles I want to load.

Unfortunately, in the titles, there are some ', " and `, so the string sent to mongo is broken. I have tried to use \" in many different ways but I can't solve the issue.

Finally, I know I could change the titles directly in the mongo db by unscapping the quotes directly in the titles of the collection, but I'd rather not if possible.

Do you know if there is a clean way to query this subset of articles?

0

There are 0 best solutions below