Search code examples
node.jsregexmongodbmongodb-queryregex-lookarounds

AND operator in mongodb regex


In a mongo database with an array field titled interests, when i need to look for people interested in either judo OR mma, i write the query like this:

{interests : {$regex: "judo|mma", $options: 'i'}}

How do i find people interested in judo AND mma?

Note: I've tried the following and various variations of these but none seem to work :

{interests : {$regex: "(?=judo)(?=mma)", $options: 'i'}},
{interests : {$regex: "(?=.*judo)(?=.*mma)", $options: 'i'}}

Thanks.


Solution

  • something like this should work :

    {interests: {$all: [/^judo$/i, /^mma$/i]}