【问题标题】:How to write a find query with condition in php-mongodb?如何在 php-mongodb 中编写带有条件的查找查询?
【发布时间】:2016-03-20 18:12:12
【问题描述】:

我在 php-monogodb 中查找查询时遇到问题。 find() 没有条件可以正常工作,但是在 find() 方法中使用条件时,它不会给出任何结果。如何解决这个问题?

$connection = new Mongo();
$db = $connection->selectDB('db1');
$collection = $db->selectCollection('customers');

$cursor = $collection->find(array('CUSTOMER_ID' => $id));

$num_docs = $cursor->count();

if($num_docs > 0)
{
    foreach($cursor as $obj)
    {
        echo 'Customer-Id: '.$obj['CUSTOMER_ID']."\n";
        echo 'Customer Name: '.$obj['CUST_FIRST_NAME']." ".$obj['CUST_LAST_NAME']."\n";
        echo 'Customer Email: '.$obj['CUST_EMAIL']."\n";
        echo "\n\n\n";
    }
}

在 var_dump($cursor->explain()); 之后它打印:

array (size=3)
  'queryPlanner' => 
    array (size=6)
      'plannerVersion' => int 1
      'namespace' => string 'db1.customers' (length=13)
      'indexFilterSet' => boolean false
      'parsedQuery' => 
        array (size=1)
          'CUSTOMER_ID' => 
            array (size=1)
              ...
      'winningPlan' => 
        array (size=3)
          'stage' => string 'COLLSCAN' (length=8)
          'filter' => 
            array (size=1)
              ...
          'direction' => string 'forward' (length=7)
      'rejectedPlans' => 
        array (size=0)
          empty
  'executionStats' => 
    array (size=7)
      'executionSuccess' => boolean true
      'nReturned' => int 0
      'executionTimeMillis' => int 1
      'totalKeysExamined' => int 0
      'totalDocsExamined' => int 325
      'executionStages' => 
        array (size=14)
          'stage' => string 'COLLSCAN' (length=8)
          'filter' => 
            array (size=1)
              ...
          'nReturned' => int 0
          'executionTimeMillisEstimate' => int 0
          'works' => int 327
          'advanced' => int 0
          'needTime' => int 326
          'needYield' => int 0
          'saveState' => int 2
          'restoreState' => int 2
          'isEOF' => int 1
          'invalidates' => int 0
          'direction' => string 'forward' (length=7)
          'docsExamined' => int 325
      'allPlansExecution' => 
        array (size=0)
          empty
  'serverInfo' => 
    array (size=4)
      'host' => string 'deadpool' (length=8)
      'port' => int 27017
      'version' => string '3.2.4' (length=5)
      'gitVersion' => string 'e2ee9ffcf9f5a94fad76802e28cc978718bb7a30' (length=40)

你能解释一下出了什么问题吗?

【问题讨论】:

  • 尝试打印您的查询,例如 var_dump($cursor->explain());查看查询是否正确。
  • 我这样做了,并将结果复制到我的问题本身下面。你能告诉我它有什么问题吗,我是新手。

标签: php mongodb mongodb-php php-mongodb


【解决方案1】:

$cursor = $collection->find(array('CUSTOMER_ID' => $id)); 你从哪里得到 id 值。

并尝试不使用 id ,因为如果在其他地方使用 $id ,有时它会遇到问题,尤其是找到值。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-13
    • 1970-01-01
    • 2012-09-12
    • 2012-08-05
    • 1970-01-01
    • 2019-06-14
    相关资源
    最近更新 更多