【问题标题】:Javascript function not working properly in mongoDB using PHPJavascript函数在使用PHP的mongoDB中无法正常工作
【发布时间】:2012-07-24 13:17:10
【问题描述】:

我已将正则表达式保存为集合中的值。我想根据 mongodb 中的正则表达式检查一个字符串,它是否匹配?我知道我应该使用 javascript 代码,但它在 php-mongodb 中不起作用。

它在 mongodb 客户端控制台上运行。

这是我的代码,

<?php

    $dbName = "XYZ";
    try
    {
            $dbInstanceName = $dbName;
            $mongoInstance  = new Mongo("localhost:27017", array("persist"    => "x"));
            $dbReference = $mongoInstance->$dbInstanceName;
            $db          = $mongoInstance->$dbInstanceName;
    }
    catch (Exception $e)
    {
            echo "<pre>";
            print_r($e);
            echo "</pre>";
            exit;
    }

    $query  = new MongoCode("function (){return RegExp(this.regCountry).test('India');}");

    $c = "Countries1";

    $cursor = $db->$c->find($query);


    if ($cursor->count() == 0)
    {
            echo "<pre>";
            print_r("No Data Found.!");
            echo "</pre>";
            exit;
    }
    else
    {

            $data = array();
            while ($cursor->hasNext())
            {
                    $cursor->next();
                    array_push($data, $cursor->current());
            }                
    }
    echo "<pre>";
    print_r($data);
    echo "</pre>";
    exit;

?>

它工作但什么也没返回......!

共鸣:

Array
(

)

这里,控制台屏幕排序,它的给定返回一个值。

这里是我的数据样本:

怎么了?

【问题讨论】:

  • 你试过用var_dump代替print_r吗?
  • 感谢回复...我试过了,但这没关系...
  • 试试$c = "Countries1";

标签: php javascript mongodb


【解决方案1】:

如果我上面的评论不起作用,请尝试更改:

$cursor = $db->$c->find($query);

到:

$cursor = $db->selectCollection($c)->find($query);

【讨论】:

    【解决方案2】:

    终于找到答案了:

    更改我的代码如下所示

    <?php
    
        $dbName = "Scrubly";
        try
        {
                $dbInstanceName = $dbName;
                $mongoInstance  = new Mongo("localhost:27017", array("persist"    => "x"));
                $dbReference = $mongoInstance->$dbInstanceName;
                $db          = $mongoInstance->$dbInstanceName;
        }
        catch (Exception $e)
        {
                echo "<pre>";
                print_r($e);
                echo "</pre>";
                exit;
        }
    
        $query = "function(){return db.Countries1.find(function (){return RegExp(this.regCountry).test('India');}).toArray();}";
    
        $result = $db->execute($query);
        echo "<pre>";
        print_r($result);
        echo "</pre>";
        exit;
    
    ?>
    

    输出:

    Array
    (
    [retval] => Array
        (
            [0] => Array
                (
                    [_id] => MongoId Object
                        (
                            [$id] => 500e5b456bcb44780d000072
                        )
    
                    [country] => India
                    [countryCode] => IN
                    [createdAt] => 2012-07-24 13:52:29
                    [id] => 386
                    [regCountry] => ^(.*)India(.*)$
                    [updatedAt] => 2012-07-24 13:52:29
                )
    
        )
    
    [ok] => 1
    )
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多