3

My doc looks as follows

doc = {
    name: 'abc',
    age:20
}

and my query looks like

{ $expr: {$and:[{ $gt:[ "$age",  10 ] },
                { $regex:["$name",'ab']}
               ]
          }
} }

But it's not working and I get an error

Unrecognized expression '$regex'

How can I make it work?

My original query looks like this

db.orders.aggregate([{
$match: {}},
{$lookup: {
    from: "orders",
    let: {
        "customer_details": "$customerDetails"
    },
    pipeline: [
        {
            $match: {
                $expr: {
                        $and: [
                                { $or: [
                                                {
                                                $eq: ["$customerDetails.parentMobile","$$customer_details.parentMobile"]
                                                },
                                                {$eq: ["$customerDetails.studentMobile","$$customer_details.parentMobile"]
                                                },
                                                {$eq: ["$customerDetails.studentMobile","$$customer_details.parentMobile"]
                                                },
                                                {$eq: ["$customerDetails.studentMobile","$$customer_details.studentMobile"]
                                                }
                                            ]
                                        },
                                {$eq: ["$customerDetails.zipCode","$$customer_details.zipCode"]},
                                {$eq: ["$customerDetails.address","$$customer_details.address"]}
                        ]
                    }

            }
        }],
    as: "oldOrder"
}
}])

I want to use regex for matching address.

Any help will be greatly appreciated. Thanks in advance.

Ashh
  • 44,693
  • 14
  • 105
  • 132
ganesh
  • 2,024
  • 16
  • 17

2 Answers2

7

$regex is a query operator you cannot use inside $expr because it only supports aggregation pipeline operators.

{
  "$expr": { "$gt": ["$age", 10] } ,
  "name": { "$regex": "ab" }
}

If you have mongodb 4.2, you can use $regexMatch

{ "$expr": {
  "$and": [
    { "$gt": ["$age", 10] },
    {
      "$regexMatch": {
        "input": "$name",
        "regex": "ab", //Your text search here
        "options": "i",
      }
    }
  ]
}}
Ashh
  • 44,693
  • 14
  • 105
  • 132
  • How can do fuzzy matching inside expr , because I'm using $and inside $expr – ganesh Apr 28 '20 at 20:15
  • Above query will satisfy the `$and` condition. BTW what is your mongodb version? – Ashh Apr 28 '20 at 20:17
  • problem with $regexMatch is , I'm using this $expr inside match of `$lookup` , inside $lookup i'm using variable declaration and using those variable inside $regexMatch as `$regexMatch: {input :"$name" , regex:"$$NAME"}` where `NAME` is variable declared in $lookup – ganesh Apr 28 '20 at 20:33
  • @ganesh this is working https://mongoplayground.net/p/kwMV8iHIVQK – Ashh Apr 28 '20 at 21:45
  • 1
    Pls show your actual documents and query then only I can help. – Ashh Apr 29 '20 at 08:45
5

If your mongoDB version is 4.2, then you can use $regexMatch

try this

db.collection.find({
  $expr: {
    $and: [
      {
        $gt: [
          "$age",
          10
        ]
      },
      {
        $regexMatch: {
          input: "$name",
          regex: "ab"
        }
      }
    ]
  }
})

check this Mongo Playground

Mohammed Yousry
  • 2,134
  • 1
  • 5
  • 7