Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unrecognized expression '$regex inside $project

Hi im try to group all elements by _id, that will it same in all cases

db.movies.aggregate([
{
    $project:{
        _id:{
            award:{$cond:[
                {$and:[{awards:{$regex:/win/}}]}
                ]}
        },
    imdb:1
    }
},
{
    $group:{
        highest_rating:{$max:'$imdb.rating'},
        lowest_rating:{$min:'$imdb.rating'},
        average_rating:{$avg:'$imdb.rating'},
        deviation:{$stdDevPop:'$imdb.rating'}
    }
}
])

But the response its

{
"message" : "Unrecognized expression '$regex'",
"operationTime" : "Timestamp(1560994467, 1)",
"ok" : 0,
"code" : 168,
"codeName" : "InvalidPipelineOperator",
"$clusterTime" : {
    "clusterTime" : "Timestamp(1560994467, 1)",
    "signature" : {
        "hash" : "++GYj/cVU+Xk2bYmDYAowZfrhWw=",
        "keyId" : "6661714504839069697"
    }
},
"name" : "MongoError"

} What im doing wrong?

like image 985
Daniel ORTIZ Avatar asked Nov 24 '25 21:11

Daniel ORTIZ


1 Answers

For people who found this (like me), the issue was corrected in May/2019 (https://jira.mongodb.org/browse/SERVER-11947) and you should use $regexMatch instead

db.coll.aggregate([{
    $project: {
        region: {
            $cond: {
                if: {
                    $regexMatch: {
                        input: “$phoneNumber”,
                        regex: “^212.*$”,
                    }               
                }
                 then: "New York",
                else: "Somewhere Else"
            }
        }
    }
}])
like image 104
guiwunsc Avatar answered Nov 27 '25 15:11

guiwunsc



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!