Sokil MongoDB从数据库中读取

Sokil MongoDB从数据库中读取

问题描述:

how can i get the parameters without loop, only read the array $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 return me a lot of classes with private parameters. i cannot read the different parameter (private) directly. Hier is 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"
  }
}

How can read the $find-Array? something like :

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

i don't want to use a loop

$collection->find()->where('Age', 5) returns cursor, not document or list of documents. So you need to get document or list of documents.

// 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');

Sokil's QueryBuilder class extends Cursor, which in turn has a findAll() terminator method that converts the underlying MongoCursor object into an array with iterator_to_array(). This is discussed in the project's documentation on querying documents.