MongoDB multiselect string array formatting incorrect

I have a multiselect and am trying to use the selected values in a MongoDB aggregation with $in.

The multiselect has three values, "a", "b", and "c".

If I try to use it like this:

"field": {
    "$in":{{myMultiSelect.values}}
},

the editor gives an error saying

The value has to be of type 'object | void', you provided 'string'

and shows the preview output as:

"field": {
    "$in":a,b,c
},

I have tried changing the template to this:

"field": {
    "$in":[{{myMultiSelect.values.map(v => `\"${v}\"`)}}]
},

which produces a correct preview

"field": {
    "$in": ["a","b","c"]
},

but then the database receives the value as this:

"field": {
    "$in": [ [ "\"a\"", "\"b\"", "\"c\"" ] ]
}

There seems to be some mismatch between the preview and what is actually sent to the database when the query is run, since my first attempt does send the correct format to the database, even though it looks wrong in the preview. When changing the multiselect selection, the query also does not run automatically, which might be caused by the same issue.

Hello @Callum!

Thank you for bringing this to our attention!

Your examples are very useful, it seems that the bug is in the preview components parser. I will reproduce the bug and send it to our engineer team to fix.

That's great to hear that the first attempt was able to save the proper data to you database, this would definitely be very confusing for a less technically astute user that is trying to follow along with the preview as their source of truth for what is getting sent in the DB query.

For the issue you mentioned at the end saying that changing the multiselect section did not cause the query to run automatically, could you elaborate further on this?

What do you have set as the query trigger? Is the component 'myMultiSelect' set to query on change, input change or a button press?

I see what you are saying and the preview indicating that the data is incorrect could be preventing the query from firing on every on change event, as it is most likely waiting/expecting valid query inputs before firing as a safety precaution. Hopefully fixing the preview will solve this!

@Callum could you provide some screenshots of the bug/error message in the app when you preview/test/run the query?

If the queries 'Run Behavior' should be set to 'Automatic' by default, which will run the query every time the selected options in .value are selected.

Also try myMultiselect.value as .values will grab every option possible in a multiselect component :smiley:

In the case I tested, the value and values properties contain exactly the same value, an array of three strings.

image
image

In these images you can see that the preview is incorrect. The database receives the query correctly as:

"field": {
  "$in": ["a", "b", "c"]
}

The query run behaviour is set to automatic, but doesn't seem to want to run while it has this preview error.

Hey @Callum!

I think my team and I have found the issue.

It looks like when the query is missing a pair of curly braces that would make valid JSON, it defaults to changing the data to a string which triggers the preview error as shown here

But when we add in an outer level curly braces the data is properly registered as JSON, the preview error is resolved and the query functions properly. As shown below, let me know if you are able to replicate this!

Unfortunately, while your reproduction produces the same issue that I am experiencing, it is not the same cause. I have triple checked my JSON syntax, and have confirmed that just replacing {{myMultiSelect.value}} with [] produces value JSON with no errors.


On a possibly unrelated note, I have just noticed that when setting the value of a multiselect option, I am getting an error saying that I provided an arrray when it's clearly a string:
image

Perhaps this is a root cause?

I found this thread which shows the same issue, saying that the default value field is the problem:

My default value looks like this:
image


Edit

Ok so I have discovered another issue that seems to be causing the problem. In my query I have the following:

image

This gives an error.

If I change the $in to this:

image

the error is resolved.

However, if I instead remove the $gte to make it this:

image

it also resolves the error.

So, somehow removing either of these parts of the query resolves the issue. I can't understand this, but maybe you will.

Hey @Callum,

Yes you are correct the error shown where the value is mislabeled is as array is a bug in our linter and I will pass that along to our engineer team to fix. As long as you put in values to the multiselect the displayed error should not be blocking queries from running.

On the issue of the Mongo query, I believe you will need to format the query differently to have it filter by multiple parameters.

From what I have tested out, you can only specify one parameter inside of $match. Maybe in the Mongo docs there are more details on how to match by created at and $field/$in as well.

I'm confident in my ability to write a valid MongoDB query. The error I am seeing is not a MongoDB one but a Retool one.

Retool is happy for me to have either of these double curlies, but not both - very strange.