MongoDB中$ cond内的'like'或$ regex查询

时间:2021-11-28 11:29:09

Please go through this question of mine:
MongoDB $group and explicit group formation with computed column

请仔细阅读我的这个问题:MongoDB $ group和带有计算列的显式组形成

But this time, I need to compare strings, not numbers.The CASE query must have a LIKE:
CASE WHEN source LIKE '%Web%' THEN 'Web'

I then need to group by source. How to write this in Mongo? I am trying the following but not sure if $regex is supported inside $cond. By the way, is there a list of valid operators inside $cond somewhere? Looks like $cond isn't very fond of me :)

但这一次,我需要比较字符串,而不是数字.CASE查询必须有一个LIKE:CASE WHEN来源LIKE'%Web%'那么'Web'然后需要按来源分组。如何在Mongo中写这个?我正在尝试以下但不确定$ cond中是否支持$ regex。顺便问一下,$ cond里面有一个有效的运算符列表吗?看起来$ cond不是很喜欢我:)

db.Twitter.aggregate(
    { $project: { 
        "_id":0,
        "Source": {
            $cond: [
                { $regex:['$source','/.* Android.*/'] },
                'Android', 
                { $cond: [
                    { $eq: ['$source', 'web'] }, 'Web', 'Others'
                ] } 
            ]
        }
    } }
);

There're many other values that I need to write in there, doing a deeper nesting. This is just an example with just 'Android' and 'Web' for the sake of brevity. I have tried both with $eq and $regex. Using $regex gives error of invalid operator whereas using $eq doesn't understand the regex expression and puts everything under 'Others'. If this is possible with regex, kindly let me know how to write it for case-insensitive match.

我需要在那里写下许多其他值,进行更深层次的嵌套。为简洁起见,这只是“Android”和“Web”的一个例子。我用$ eq和$ regex试过了。使用$ regex会产生无效运算符的错误,而使用$ eq则无法理解正则表达式并将所有内容置于“其他”下。如果这可以使用正则表达式,请告诉我如何编写它以区分大小写匹配。

Thanks for any help :-)

谢谢你的帮助 :-)

2 个解决方案

#1


1  

Well, it still seems to be not even scheduled to be implemented :( https://jira.mongodb.org/browse/SERVER-8892

好吧,它似乎甚至没有安排实施:( https://jira.mongodb.org/browse/SERVER-8892

I'm using 2.6 and took a peek on 3.0, but it's just not there.

我正在使用2.6并看一眼3.0,但它不存在。

There's one workaround though, if you can project your problem onto a stable substring. Then you can $substr the field and use multiple nested $cond. It's awkward, but it works.

但是,如果您可以将问题投影到稳定的子字符串上,则有一种解决方法。然后你可以$ substr字段并使用多个嵌套的$ cond。这很尴尬,但它确实有效。

#2


0  

Maybe you can try it with MapReduce.

也许你可以尝试使用MapReduce。

var map = function()
{
    var reg1=new RegExp("(Android)+");
    var reg2=new RegExp("(web)+");

    if (reg1.test(this.source)){
        emit(this._id,'Android');
    }
    else if  (reg2.test(this.source))
    {
        emit(this._id,'web');
    }

}

var reduce = function (key,value){
    var reduced = {
        id:key,
        source:value
    }    
    return reduced;

}

db.Twitter.mapReduce(map,reduce,{out:'map_reduce_result'});

db.map_reduce_result.find();

You can use JavaScript regular expresions instead of MongoDB $regex.

您可以使用JavaScript常规表达式而不是MongoDB $ regex。

#1


1  

Well, it still seems to be not even scheduled to be implemented :( https://jira.mongodb.org/browse/SERVER-8892

好吧,它似乎甚至没有安排实施:( https://jira.mongodb.org/browse/SERVER-8892

I'm using 2.6 and took a peek on 3.0, but it's just not there.

我正在使用2.6并看一眼3.0,但它不存在。

There's one workaround though, if you can project your problem onto a stable substring. Then you can $substr the field and use multiple nested $cond. It's awkward, but it works.

但是,如果您可以将问题投影到稳定的子字符串上,则有一种解决方法。然后你可以$ substr字段并使用多个嵌套的$ cond。这很尴尬,但它确实有效。

#2


0  

Maybe you can try it with MapReduce.

也许你可以尝试使用MapReduce。

var map = function()
{
    var reg1=new RegExp("(Android)+");
    var reg2=new RegExp("(web)+");

    if (reg1.test(this.source)){
        emit(this._id,'Android');
    }
    else if  (reg2.test(this.source))
    {
        emit(this._id,'web');
    }

}

var reduce = function (key,value){
    var reduced = {
        id:key,
        source:value
    }    
    return reduced;

}

db.Twitter.mapReduce(map,reduce,{out:'map_reduce_result'});

db.map_reduce_result.find();

You can use JavaScript regular expresions instead of MongoDB $regex.

您可以使用JavaScript常规表达式而不是MongoDB $ regex。