MongoDB driver classes目次
The MongoDB\Driver\Manager classはじめにThe MongoDB\Driver\Manager is the main entry point to the extension. It is responsible for maintaining connections to MongoDB (be it standalone server, replica set, or sharded cluster). No connection to MongoDB is made upon instantiating the Manager. This means the MongoDB\Driver\Manager can always be constructed, even though one or more MongoDB servers are down. Any write or query can throw connection exceptions as connections are created lazily. A MongoDB server may also become unavailable during the life time of the script. It is therefore important that all actions on the Manager to be wrapped in try/catch statements. クラス概要MongoDB\Driver\Manager
final
class MongoDB\Driver\Manager
{
/* メソッド */
final public void addSubscriber(MongoDB\Driver\Monitoring\Subscriber
$subscriber )final public __construct(stringnull
$uri = null , arraynull $uriOptions = null , arraynull $driverOptions = null )final public MongoDB\Driver\ClientEncryption createClientEncryption(array
$options )final public MongoDB\Driver\WriteResult executeBulkWrite(string
$namespace , MongoDB\Driver\BulkWrite $bulk , arrayMongoDB\Driver\WriteConcernnull $options = null )final public MongoDB\Driver\Cursor executeCommand(string
$db , MongoDB\Driver\Command $command , arrayMongoDB\Driver\ReadPreferencenull $options = null )final public MongoDB\Driver\Cursor executeQuery(string
$namespace , MongoDB\Driver\Query $query , arrayMongoDB\Driver\ReadPreferencenull $options = null )final public MongoDB\Driver\Cursor executeReadCommand(string
$db , MongoDB\Driver\Command $command , arraynull $options = null )final public MongoDB\Driver\Cursor executeReadWriteCommand(string
$db , MongoDB\Driver\Command $command , arraynull $options = null )final public MongoDB\Driver\Cursor executeWriteCommand(string
$db , MongoDB\Driver\Command $command , arraynull $options = null )final public arrayobjectnull getEncryptedFieldsMap()
final public MongoDB\Driver\ReadConcern getReadConcern()
final public MongoDB\Driver\ReadPreference getReadPreference()
final public array getServers()
final public MongoDB\Driver\WriteConcern getWriteConcern()
final public void removeSubscriber(MongoDB\Driver\Monitoring\Subscriber
$subscriber )final public MongoDB\Driver\Server selectServer(MongoDB\Driver\ReadPreferencenull
$readPreference = null )final public MongoDB\Driver\Session startSession(arraynull
}$options = null )例例1 MongoDB\Driver\Manager::__construct basic example var_dumping a MongoDB\Driver\Manager will print out various details about the manager that are otherwise not normally exposed. This can be useful to debug how the driver views your MongoDB setup, and which options are used.
<?php 上の例の出力は、 たとえば以下のようになります。 object(MongoDB\Driver\Manager)#1 (2) { ["uri"]=> string(26) "mongodb://127.0.0.1:27017/" ["cluster"]=> array(0) { } } The MongoDB\Driver\Command classはじめにThe MongoDB\Driver\Command class is a value object that represents a database command.
To provide クラス概要MongoDB\Driver\Command
final
class MongoDB\Driver\Command
{
/* メソッド */
final public __construct(arrayobject
}$document , arraynull $commandOptions = null )例例1 Composing MongoDB\Driver\Command to provide a helper to create collections
<?php 上の例の出力は以下となります。 object(MongoDB\Driver\Command)#3 (1) { ["command"]=> array(3) { ["create"]=> string(16) "cappedCollection" ["capped"]=> bool(true) ["size"]=> int(65536) } } array(1) { ["ok"]=> float(1) } array(16) { ["ns"]=> string(29) "databaseName.cappedCollection" ["count"]=> int(0) ["size"]=> int(0) ["numExtents"]=> int(1) ["storageSize"]=> int(65536) ["nindexes"]=> int(1) ["lastExtentSize"]=> float(65536) ["paddingFactor"]=> float(1) ["paddingFactorNote"]=> string(101) "paddingFactor is unused and unmaintained in 2.8. It remains hard coded to 1.0 for compatibility only." ["userFlags"]=> int(0) ["capped"]=> bool(true) ["max"]=> int(9223372036854775807) ["maxSize"]=> int(65536) ["totalIndexSize"]=> int(8176) ["indexSizes"]=> object(stdClass)#4 (1) { ["_id_"]=> int(8176) } ["ok"]=> float(1) } The MongoDB\Driver\Query classはじめにThe MongoDB\Driver\Query class is a value object that represents a database query. クラス概要MongoDB\Driver\Query
final
class MongoDB\Driver\Query
{
/* メソッド */
final public __construct(arrayobject
}$filter , arraynull $queryOptions = null )The MongoDB\Driver\BulkWrite classはじめにThe MongoDB\Driver\BulkWrite collects one or more write operations that should be sent to the server. After adding any number of insert, update, and delete operations, the collection may be executed via MongoDB\Driver\Manager::executeBulkWrite. Write operations may either be ordered (default) or unordered. Ordered write operations are sent to the server, in the order provided, for serial execution. If a write fails, any remaining operations will be aborted. Unordered operations are sent to the server in an arbitrary order where they may be executed in parallel. Any errors that occur are reported after all operations have been attempted. クラス概要MongoDB\Driver\BulkWrite
final
class MongoDB\Driver\BulkWrite
implements
Countable {
/* メソッド */
public __construct(arraynull
$options = null )public int count()
public void delete(arrayobject
$filter , arraynull $deleteOptions = null )public mixed insert(arrayobject
$document )public void update(arrayobject
}$filter , arrayobject $newObj , arraynull $updateOptions = null )例例1 Mixed write operations are grouped by type Mixed write operations (i.e. inserts, updates, and deletes) will be assembled into typed write commands to be sent sequentially to the server.
<?php Will result in four write commands (i.e. roundtrips) being executed. Since the operations are ordered, the third insertion cannot be sent until the preceding update is executed. 例2 Ordered write operations causing an error
<?php 上の例の出力は以下となります。 Operation#7: E11000 duplicate key error index: db.collection.$_id_ dup key: { : 3 } (11000) Inserted 4 document(s) Updated 2 document(s) If the write concern could not be fullfilled, the example above would output something like: waiting for replication timed out (64): array ( 'wtimeout' => true, ) Operation#7: E11000 duplicate key error index: databaseName.collectionName.$_id_ dup key: { : 3 } (11000) Inserted 4 document(s) Updated 2 document(s) If we execute the example above, but allow for unordered writes:
<?php 上の例の出力は以下となります。 Operation#7: E11000 duplicate key error index: db.collection.$_id_ dup key: { : 3 } (11000) Operation#8: E11000 duplicate key error index: db.collection.$_id_ dup key: { : 4 } (11000) Inserted 5 document(s) Updated 2 document(s) 参考
The MongoDB\Driver\Session classはじめにThe MongoDB\Driver\Session class represents a client session and is returned by MongoDB\Driver\Manager::startSession. Commands, queries, and write operations may then be associated the session. クラス概要MongoDB\Driver\Session
final
class MongoDB\Driver\Session
{
/* 定数 */
const
string
MongoDB\Driver\Session::TRANSACTION_NONE = none;
const
string
MongoDB\Driver\Session::TRANSACTION_STARTING = starting;
const
string
MongoDB\Driver\Session::TRANSACTION_IN_PROGRESS = in_progress;
const
string
MongoDB\Driver\Session::TRANSACTION_COMMITTED = committed;
const
string
MongoDB\Driver\Session::TRANSACTION_ABORTED = aborted;
/* メソッド */
final public void abortTransaction()
final public void advanceClusterTime(arrayobject
$clusterTime )final public void advanceOperationTime(MongoDB\BSON\TimestampInterface
$operationTime )final public void commitTransaction()
final private __construct()
final public void endSession()
final public objectnull getClusterTime()
final public object getLogicalSessionId()
final public MongoDB\BSON\Timestampnull getOperationTime()
final public MongoDB\Driver\Servernull getServer()
final public arraynull getTransactionOptions()
final public string getTransactionState()
final public bool isDirty()
final public bool isInTransaction()
final public void startTransaction(arraynull
}$options = null )定義済み定数
The MongoDB\Driver\ClientEncryption classはじめにThe MongoDB\Driver\ClientEncryption class handles creation of data keys for client-side encryption, as well as manually encrypting and decrypting values. クラス概要MongoDB\Driver\ClientEncryption
final
class MongoDB\Driver\ClientEncryption
{
/* 定数 */
const
string
MongoDB\Driver\ClientEncryption::AEAD_AES_256_CBC_HMAC_SHA_512_DETERMINISTIC = AEAD_AES_256_CBC_HMAC_SHA_512-Deterministic;
const
string
MongoDB\Driver\ClientEncryption::AEAD_AES_256_CBC_HMAC_SHA_512_RANDOM = AEAD_AES_256_CBC_HMAC_SHA_512-Random;
const
string
MongoDB\Driver\ClientEncryption::ALGORITHM_INDEXED = Indexed;
const
string
MongoDB\Driver\ClientEncryption::ALGORITHM_UNINDEXED = Unindexed;
const
string
MongoDB\Driver\ClientEncryption::ALGORITHM_RANGE_PREVIEW = RangePreview;
const
string
MongoDB\Driver\ClientEncryption::QUERY_TYPE_EQUALITY = equality;
const
string
MongoDB\Driver\ClientEncryption::QUERY_TYPE_RANGE_PREVIEW = rangePreview;
/* メソッド */
final public objectnull addKeyAltName(MongoDB\BSON\Binary
$keyId , string $keyAltName )final public __construct(array
$options )final public MongoDB\BSON\Binary createDataKey(string
$kmsProvider , arraynull $options = null )final public mixed decrypt(MongoDB\BSON\Binary
$value )final public object deleteKey(MongoDB\BSON\Binary
$keyId )final public MongoDB\BSON\Binary encrypt(mixed
$value , arraynull $options = null )final public object encryptExpression(arrayobject
$expr , arraynull $options = null )final public objectnull getKey(MongoDB\BSON\Binary
$keyId )final public objectnull getKeyByAltName(string
$keyAltName )final public MongoDB\Driver\Cursor getKeys()
final public objectnull removeKeyAltName(MongoDB\BSON\Binary
$keyId , string $keyAltName )final public object rewrapManyDataKey(arrayobject
}$filter , arraynull $options = null )定義済み定数
変更履歴
参考
The MongoDB\Driver\ServerApi classはじめに
クラス概要MongoDB\Driver\ServerApi
final
class MongoDB\Driver\ServerApi
implements
MongoDB\BSON\Serializable, Serializable {
/* 定数 */
const
string
MongoDB\Driver\ServerAPI::V1 = "1";
/* メソッド */
final public stdClass bsonSerialize()
final public __construct(string
$version , boolnull $strict = null , boolnull $deprecationErrors = null )final public string serialize()
final public void unserialize(string
}$data )定義済み定数
例例1 Declare an API version on a manager
<?php 上の例の出力は以下となります。 4.9.0-alpha7-49-gb968ca0 例2 Declare a strict API version on a manager
The following example sets the
<?php 上の例の出力は以下となります。 Provided apiStrict:true, but the command buildInfo is not in API Version 1 The MongoDB\Driver\WriteConcern classはじめに
MongoDB\Driver\WriteConcern describes the level of
acknowledgement requested from MongoDB for write operations to a standalone
クラス概要MongoDB\Driver\WriteConcern
final
class MongoDB\Driver\WriteConcern
implements
MongoDB\BSON\Serializable, Serializable {
/* 定数 */
const
string
MongoDB\Driver\WriteConcern::MAJORITY = "majority";
/* メソッド */
final public stdClass bsonSerialize()
final public __construct(stringint
$w , intnull $wtimeout = null , boolnull $journal = null )final public boolnull getJournal()
final public stringintnull getW()
final public int getWtimeout()
final public bool isDefault()
final public string serialize()
final public void unserialize(string
}$data )定義済み定数
変更履歴
The MongoDB\Driver\ReadPreference classはじめに
クラス概要MongoDB\Driver\ReadPreference
final
class MongoDB\Driver\ReadPreference
implements
MongoDB\BSON\Serializable, Serializable {
/* 定数 */
const
int
MongoDB\Driver\ReadPreference::RP_PRIMARY = 1;
const
int
MongoDB\Driver\ReadPreference::RP_PRIMARY_PREFERRED = 5;
const
int
MongoDB\Driver\ReadPreference::RP_SECONDARY = 2;
const
int
MongoDB\Driver\ReadPreference::RP_SECONDARY_PREFERRED = 6;
const
int
MongoDB\Driver\ReadPreference::RP_NEAREST = 10;
const
string
MongoDB\Driver\ReadPreference::PRIMARY = primary;
const
string
MongoDB\Driver\ReadPreference::PRIMARY_PREFERRED = primaryPreferred;
const
string
MongoDB\Driver\ReadPreference::SECONDARY = secondary;
const
string
MongoDB\Driver\ReadPreference::SECONDARY_PREFERRED = secondaryPreferred;
const
string
MongoDB\Driver\ReadPreference::NEAREST = nearest;
const
int
MongoDB\Driver\ReadPreference::NO_MAX_STALENESS = -1;
const
int
MongoDB\Driver\ReadPreference::SMALLEST_MAX_STALENESS_SECONDS = 90;
/* メソッド */
final public stdClass bsonSerialize()
final public __construct(stringint
$mode , arraynull $tagSets = null , arraynull $options = null )final public objectnull getHedge()
final public int getMaxStalenessSeconds()
final public int getMode()
final public string getModeString()
final public array getTagSets()
final public string serialize()
final public void unserialize(string
}$data )定義済み定数
変更履歴
The MongoDB\Driver\ReadConcern classはじめにMongoDB\Driver\ReadConcern controls the level of isolation for read operations for replica sets and replica set shards. This option requires MongoDB 3.2 or later. クラス概要MongoDB\Driver\ReadConcern
final
class MongoDB\Driver\ReadConcern
implements
MongoDB\BSON\Serializable, Serializable {
/* 定数 */
const
string
MongoDB\Driver\ReadConcern::AVAILABLE = "available";
const
string
MongoDB\Driver\ReadConcern::LINEARIZABLE = "linearizable";
const
string
MongoDB\Driver\ReadConcern::LOCAL = "local";
const
string
MongoDB\Driver\ReadConcern::MAJORITY = "majority";
const
string
MongoDB\Driver\ReadConcern::SNAPSHOT = "snapshot";
/* メソッド */
final public stdClass bsonSerialize()
final public __construct(stringnull
$level = null )final public stringnull getLevel()
final public bool isDefault()
final public string serialize()
final public void unserialize(string
}$data )定義済み定数
変更履歴
The MongoDB\Driver\Cursor classはじめにThe MongoDB\Driver\Cursor class encapsulates the results of a MongoDB command or query and may be returned by MongoDB\Driver\Manager::executeCommand or MongoDB\Driver\Manager::executeQuery, respectively. クラス概要MongoDB\Driver\Cursor
final
class MongoDB\Driver\Cursor
implements
MongoDB\Driver\CursorInterface, Iterator {
/* メソッド */
final private __construct()
public arrayobjectnull current()
final public MongoDB\Driver\CursorId getId()
final public MongoDB\Driver\Server getServer()
final public bool isDead()
public int key()
public void next()
public void rewind()
final public void setTypeMap(array
$typemap )final public array toArray()
public bool valid()
}変更履歴
例例1 Reading a result set MongoDB\Driver\Manager::executeCommand and MongoDB\Driver\Manager::executeQuery both return their result(s) as a MongoDB\Driver\Cursor object. This object can be used to iterate over the result set of the command or query.
Because MongoDB\Driver\Cursor implements the
Traversable interface, you can simply
iterate over the result set with
<?php 上の例の出力は、 たとえば以下のようになります。 stdClass Object ( [_id] => MongoDB\BSON\ObjectId Object ( [oid] => 5a4cff2f122d3321565d8cc2 ) [name] => Ceres [size] => 946 [distance] => 2.766 ) stdClass Object ( [_id] => MongoDB\BSON\ObjectId Object ( [oid] => 5a4cff2f122d3321565d8cc3 ) [name] => Vesta [size] => 525 [distance] => 2.362 } 例2 Reading a result set for a tailable cursor » Tailable cursors are a special type of MongoDB cursor that allows the client to read some results and then wait until more documents become available. These cursors are primarily used with » Capped Collections and » Change Streams.
While normal cursors can be iterated once with In order to continuously read from a tailable cursor, the Cursor object must be wrapped with an IteratorIterator. This allows the application to directly control the cursor's iteration, avoid inadvertently rewinding the cursor, and decide when to wait for new results or stop iteration entirely. In order to demonstrate a tailable cursor in action, two scripts will be used: a "producer" and a "consumer". The producer script will create a new capped collection using the » create command and proceed to insert a new document into that collection each second.
<?php
With the producer script still running, a second consumer script may be
executed to read the inserted documents using a tailable cursor, indicated
by the
<?php
The consumer script will start by quickly printing all available documents
in the capped collection (as if
エラー / 例外When iterating over the cursor object, BSON data is converted into PHP variables. This iteration can cause the following Exceptions:
The MongoDB\Driver\CursorId classはじめにThe MongoDB\Driver\CursorID class is a value object that represents a cursor ID. Instances of this class are returned by MongoDB\Driver\Cursor::getId. クラス概要MongoDB\Driver\CursorId
final
class MongoDB\Driver\CursorId
implements
Serializable, Stringable {
/* メソッド */
final private __construct()
final public string serialize()
final public string __toString()
final public void unserialize(string
}$data )変更履歴
The MongoDB\Driver\CursorInterface interfaceはじめにThis interface is implemented by MongoDB\Driver\Cursor to be used as a parameter, return, or property type in userland classes. クラス概要MongoDB\Driver\CursorInterface
class MongoDB\Driver\CursorInterface
implements
Traversable {
/* メソッド */
abstract public MongoDB\Driver\CursorId getId()
abstract public MongoDB\Driver\Server getServer()
abstract public bool isDead()
abstract public void setTypeMap(array
$typemap )abstract public array toArray()
}変更履歴
The MongoDB\Driver\Server classはじめに
クラス概要MongoDB\Driver\Server
final
class MongoDB\Driver\Server
{
/* 定数 */
const
int
MongoDB\Driver\Server::TYPE_UNKNOWN = 0;
const
int
MongoDB\Driver\Server::TYPE_STANDALONE = 1;
const
int
MongoDB\Driver\Server::TYPE_MONGOS = 2;
const
int
MongoDB\Driver\Server::TYPE_POSSIBLE_PRIMARY = 3;
const
int
MongoDB\Driver\Server::TYPE_RS_PRIMARY = 4;
const
int
MongoDB\Driver\Server::TYPE_RS_SECONDARY = 5;
const
int
MongoDB\Driver\Server::TYPE_RS_ARBITER = 6;
const
int
MongoDB\Driver\Server::TYPE_RS_OTHER = 7;
const
int
MongoDB\Driver\Server::TYPE_RS_GHOST = 8;
const
int
MongoDB\Driver\Server::TYPE_LOAD_BALANCER = 9;
/* メソッド */
final private __construct()
final public MongoDB\Driver\WriteResult executeBulkWrite(string
$namespace , MongoDB\Driver\BulkWrite $bulk , arrayMongoDB\Driver\WriteConcernnull $options = null )final public MongoDB\Driver\Cursor executeCommand(string
$db , MongoDB\Driver\Command $command , arrayMongoDB\Driver\ReadPreferencenull $options = null )final public MongoDB\Driver\Cursor executeQuery(string
$namespace , MongoDB\Driver\Query $query , arrayMongoDB\Driver\ReadPreferencenull $options = null )final public MongoDB\Driver\Cursor executeReadCommand(string
$db , MongoDB\Driver\Command $command , arraynull $options = null )final public MongoDB\Driver\Cursor executeReadWriteCommand(string
$db , MongoDB\Driver\Command $command , arraynull $options = null )final public MongoDB\Driver\Cursor executeWriteCommand(string
$db , MongoDB\Driver\Command $command , arraynull $options = null )final public string getHost()
final public array getInfo()
final public integernull getLatency()
final public int getPort()
final public MongoDB\Driver\ServerDescription getServerDescription()
final public array getTags()
final public int getType()
final public bool isArbiter()
final public bool isHidden()
final public bool isPassive()
final public bool isPrimary()
final public bool isSecondary()
}定義済み定数
変更履歴
The MongoDB\Driver\ServerDescription classはじめにThe MongoDB\Driver\ServerDescription class is a value object that represents a server to which the driver is connected. Instances of this class are returned by MongoDB\Driver\Server::getServerDescription and MongoDB\Driver\Monitoring\ServerChangedEvent methods. クラス概要MongoDB\Driver\ServerDescription
final
class MongoDB\Driver\ServerDescription
{
/* 定数 */
const
string
MongoDB\Driver\ServerDescription::TYPE_UNKNOWN = "Unknown";
const
string
MongoDB\Driver\ServerDescription::TYPE_STANDALONE = "Standalone";
const
string
MongoDB\Driver\ServerDescription::TYPE_MONGOS = "Mongos";
const
string
MongoDB\Driver\ServerDescription::TYPE_POSSIBLE_PRIMARY = "PossiblePrimary";
const
string
MongoDB\Driver\ServerDescription::TYPE_RS_PRIMARY = "RSPrimary";
const
string
MongoDB\Driver\ServerDescription::TYPE_RS_SECONDARY = "RSSecondary";
const
string
MongoDB\Driver\ServerDescription::TYPE_RS_ARBITER = "RSArbiter";
const
string
MongoDB\Driver\ServerDescription::TYPE_RS_OTHER = "RSOther";
const
string
MongoDB\Driver\ServerDescription::TYPE_RS_GHOST = "RSGhost";
const
string
MongoDB\Driver\ServerDescription::TYPE_LOAD_BALANCER = "LoadBalancer";
/* メソッド */
final public array getHelloResponse()
final public string getHost()
final public int getLastUpdateTime()
final public int getPort()
final public intnull getRoundTripTime()
final public string getType()
}定義済み定数
The MongoDB\Driver\TopologyDescription classはじめにThe MongoDB\Driver\TopologyDescription class is a value object that represents a topology to which the driver is connected. Instances of this class are returned by MongoDB\Driver\Monitoring\TopologyChangedEvent methods. クラス概要MongoDB\Driver\TopologyDescription
final
class MongoDB\Driver\TopologyDescription
{
/* 定数 */
const
string
MongoDB\Driver\TopologyDescription::TYPE_UNKNOWN = "Unknown";
const
string
MongoDB\Driver\TopologyDescription::TYPE_SINGLE = "Single";
const
string
MongoDB\Driver\TopologyDescription::TYPE_SHARDED = "Sharded";
const
string
MongoDB\Driver\TopologyDescription::TYPE_REPLICA_SET_NO_PRIMARY = "ReplicaSetNoPrimary";
const
string
MongoDB\Driver\TopologyDescription::TYPE_REPLICA_SET_WITH_PRIMARY = "ReplicaSetWithPrimary";
const
string
MongoDB\Driver\TopologyDescription::TYPE_LOAD_BALANCED = "LoadBalanced";
/* メソッド */
final public array getServers()
final public string getType()
final public bool hasReadableServer(MongoDB\Driver\ReadPreferencenull
$readPreference = null )final public bool hasWritableServer()
}定義済み定数
The MongoDB\Driver\WriteConcernError classはじめにThe MongoDB\Driver\WriteConcernError class encapsulates information about a write concern error and may be returned by MongoDB\Driver\WriteResult::getWriteConcernError. クラス概要MongoDB\Driver\WriteConcernError
final
class MongoDB\Driver\WriteConcernError
{
/* メソッド */
final public int getCode()
final public objectnull getInfo()
final public string getMessage()
}The MongoDB\Driver\WriteError classはじめにThe MongoDB\Driver\WriteError class encapsulates information about a write error and may be returned as an array element from MongoDB\Driver\WriteResult::getWriteErrors. クラス概要MongoDB\Driver\WriteError
final
class MongoDB\Driver\WriteError
{
/* メソッド */
final public int getCode()
final public int getIndex()
final public objectnull getInfo()
final public string getMessage()
}The MongoDB\Driver\WriteResult classはじめにThe MongoDB\Driver\WriteResult class encapsulates information about an executed MongoDB\Driver\BulkWrite and may be returned by MongoDB\Driver\Manager::executeBulkWrite. クラス概要MongoDB\Driver\WriteResult
final
class MongoDB\Driver\WriteResult
{
/* メソッド */
final public intnull getDeletedCount()
final public intnull getInsertedCount()
final public intnull getMatchedCount()
final public intnull getModifiedCount()
final public MongoDB\Driver\Server getServer()
final public intnull getUpsertedCount()
final public array getUpsertedIds()
final public MongoDB\Driver\WriteConcernErrornull getWriteConcernError()
final public array getWriteErrors()
final public bool isAcknowledged()
} |