Collection.find with RegExp delivers unexpected results

Hi everyone,

I’m facing some issues with querying MogoDb-documents via RegEx:
I’d like to find all documents of a user (and the appropriate sessions) where at least one attribute (title and/or comment) contains some words (case-insensitive). With using online RegEx-tester apps I found the expression ‘^(?=.*bla)(?=.*test).+’ with the option ‘i’ for finding ‘bla’ and ‘test’ in at least one of the attribute values.

My code for finding the documents (sessionIds is an array with identifier):

const partialMatchRegex = new RegExp('^(?=.*bla)(?=.*test).+', 'i');
AgendaItems.find(
            { sessionId: { $in: sessionIds } },
            {
                $or: [
                    { title: partialMatchRegex },
                    { comment: partialMatchRegex }
                ]
            }
        ).fetch();

When executing this query, I also get result documents where only the title attribute matches only one word and the value of comment doesn’t match any word. Am I missing here something? I’d appreciate any helpful hint.
Thank you very much in advance!
Thomas

1 Like