[MongoDB] 使用PHP在MongoDB中搜索的實現

  • 2020 年 2 月 25 日
  • 筆記

條件操作符用於比較兩個表達式並從mongoDB集合中獲取數據。 MongoDB中條件操作符有: (>) 大於 – $gt (<) 小於 – $lt (>=) 大於等於 – $gte (<= ) 小於等於 – $lte MongoDB 使用 $regex 操作符來設置匹配字符串的正則表達式,使用PCRE (Perl Compatible Regular Expression) 作為正則表達式語言。 MongoDB OR 條件語句使用了關鍵字 $or

下面是具體一個PHP例子中的$filter數組:

array(3) {    ["$or"]=>    array(2) {      [0]=>      array(1) {        ["modelID"]=>        string(12) "基礎新聞"      }      [1]=>      array(1) {        ["name"]=>        string(12) "基礎新聞"      }    }    ["createTime"]=>    array(2) {      ["$gte"]=>      string(19) "2020-02-18 00:00:00"      ["$lte"]=>      string(19) "2020-02-18 23:59:59"    }    ["modelXML"]=>    array(1) {      ["$regex"]=>      string(6) "標題"    }  }
        $filter=$this->parseSearchQuery($q);          //分頁顯示          $options = [              'skip'=>($page - 1) * $pageSize,              'limit'=>$pageSize,              'sort' => ['createTime' => -1],              'projection'=>['_id'=> False, "modelXML"=> False],          ];          var_dump($filter);          $mongoManger = new MongoDBDriverManager("mongodb://127.0.0.1:27017");          $query = new MongoDBDriverQuery($filter, $options);          $cursor = $mongoManger->executeQuery('.article', $query);          if($cursor->isDead()){              return [];          }          $list=[];          foreach ($cursor as $document) {              $list[]=$document;          }          return $list;