简体   繁体   English

使用变量通过PHP查询MongoDB

[英]Use variable to query MongoDB with PHP

I need to dynamically build a complex MongoDB query before executing it in PHP. 在PHP中执行查询之前,我需要动态构建一个复杂的MongoDB查询。 My query line looks like $cursor = $c_sbc->aggregate($query_string); 我的查询行看起来像$cursor = $c_sbc->aggregate($query_string); , where $query_string is something like [['$match' => ['symbol' => $sym]],['$project' => ['first' => ['$arrayElemAt' => ['$data.1000', -1]]]]] . ,其中$ query_string类似于[['$match' => ['symbol' => $sym]],['$project' => ['first' => ['$arrayElemAt' => ['$data.1000', -1]]]]]

Copy-and-pasting the above-given example to replace $query_string gives the desired result. 复制并粘贴上面给出的示例以替换$ query_string可以得到所需的结果。 However, running it with $query_string in place gives an error saying it expects an array, not a string. 但是,在$ query_string到位的情况下运行它会出现错误,提示它需要数组而不是字符串。 How do I get this query to work? 如何使该查询起作用?

Catchable fatal error: Argument 1 passed to MongoDB\Collection::aggregate() must be of the type array, string given, called in C:\xampp\htdocs\gc5\screen.php on line 60 and defined in C:\xampp\htdocs\gc5\vendor\mongodb\mongodb\src\Collection.php on line 163

Edit: relevant PHP 编辑:相关的PHP

$query = $_POST['screen'];

  $t = array(
    "revenue" => 1000,
    "costofgoodssold" => 1001
  );

  $data_array = [];

  //turn words into data.XXXX codes
  function translate($match){
    global $t;
    global $data_array;
    foreach($match as $m){
      $d = "data.".$t[$m];
      $data_array[] = $d;
      return $d;
    }
  }

  $query = preg_replace('/\s/', '', $query); //strip whitespace
  $query = strtolower($query);
  $query = preg_replace_callback('/([A-Z]+)/i','translate', $query);

  echo "<br>Query: ";
  print_r($query);
  echo "<br>";


  $client = new MongoDB\Client("mongodb://localhost:27017");
  $db = $client->gc_dev;
  $c_sbc = $db->screenByCompany;

  $for_years = [-1]; //default is TTM
  $symbols = ['goog', 'fb', 'crmt', 'vlgea', 'ko', 'pep', 'flws'];

  for($i=0;$i<count($symbols);$i++){
    $sym = $symbols[$i];
    for($j=0;$j<count($for_years);$j++){
      $k = $for_years[$j];

      //build query for data
      $data_query = "";
      foreach($data_array as $d){
        if($data_query == ""){ //first go-around, no need for comma
          $data_query .= "['first' => ['\$arrayElemAt' => ['$".$d."', ".$k."]]]";
        }else{
          //$data_query .= ",['second' => ['\$arrayElemAt' => ['$".$d."', ".$k."]]]";
        }
        $query_string = "[['\$match' => ['symbol' => \$sym]],['\$project' => ".$data_query."]]";

      }

      echo "<br>\$query_string: ".$query_string;
      $cursor = $c_sbc->aggregate($query_string);
      //$cursor = $c_sbc->aggregate([['$match' => ['symbol' => $sym]],['$project' => ['first' => ['$arrayElemAt' => ['$data.1000',-1]]]]]);
      $cursor = iterator_to_array($cursor);
      //var_dump($cursor);
      echo "Cursor: ".$cursor[0]['first'] . "<br><br>";

    }

Results in: 结果是:

Query: (data.1000-data.1001)>1,000

$query_string: [['$match' => ['symbol' => $sym]],['$project' => ['first' => ['$arrayElemAt' => ['$data.1000', -1]]]]]

Catchable fatal error: Argument 1 passed to MongoDB\Collection::aggregate() must be of the type array, string given, called in C:\xampp\htdocs\gc5\screen.php on line 60 and defined in C:\xampp\htdocs\gc5\vendor\mongodb\mongodb\src\Collection.php on line 163

Found your error. 发现您的错误。 You are declaring $query_string as a string and not as an array like what the function aggregate is asking for. 您将$ query_string声明为字符串,而不是像函数集合所要求的那样声明为数组。 Your code is: 您的代码是:

$query_string = "[['\\$match' => ['symbol' => \\$sym]],['\\$project' => ".$data_query."]]"; $ query_string =“ [['\\ $ match'=> ['symbol'=> \\ $ sym]],['\\ $ project'=>”。$ data_query。“]]”“;

Replace it with: 替换为:

$query_string = [['\\$match' => ['symbol' => \\$sym]],['\\$project' => $data_query]]; $ query_string = [['\\ $ match'=> ['symbol'=> \\ $ sym]],['\\ $ project'=> $ data_query]];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM