Find document $in with string array Mongoose match all the text cases

Issue

I am currently working with MERN stack for an E-commerce website

I am finding a syntax in Mongoose like:

Product.find({ brands: {$in : ["Brand1", "brand2", "BRAND3"], $option: 'match all cases'}})

I expect the result will match all Products that have brand1 or Brand1 or BRAND1, Brand2 or brand2, and so on…

How can I solve it in Mongoose v6.0.13?

Solution

If want to execute in mongoDB compass use below query

Product.find({brands :{$in:[new RegExp('brand1', 'i'), new RegExp('brand2', 'i')]}} )

If using mongoose use

Product.find({ brands:{$in:[{ $regex: new RegExp('brand1', "i") }, { $regex: new RegExp('brand2', "i") }] })

Answered By – Priyanka Sankhala

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published