【问题标题】:Retrieve fields of Mongo collection in PHP在 PHP 中检索 Mongo 集合的字段
【发布时间】:2013-07-13 03:23:03
【问题描述】:

我尝试在光标上使用fields() 方法:

<?php
$mongo  = new Mongo("mongodb://localhost");
print_r($mongo);

$db = $mongo->test;

// access collection
$collection = $db->test;

// execute query
// retrieve all documents
print_r($test);

$cursor = $collection->find();
print_r($cursor);

$fields = $cursor->fields(array("summary" => true));
print_r($fields);

输出是:

Mongo Object ( [connected] => 1 [status] => [server:protected] => [persistent:protected] => )
MongoCursor Object ( )
MongoCursor Object ( ) 

看起来驱动程序和连接工作正常,但我无法检索字段的独特摘要。

【问题讨论】:

    标签: php mongodb-php


    【解决方案1】:

    假设我们可以访问名为 db 的 mongodb 数据库,将从 MyCollection 检索数据并使用 MongoDB\Driver\Manager

      $manager = new MongoDB\Driver\Manager( $DB_CONNECTION_STRING );
    

    然后我们在这种情况下通过 name 检索数据,限制 2 个文档,并希望获得 only fields name em>、ageaddress 等等。 Projection 选项可用于指定应返回哪些字段,使用 0 排除并使用 1 包含:

      $filter = ['name' => 'John'];
      $options = [ 'projection' => ['_id' => 0, 'name' => 1, 'age' => 1, 'address' => 1], 'limit' => 2 ];
      $query = new MongoDB\Driver\Query($filter, $options);
      $manager->executeQuery('db.MyCollection', $query);
    

    如果我们需要获取所有列,那么我们只需省略 projection 选项,如下所示:

      $filter = ['name' => 'daniel'];
      $options = [];
      $query = new MongoDB\Driver\Query($filter, $options);
      $manager->executeQuery('db.MyCollection', $query);
    

    【讨论】:

    • 我事先不知道这些字段。在 2013 年,我需要先获得字段才能知道它们。
    • 希望答案的添加部分适合您的情况
    • 它没有。这如何总结集合中可用的字段(~列)?
    【解决方案2】:
     const HOST = 'localhost';
     const PORT = 27017;
     const DBNAME = 'test';
     const COLLECTION = 'test';
     $connectionString = sprintf('mongodb://%s:%d', HOST, PORT);
    
     try {
    
            $connection = new Mongo($connectionString);
            $database = $connection->selectDB(DBNAME);
    
     } catch (MongoConnectionException $e) {
    
            throw $e;
    
    }
    $collection = $database->selectCollection(COLLECTION);
    
    try{
            $query = array();
            $specifyKey = array("summary" => true);
            $cursor = $collection->find($query , $specifyKey);
    
    }catch(MongoException $e) {
    
            die('Failed to find One data '.$e->getMessage());
    
    }catch (MongoCursorException $e) {
    
        echo "error message: ".$e->getMessage()."\n";
        echo "error code: ".$e->getCode()."\n";
    }
    
    while ($cursor->hasNext()){
    
        $nextCursor = $cursor->getNext(); 
        //process next cursor
    }
    

    【讨论】:

      【解决方案3】:

      在 $cursor 变量之后尝试使用 foreach。它遍历游标结果。

      foreach($cursor as $document) {  
       var_dump($document);  
      }
      

      【讨论】:

      • 这适用于获取所有行(适用于find()),我想没有办法获取集合中可用的所有不同字段?
      • 这会获取不同的值,但我需要不同的键 :-( 就像上面的示例一样,我需要 "name, randomNumber" 作为结果
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-02-16
      • 2018-12-31
      • 1970-01-01
      • 2017-09-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多