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.

1
  • could you add a sample of the data?, it seems you do the lookup from the same collection "orders"? Commented Apr 28, 2020 at 21:59

2 Answers 2

9

$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",
      }
    }
  ]
}}
Sign up to request clarification or add additional context in comments.

5 Comments

How can do fuzzy matching inside expr , because I'm using $and inside $expr
Above query will satisfy the $and condition. BTW what is your mongodb version?
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 this is working mongoplayground.net/p/kwMV8iHIVQK
Pls show your actual documents and query then only I can help.
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

3 Comments

what if i want to pass "ab" as a variable like "$$name" not a hardcoded string
Could you add the whole query to the question?
and a sample of your data, and the expected output too

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.