To stare pytanie, ale możesz użyć query.replace(/[-[\]{}()*+?.,\\/^$|#\s]/g, "\\$&");
Działa to z aggregate
i matches
:
const order = user_input.replace(/[-[\]{}()*+?.,\\/^$|#\s]/g, "\\$&");
const regex = new RegExp(order, 'i');
const query = await this.databaseModel.aggregate([
{
$match: {
name : regex
}
// ....