MongoDB: $regex for array of objects

Hello,

I would like to seek your assistance regarding a question regarding the use of MongoDB with Meteor. I am looking to use a regular expression (regex) to search for documents within a MongoDB database. However, the particularity of my query is that I wish for this regular expression to be applied to an array of objects within the documents.

Do you have any advice or examples to share on how to implement this search effectively within the scope of my Meteor application?

Thank you in advance for your help.

Best regards,

Collection.find(
        {
            $and: [
                {id: {$regex: new RegExp(requestId, "i")}},
                {host: {$regex: new RegExp(host, "i")}},
                {uri: {$regex: new RegExp(url, "i")}},
                {'messages.id': {$regex: new RegExp(ruleId, "i")}},
                {'messages.data': {$regex: new RegExp(data, "i")}},
                {'messages.msg': {$regex: new RegExp(message, "i")}},
                {'messages.severity': {$regex: new RegExp(severity, "i")}},
            ]
        },
        {
            limit: 100, skip: (page - 1) * 100,
            $orderby: { requestDate : 1 }
        },
    ),

for example messages.severity

doublem

This looks wrong to me. I don’t know what is business need here but the translation into data models seems wrong.

The “i” in your regex queries (case insensitive) means that indexes cannot be used and so the queries will be very slow. This is an anti-pattern in Mongo. There are some workarounds. One solution is to use case-insensitive indexes and a query with a suitable collation strength. See more here. Another, sometimes simpler option, is to normalize the relevant strings in Mongo at write time and then just run case sensitive queries on the normalized fields.

Having said that, before dealing with those issues I think it’s more important to assess if you really need such a query with so many regexes in the first place. For all practical purposes the answer seems to be no. Probably the data can be modelled in a better way.