Compose JS Query to trigger MongoDB query

Hi,

I'm building a conditional query that's easier to create syntactically in a JSQuery and was hoping to pass it as additionalScope to a MongoDB query.

I'm able to construct a query object in the JSQuery, which I then render to JSON with JSON.stringify() and set as an additionalScope variable. I access the string variable using the {{ }} notation in the MongoDB query but get an error saying the value is not valid JSON.

If I copy the value from the error message and paste it into the MongoDB query directly, it works, no problem.

JSQuery Contents:

let query = {};
query["changedDate"] = {
  $gte: {
    $date : myDatePicker.startValue
  },
  $lte: {
    $date: myDatePicker.endValue
  }
};

query["title"] = {
  "$regex": titleInput.value, "$options":"i"
};

MQuery.trigger({additionalScope:{myQuery:JSON.stringify(myQuery)}});

MQuery Contents (in the 'Query' text area):

{{ myQuery }}

The JSON it generates and inserts into the MongoDB Query is valid, but I get this error message:

The value given - { "changedDate": { "$gte": { "$date": "2015-04-03T06:27:45.000Z" }, "$lte": { "$date": "2018-06-30T06:00:00.000Z" } },"title": { "$regex": "", "$options": "i" } } must be valid JSON.

That JSON appears to be valid and in fact, can be copied and pasted into the MQuery and run.

I've simplified the example above, but the reason I'm trying to use this approach is that there is conditional logic I'd like to use that's more complex than what I'm able to do directly in the MongoDB query using {{ }} notation.

Is this use of trigger additionalScope and JSQuery-to-MongoQuery unsupported?

Hey there! The first thing that comes to mind for me might be that passing in the stringified object is throwing an error as it’s being parsed as a single string. Could you try {{ JSON.parse(myQuery) }} in the MQuery input?

1 Like

Thanks, Joe. That did the trick!

If I’m following, JSON.parse packs the string back into an object. What’s the {{ }} notation doing behind the scenes that allows that to resolve into a value that Retool can parse?

Glad to hear that fixed it for you! You are correct, JSON.parse turns that ‘object string’ back into an actual object. The {{ }} notation is a flag for our software that the expression inside of those curly brackets is JavaScript and needs to be evaluated in some way. Basically everywhere you write JavaScript inside of a Retool app, it should be wrapped in {{ }}. There are some exceptions: 1) Inside of a Run JS Code query: Everything is evaluated as JS, and you do not need to refer to references inside {{ }}2) Inside of a Transformer: Everything is evaluated as JS, but you do need to reference any external values (i.e. table1.data) inside {{ }}
3) Inside of {{ }} in the app: You can reference any properties in the app (You can see the full list of values and selectors in the left panel of the editor), and everything inside the {{ }} tag will be evaluated as JS

Hi Joe,

Thanks for that detail.

Is Retool’s default evaluation for an object handle inside of curly braces to expand to return the JSON for that object?

How can this JS be written to generate a query with an array of Objects in it?