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:


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:


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


the result is


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


I get


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.