2016-03-20 25 views
0

php-monogodbで検索クエリに問題があります。 find()の条件がない場合はうまく動作しますが、find()の場合は結果が得られません。この問題を解決するには?php-mongodbに条件付き検索クエリを書くには?

$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) 

あなたは何がうまくいかないのですか?

+0

var_dump($ cursor-> explain())のようにクエリを印刷してみてください。クエリが正しいかどうかを確認します。 –

+0

私はそれをして、その結果を自分の質問の下にコピーしました。あなたは何が間違っているか教えてもらえますか?それは初めてです。 – luffy

答えて

0

$cursor = $collection->find(array('CUSTOMER_ID' => $id));ここからid値を取得しています。

idを使用しないでください。$idが他の場所で使用されている場合は値espを見つけるのに問題があることがあります。