You could either change the way you construct your query, to pass it as JSON an then use JSON.parse() / JSON.stringify()
// Note the necessary double quotes around the keys
const jsonFilter = '{ "stamps": { "$gte": "2020-11-06 06:42:25.000+01:00", "$lte": "2020-11-06 09:52:25.000+01:00" } }'
const query = JSON.parse(jsonFilter)
Message.find(query, callback)
Or you could introduce another dependency like mongodb-query-parser that can parse the string for you.
const parser = require('mongodb-query-parser')
// Note it still needs double quotes around the dates to run properly
const filter = '{stamps:{$gte: "2020-11-06 06:42:25.000+01:00", $lte: "2020-11-06 09:52:25.000+01:00" } }'
const query = parser(filter)
Message.find(query, callback)
https://runkit.com/5fbd473b95d0a9001a2359b3/5fbd473b98492c001a8bba06
If you can't include the quotes around the values in the string, you can use a regular expression to add them. But keep in mind that this regex is tailored to match this exact date format. If the format can change or you want to also match other data types, you should try to include them in your filter string from the start.
const parser = require("mongodb-query-parser")
const filter = '{stamps:{$gte: 2020-11-06 06:42:25.000+01:00, $lte: 2020-11-06 09:52:25.000+01:00} }'
const filterWithQuotedDates = filter.replace(/(\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}\.\d{3}(\+\d{2}:\d{2})?)/g, '"$1"')
parser(filterWithQuotedDates)
Message.find(query, callback)
https://runkit.com/5fbd494bcd812c0019b491fb/5fbd4951ebe43f001a5b590a
It should be noted that a common use-case is to pass a MongoDB query via URL params and that there are special packages for that:
JSON.parse(filter)?