I have the following Mongoose Schema -
const myTableSchema = new Schema({
Category: { type: String, required: false },
Tag: { type: String, required: false },
createdAt: { type: Date, 'default': Date.now },
updatedAt: { type: Date, 'default': Date.now },
});
Note that, both of them are String. I was trying to do a query like the following -
localhost:1971/api/myTable?Category[$like]=Javascript
I have rows with Javascript in Category column. But getting the following error-
{
"name": "GeneralError",
"message": "Can't use $like with String.",
"code": 500,
"className": "general-error",
"data": {},
"errors": {}
}
I know its been a while since this was discussed, but I've had a similar question and the code provided by @Daff helped me a lot, but it contains an error.
query[field].$like
is checked for presence, but then query[field].$search
is attached to the final query.
The correct code representation (if following the original question) should be:
exports.searchRegex = function () {
return function (hook) {
const query = hook.params.query;
for (let field in query) {
if(query[field].$like && field.indexOf('$') == -1) {
query[field] = { $regex: new RegExp(query[field].$like) }
}
}
hook.params.query = query
return hook
}
}