【问题标题】:Mongodb Multiple and statement using PHP codeMongodb Multiple and语句使用PHP代码
【发布时间】:2016-08-01 06:16:09
【问题描述】:

我想要使用 PHP 代码从 mongodb 中进行如下查询:

select * from orders where mrn=1234 and status!=cancelled and status!=delivered and status!=draft

我尝试了以下代码,但不起作用:

$filterpatient = array("order.patientinfo.mrn" => $reqresult, '$and' => array( 
    array('order.orderitem.status' => array('$ne' => array('cancelled','delivered')))
    ));
$cursor = $collection->find($filterpatient)->sort(array('_id'=>-1));

【问题讨论】:

    标签: php mongodb mongodb-query php-mongodb


    【解决方案1】:
    Try:
    $collection->find(array("order.patientinfo.mrn" => $reqresult, 'order.orderitem.status' => array('$nin' => array("cancelled","delivered","draft"))))->sort(array('_id'=>-1));
    

    MongoDB查询:

    db.orders.find({"mrn":1234,"status":{"$nin":["cancelled","delivered","draft"]}});
    

    更多详情Click here

    【讨论】:

      【解决方案2】:

      您的查询不正确,首先您的 mrn 查询也应该在 $and 子句中,并且您应该使用$nin(不在数组中)作为您的状态。您的状态查询也被两个 array( 子句包围。

      $filterpatient = array('$and' => array(
          "order.patientinfo.mrn" => $reqresult, 
          "order.orderitem.status" => array('$nin' => array('cancelled','delivered', 'draft'))
      ));
      

      【讨论】:

        【解决方案3】:

        您可以使用MongoDB_DataObject,如下:

        $model = new MongoDB_DataObject();
        
        $model->query("select * from orders where mrn=1234 and status!='cancelled' and status!='delivered' and status!='draft'");
        
        while ($model->fetch()) {
            var_dump($model);
        } 
        

        或者:

        $model = new MongoDB_DataObject('orders');
        
        $model->whereAdd("mrn=1234 and status!='cancelled' and status!='delivered' and status!='draft'");
        
        $model->find();
        
        while ($model->fetch()) {
            var_dump($model);
        }    
        

        【讨论】:

          猜你喜欢
          • 2020-06-08
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-03-21
          • 2017-09-26
          • 1970-01-01
          • 1970-01-01
          • 2012-02-13
          相关资源
          最近更新 更多