duanchao4445 2014-03-12 07:12
浏览 57
已采纳

MongoDB:将PHP代码转换为Python代码

I was searching a way to combine $in and $regex in mongoDB and found exactly the same question with exactly what I want to do. The problem is that the answer is in PHP and I don't have an idea about it. The question is here.

Do you know if I can find anywhere the same sollution in Python code?

$user_query = preg_replace("/[[:blank:]]+/"," ", $user_query);
$arr_query = explode(' ', $user_query);

if (count($arr_query) > 1) {
    $tmp = array();

    foreach ($arr_query as $q) {
        $tmp[] = new MongoRegex( "/". $q ."/" );
    }

    $who['keywords'] = array('$in' => $tmp);

} else {
    $who['keywords'] = new MongoRegex( "/". $user_query ."/" );
}

$db->collection->find( $who ); 
  • 写回答

1条回答 默认 最新

  • doucha4054 2014-03-12 08:15
    关注

    This is what the code does. Given the string:

    "one two three"
    

    It gets converted to the equivalent of :

    { "$in": [
        {"$regex": "one"},
        {"$regex": "two"},
        {"$regex": "three"}
    ]}
    

    But this regex is exactly the same:

    {"$regex": "one|two|three"}
    

    And to get that, then all you need to do is replace the whitespace with a |.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 WPF 大屏看板表格背景图片设置
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示