Query Builder - Format Query to Mongo

Hey Retool fam,

I am looking to use the Query Builder Component for my Mongo Database.

In the React Query Builder Docs they refer to exporting the query to Mongo with formartQuery().

Can someone help me figure this out?

Hey Todd - welcome to the forum!
Doesn't seem like it's built-in to the Retool adapted component. Your best option will most likely be to either make a custom component based on React QueryBuilder (which the Retool component is also based on), or extract the code from the React library to export to MongoDB flavour, and then input the querySelector.value object to get the MongoDB query.

I hope this helps you in the right direction!

1 Like

Hey @jonathanbredo - Thank you so much for the reply & the advice!

Can anyone please show or describe, how to load react-query-builder’s formatQuery() in app scripts and use it, for example in a on button-click script? I tried it and it never worked.

AFAIK you only need the formatQuery() functionality, the UI already exists in the queryBuilder component.

Hey @Istvan_Palfy! Welcome :slight_smile:

Would you mind sharing a screenshot of what you currently have? Have you imported the library into Retool?


Took me too much time already, deleted it.

I just want to use (the original) react-query-builder's formatQuery(query, 'mongodb') function (which is said to be in utils) so I can take queryBuilder.value and convert it into a mongodDB-query like mentioned here: Export | React Query Builder

This (along with formatQuery(query, 'sql) ) appears to be an extremely common use case, since manually converting queryBuilder's resulting json into (any) query is a ton of work. Otherwise retool 's integrated queryBuilder component is kind of useless.

That's why a "mini-tutorial" or a step by step description could be helpful.

  • Which is the right package (currently endless searching)
  • adding (what exactly) to the apps ''scripts and styles/Library"
  • adding it to "window." in Javascript. (Didn't work at all)
  • reference it anywhere in the apps code by simply calling the desired "...query = formatQuery(queryBilder1.value, 'mongodb')"

I mean: Didn't anyone do this before?

PS: I just found out, there is a queryBuilder1.asSQL property. Having the same for mongoDB would be super nice.

I've been trying to get this to work a few different ways and have also not been successful. I filed an internal feature request to get this officially supported!

In the meantime, I wonder if you could use JS to format the query (if it's a simple enough query). What kind of query are you writing? :slight_smile:

Thank You very much!

My queries might be too hard to convert manually, since I also have to query for coordinates in nested fields and I will have plenty and/or scenarios. I also tried to grab the code from GitHub, but there were too many dependencies.

Your Devs however could easily integrate those as they did with .asSQL.

Let’s see, I will add simple form, so we can query at least a few predefined scenarios.

Again: Thank you for your time.

Understood. I'll keep you updated if this feature gets prioritized. In the meantime, hopefully the simple form works for you :crossed_fingers: And let me know if there's anything else we can help with!