/**
  * Constructs new Database instance.
  *
  * This class provides methods for database-specific operations and serves
  * as a gateway for accessing collections.
  *
  * @param Manager        $manager        Manager instance from the driver
  * @param string         $databaseName   Database name
  * @param WriteConcern   $writeConcern   Default write concern to apply
  * @param ReadPreference $readPreference Default read preference to apply
  * @throws InvalidArgumentException if $databaseName is invalid
  */
 public function __construct(Manager $manager, $databaseName, WriteConcern $writeConcern = null, ReadPreference $readPreference = null)
 {
     if (strlen($databaseName) < 1) {
         throw new InvalidArgumentException('$databaseName is invalid: ' . $databaseName);
     }
     $this->manager = $manager;
     $this->databaseName = (string) $databaseName;
     $this->writeConcern = $writeConcern ?: \MongoDB\get_manager_write_concern($this->manager);
     $this->readPreference = $readPreference ?: \MongoDB\get_manager_read_preference($this->manager);
 }
 /**
  * Constructs new Collection instance.
  *
  * This class provides methods for collection-specific operations, such as
  * CRUD (i.e. create, read, update, and delete) and index management.
  *
  * @param Manager        $manager        Manager instance from the driver
  * @param string         $namespace      Collection namespace (e.g. "db.collection")
  * @param WriteConcern   $writeConcern   Default write concern to apply
  * @param ReadPreference $readPreference Default read preference to apply
  * @throws InvalidArgumentException if $namespace is invalid
  */
 public function __construct(Manager $manager, $namespace, WriteConcern $writeConcern = null, ReadPreference $readPreference = null)
 {
     $parts = explode('.', $namespace, 2);
     if (count($parts) != 2 || strlen($parts[0]) == 0 || strlen($parts[1]) == 0) {
         throw new InvalidArgumentException('$namespace is invalid: ' . $namespace);
     }
     $this->databaseName = $parts[0];
     $this->collectionName = $parts[1];
     $this->manager = $manager;
     $this->writeConcern = $writeConcern ?: \MongoDB\get_manager_write_concern($this->manager);
     $this->readPreference = $readPreference ?: \MongoDB\get_manager_read_preference($this->manager);
 }
Beispiel #3
0
 /**
  * Select a database.
  *
  * If a write concern or read preference is not specified, the write concern
  * or read preference of the Client will be applied, respectively.
  *
  * @param string         $databaseName   Name of the database to select
  * @param WriteConcern   $writeConcern   Default write concern to apply
  * @param ReadPreference $readPreference Default read preference to apply
  * @return Database
  */
 public function selectDatabase($databaseName, WriteConcern $writeConcern = null, ReadPreference $readPreference = null)
 {
     $writeConcern = $writeConcern ?: \MongoDB\get_manager_write_concern($this->manager);
     $readPreference = $readPreference ?: \MongoDB\get_manager_read_preference($this->manager);
     return new Database($this->manager, $databaseName, $writeConcern, $readPreference);
 }