The request (req.query) I got has this format:
{ duration: { gte: '5' }, difficulty: 'easy' }
To make it work in MongoDB, the final result should be as below
{ duration: { $gte: 5 }, difficulty: 'easy' }
This is the code I'm using
const queryObj = { ...req.query };
let queryStr = JSON.stringify(queryObj);
queryStr = queryStr.replace(/\b(gte|lte|gt|lt)\b/g, (match) => `$${match}`);
However, the output is { duration: { '$gte': '5' }, difficulty: 'easy' }
so it didn't work.
{ duration: { $gte: 5 }, difficulty: 'easy' } : return every matched records
{ duration: { '$gte': '5' }, difficulty: 'easy' }: return 0 record.
My question is: How can i remove the '' sign so it can be used for query string?