2

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

kenlukas
  • 3,616
  • 9
  • 25
  • 36

2 Answers2

1

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

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

*using jsonlite

query <- paste('{"store" : {"$in":',toJSON(q1, auto_unbox=TRUE),'}}', sep='')
Aaron Soderstrom
  • 599
  • 1
  • 6
  • 12