mongolite filtering with dynamic array in r shiny

550 Views Asked by At

I have a select input with multiple options and my Mongo query

Here is the array if elements:

c<- c("elen","shallen")  
  query1  <-  paste0('{"client": {"$in"["',c,'"]}')

#sales info is the data base
salesinfo$find(fields = '{"store":true,"_id":false}',query = query1)

Error: Invalid JSON object: {"client": [ elen ]}{"client": [ shallen ]}

this isn't working please help me please remember that it is a dynamic array and the values will change

2

There are 2 best solutions below

0
Uttam Gogineni On BEST ANSWER

After extensive research i found a way to solve the issue and i hope my solution will help out guys like me.

q1=paste(shQuote(c, type="cmd"), collapse=", ")

this step is to ensure you print out the array as a string and then use the query

query =paste0('{"store":{"$in":[',q1,']}}')

and the next step would be incorporating it to the query

salesinfo$find(fields = '{"store":true,"_id":false}',query = query)
0
Aaron Soderstrom On

Found this solution while I was trying to answer the same question:

*using jsonlite

query <- paste('{"store" : {"$in":',toJSON(q1, auto_unbox=TRUE),'}}', sep='')