Reputation: 93
I have listOfintrests = ["T-shirts","Jeans","Trousers","watches"]
which I'm searching with courses collection, but I'm getting an error
Cast to string failed for value "{ '$regex': /^T-shirts,Jeans,Trousers,watches$/, '$options': 'i' }" (type Object) at path "category" for model "course"
const isCourse = await Courses.find({ category : {$in : { $regex: new RegExp(`^${listOfintrests}$`), $options: 'i' }} });
What am I doing wrong here? Please help me thanks in advance.
this is the course collection:-
Upvotes: 1
Views: 26
Reputation: 3910
You need concat the array elements with regex |
or operator
const isCourse = await Courses.find({ category : new RegExp(listOfintrests.join('|'), 'i') }
Upvotes: 1