Search code examples
rmongodbrmongo

mongolite filtering with dynamic array in r shiny


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


Solution

  • 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)