Use a list of ObjectIds from a Text Input Field

I want to use text input to pass a list of ObjectIds so I can build multiple queries by using the list. I wonder how I can use the textInput value to turn into an array of objects.

Are the IDs you are going to input in the text input field being added all at once? Or one at a time? There are multiple ways to build an array - use temporary state, a js query, etc.
Can you provide more detail about your goal and what the approach and data you are looking to do?

1 Like

I want to enter them at once. The id used in every collection in our Mongodb collection. So I want to query data from several tables that _id in that table is in the list I enter in the text field.

So in the text input field you want to be able to add:
id1, id2, id3, id4... Correct? And then iterate through each to query several tables using each ID one query at a time?

Yes for the input field.

But I want to build a bulk query something like in attached image.

I couldn't find a way to populate an array of object ids through query builder.

Do you have a set structure for the array you want to build? If not and the data doesn’t need to be shared across apps you can creat that structured array and set it to a temp state and then iterate through your text Input values and insert where needed. I will post some code as soon as I can.

As I am new to Retool, i don't know what a set structure is. But no I won't share the data across other apps in Retool. Thanks a lot for your help btw.

Baran,
I will be able to post either later tonight or tomorrow at the latest. The reason I ask is because the array needs to be built in a way so you can iterate through it in a loop. The key here is the keep the array as flat as possible. This means a simple list like
[id1,id2…] and not like [id1:{id2:id3}] if possible. This would make building the array a lot easier but if the structure needs to be multidimensional running a loop through the additional dimension could still work. I will post code as soon as I can

Baran,
I made the assumption that all ids would be added in the textInput field.
So I added a button that when clicked will rnun the following js code:

var idsToQuery = [];
idsToQuery = textInput1.value.split(",");
var runQuery = "";
for(i=0;i<idsToQuery.length; i++){ 
runQuery += " Run queries here for each " + ' '+ idsToQuery[i] + ' '; 
}
return runQuery;

I hope this works/helps

I tried to build some part of the query with a JS query. and pass that to a Resource query. Although the query throws an error when I run it, it works when I copy&paste the output of the Js query to the Query field.

Try passing in {{getIdList.data.value}}. Not at my machine at the moment but when I am I will take another look

Hi @baran!

You might want to try using JSON.parse. In your JS query you can do something like

return JSON.parse('{"_id" : {"$in": [{"$oid":"your_oid_here"}]}}')

Or, you can pass {{ JSON.parse(getIdList.data) }} to your MongoDB query.

As a side note: you might also try using the additionalScope object here. In your JS query you can trigger your MongoDB query with

var idList = idListInput.value.split(",").map(id => ({$oid: id}));
userQuery.trigger({additionalScope: {idList}}); 

and then in your MongoDB query you can write

{ _id : { $in : {{idList}} } }

Let me know if any of the suggestions here work :slightly_smiling_face: