首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在php-mongodb中编写带条件的find查询?

如何在php-mongodb中编写带条件的find查询?
EN

Stack Overflow用户
提问于 2016-03-21 02:12:13
回答 1查看 1.5K关注 0票数 0

我在php-monogodb中的find查询有问题。没有条件的find()运行得很好,但是当在find()方法中使用条件时,它不会给出任何结果。如何解决这个问题?

代码语言:javascript
复制
$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())之后;它打印:

代码语言:javascript
复制
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)

你能解释一下哪里出了问题吗?

EN

回答 1

Stack Overflow用户

发布于 2016-04-12 00:05:58

$cursor = $collection->find(array('CUSTOMER_ID' => $id));您从哪里获取id值。

尽量不要使用id,因为有时查找值时会出现问题,特别是在其他地方使用$id时。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36117590

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档