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?