Sokil MongoDB从数据库中读取

时间:2014-09-24 12:24:55

标签: php mongodb

如何在没有循环的情况下获取参数,只读取数组$ find?

$collection = $tmp->getCollection('X');
$document = [
    'Me' => 'Keyonkeu',
    'Maman' => 'Noubissie',
    'Soeur' => 'Manuella',
    'Papa' => 'SSadi',
    'Age' => 5
];
$collection->createDocument($document)->save();
$find = $collection->find()->where('Age', 5);
var_dump($find);

var_dump用私有参数返回了很多类。我不能直接读取不同的参数(私人)。 Hier是wath var_dump($ find)return。

class Sokil\Mongo\QueryBuilder#29 (11) {
  private $_client =>
  class Sokil\Mongo\Client#24 (7) {
    private $_dsn =>
    string(19) "mongodb://127.0.0.1"
    private $_connectOptions =>
    array(1) {
      'connect' =>
      bool(true)
    }
    private $_connection =>
    class MongoClient#25 (4) {
      public $connected =>
      bool(true)
      public $status =>
      NULL
      protected $server =>
      NULL
      protected $persistent =>
      NULL
    }
    private $_databasePool =>
    array(1) {
      'EmoBD' =>
      class Sokil\Mongo\Database#23 (6) {
        ...
      }
    }
    protected $_mapping =>
    array(0) {
    }
    private $_logger =>
    NULL
    private $_currentDatabaseName =>
    string(5) "EmoBD"
  }
  private $_collection =>
  class Sokil\Mongo\Collection#27 (6) {
    protected $_queryBuliderClass =>
    string(25) "\Sokil\Mongo\QueryBuilder"
    protected $_queryExpressionClass =>
    string(23) "\Sokil\Mongo\Expression"
    private $_database =>
    class Sokil\Mongo\Database#23 (6) {
      private $_client =>
      class Sokil\Mongo\Client#24 (7) {
        ...
      }
      private $_databaseName =>
      string(5) "EmoBD"
      private $_mongoDB =>
      class MongoDB#26 (2) {
        ...
      }
      private $_mapping =>
      array(0) {
        ...
      }
      private $_classPrefix =>
      NULL
      private $_collectionPool =>
      array(1) {
        ...
      }
    }
    private $_mongoCollection =>
    class MongoCollection#28 (2) {
      public $w =>
      int(1)
      public $wtimeout =>
      int(10000)
    }
    private $_documentsPool =>
    array(0) {
    }
    protected $_documentPoolEnabled =>
    bool(false)
  }
  private $_fields =>
  array(0) {
  }
  private $_cursor =>
  NULL
  private $_skip =>
  int(0)
  private $_expression =>
  class Sokil\Mongo\Expression#30 (1) {
    protected $_expression =>
    array(1) {
      'Age' =>
      int(5)
    }
  }
  private $_limit =>
  int(0)
  private $_sort =>
  array(0) {
  }
  private $_readPreferences =>
  array(0) {
  }
  protected $_queryExpressionClass =>
  NULL
  private $_options =>
  array(2) {
    'arrayResult' =>
    bool(false)
    'expressionClass' =>
    string(23) "\Sokil\Mongo\Expression"
  }
}

如何阅读$ find-Array?类似的东西:

$find['...']->get('...')

我不想使用循环

2 个答案:

答案 0 :(得分:0)

Sokil的QueryBuilder类扩展了Cursor,后者又有一个findAll()终止符方法,可以将基础MongoCursor对象转换为iterator_to_array()的数组。这在querying documents的项目文档中进行了讨论。

答案 1 :(得分:0)

$ collection-> find() - >其中(' Age',5)返回光标,而不是文档或文档列表。因此,您需要获取文档或文档列表。

// get cursor which incapsulates all matched documents
$cursor = $collection->find()->where('Age', 5);
// get first from matched documents
echo $cursor->findOne()->get('Age');
// get random from matched documents
echo $cursor->findRandom()->get('Age');
// get all matched documents as array
$documentList = $cursor->findAll();
echo $documentList['some-document-id']->get('Age');