I am trying to query data in MongoDB compass GUI based on multiple values using $in
.
My query is this -
{category : {[$in: ['a012','b23c','5870T']}}
I want to query these values with case insensitive option i
. The operator $in
also doesn't accept regex expressions as per documentation.
What would be the correct query with the case insensitive option included?
CodePudding user response:
Try this:
db.mycollection.find({'category':{$in:[/a012/i, /b23c/i, /5870T/i]}})
A couple of ways to do this:
- To limit to the 'word boundaries' of the string, you can use
\b
db.mycollection.find({'category':{$in:[/\ba012\b/i, /\bb23c\b/i, /\b5870T\b/i]}})
- Use the
m
option to match the beginning and end of the string
db.mycollection.find({'category':{$in:[/a012/im, /b23c/im, /5870T/im]}})