An odd JS + Mongo error

Hey team,

Here's what I did and the problem I'm facing.
I have a multi-select drop-down named userSelector which is based on a SQL query. The display is the name while the value is ID.

Then I'm passing this in a mongo query as:

{
    "$match":{
      "userId":{"$in":[{{userSelector.value}}]}
    }
  }

I have to pass the values inside the [] because otherwise, the mongo query is invalid as $in needs an array input.
When I do this the mongo query built in run time has the value:

{"$in":[["00bff823-7500-4366-8b8c-c8e6ec025d54","01984813-5ad3-451a-b765-db60312b55b8","01d8823e-1400-4bcb-a6a7-b652c1536b4f"]]}

This won't work as it's an array in an array.
If I change it to

{
    "$match":{
      "userId":{"$in":[{{userSelector.value**.join()**}}]}
    }
  }

the result is

{"$in":["0172b29e-7a0a-497f-8b22-c582094fb657,01984813-5ad3-451a-b765-db60312b55b8"]}

This won't work as the inner values are not springs themselves but one long string.
If I change it to

{"$in":[{{userSelector.value**.join("\",\"")**}}]}

I get

{"$in":["0172b29e-7a0a-497f-8b22-c582094fb657\",\"01984813-5ad3-451a-b765-db60312b55b8"]}

which is also wrong because the escape characters are not treated correctly.

If I join and split, then again the result is an array in an array. And if I map values to be part of string, I end up with untreated escape chars.

I tried editing the source dropdown's ID value itself to be wrapped in quotes but that too didnt work.

What do I do?

Hey @arijeet!

Typically I would expect the query to work without you needing to add in the [] since Retool will do that when the query executes:

Curious to know what error you're seeing when you run:

{
"userId":{"$in":{{userSelector.value}} }
}

Thank you so much @Kabirdas.
I did try that before, that's how I started but it just didn't work then. Works like a charm now. Maybe a brace here and there got missed before.

Now I recall @Kabirdas, earlier I was using the query all the time and it was causing an error when the selector had no value as the query became "$in":[].
Now, I've changed the logic to use the query only when there's a value. If it's empty another query without the match filter is used.