Suman
Suman

Reputation: 93

How to check with multiple fields and check case senstive in mongodb

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:-

enter image description here

Upvotes: 1

Views: 26

Answers (1)

Rajdeep D
Rajdeep D

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

Related Questions