MongoDB driver classesTable of Contents
The MongoDB\Driver\Manager classIntroductionThe 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. Class synopsisMongoDB\Driver\Manager
final
class MongoDB\Driver\Manager
{
/* Methods */
final public __construct
([ string
$uri = "mongodb://127.0.0.1/
[, array $uriOptions = []
[, array $driverOptions = []
]]] )
final public MongoDB\Driver\WriteResult executeBulkWrite
( string
$namespace
, MongoDB\Driver\BulkWrite $bulk
[, MongoDB\Driver\WriteConcern $writeConcern
] )
final public MongoDB\Driver\Cursor executeCommand
( string
$db
, MongoDB\Driver\Command $command
[, MongoDB\Driver\ReadPreference $readPreference
] )
final public MongoDB\Driver\Cursor executeQuery
( string
$namespace
, MongoDB\Driver\Query $query
[, MongoDB\Driver\ReadPreference $readPreference
] )
final public MongoDB\Driver\ReadConcern getReadConcern
( void
)
final public MongoDB\Driver\ReadPreference getReadPreference
( void
)
final public array getServers
( void
)
final public MongoDB\Driver\WriteConcern getWriteConcern
( void
)
final public MongoDB\Driver\Server selectServer
( MongoDB\Driver\ReadPreference
}$readPreference
)ExamplesExample #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 The above example will output something similar to: object(MongoDB\Driver\Manager)#1 (3) { ["request_id"]=> int(1714636915) ["uri"]=> string(25) "mongodb://localhost:27017" ["cluster"]=> array(13) { ["mode"]=> string(6) "direct" ["state"]=> string(4) "born" ["request_id"]=> int(0) ["sockettimeoutms"]=> int(300000) ["last_reconnect"]=> int(0) ["uri"]=> string(25) "mongodb://localhost:27017" ["requires_auth"]=> int(0) ["nodes"]=> array(...) ["max_bson_size"]=> int(16777216) ["max_msg_size"]=> int(50331648) ["sec_latency_ms"]=> int(15) ["peers"]=> array(0) { } ["replSet"]=> NULL } } The MongoDB\Driver\Command classIntroductionThe MongoDB\Driver\Command class is a value object that represents a database command.
To provide Class synopsisMongoDB\Driver\Command
final
class MongoDB\Driver\Command
{
/* Methods */
final public __construct
( array|object
}$document
)ExamplesExample #1 Composing MongoDB\Driver\Command to provide a helper to create collections
<?php The above example will output: 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 classIntroductionThe MongoDB\Driver\Query class is a value object that represents a database query. Class synopsisMongoDB\Driver\Query
final
class MongoDB\Driver\Query
{
/* Methods */
final public __construct
( array|object
}$filter
[, array $queryOptions
] )The MongoDB\Driver\BulkWrite classIntroductionThe 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. Class synopsisMongoDB\Driver\BulkWrite
final
class MongoDB\Driver\BulkWrite
implements
Countable
{
/* Methods */
public __construct
([ array
$options
] )
public int count
( void
)
public void delete
( array|object
$filter
[, array $deleteOptions
] )
public mixed insert
( array|object
$document
)
public void update
( array|object
}$filter
, array|object $newObj
[, array $updateOptions
] )ExamplesExample #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. Example #2 Ordered write operations causing an error
<?php The above example will output: 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 The above example will output: 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) See Also
The MongoDB\Driver\WriteConcern classIntroductionMongoDB\Driver\WriteConcern describes the level of acknowledgement requested from MongoDB for write operations to a standalone mongod or to replica sets or to sharded clusters. In sharded clusters, mongos instances will pass the write concern on to the shards. Class synopsisMongoDB\Driver\WriteConcern
final
class MongoDB\Driver\WriteConcern
implements
MongoDB\BSON\Serializable
{
/* Constants */
const
string
MongoDB\Driver\WriteConcern::MAJORITY
= "majority"
;
/* Methods */
final public object bsonSerialize
( void
)
final public __construct
( string|int
$w
[, integer $wtimeout
[, boolean $journal
]] )
final public bool|null getJournal
( void
)
final public string|int|null getW
( void
)
final public int getWtimeout
( void
)
}Predefined Constants
The MongoDB\Driver\ReadPreference classIntroduction
Class synopsisMongoDB\Driver\ReadPreference
final
class MongoDB\Driver\ReadPreference
implements
MongoDB\BSON\Serializable
{
/* Constants */
const
integer
MongoDB\Driver\ReadPreference::RP_PRIMARY
= 1
;
const
integer
MongoDB\Driver\ReadPreference::RP_PRIMARY_PREFERRED
= 5
;
const
integer
MongoDB\Driver\ReadPreference::RP_SECONDARY
= 2
;
const
integer
MongoDB\Driver\ReadPreference::RP_SECONDARY_PREFERRED
= 6
;
const
integer
MongoDB\Driver\ReadPreference::RP_NEAREST
= 10
;
const
integer
MongoDB\Driver\ReadPreference::NO_MAX_STALENESS
= -1
;
const
integer
MongoDB\Driver\ReadPreference::SMALLEST_MAX_STALENESS_SECONDS
= 90
;
/* Methods */
final public object bsonSerialize
( void
)
final public __construct
( int
$mode
[, array $tagSets = NULL
[, array $options = []
]] )
final public integer getMaxStalenessSeconds
( void
)
final public integer getMode
( void
)
final public array getTagSets
( void
)
}Predefined Constants
Changelog
The MongoDB\Driver\ReadConcern classIntroductionMongoDB\Driver\ReadConcern controls the level of isolation for read operations for replica sets and replica set shards. This option requires the WiredTiger storage engine and MongoDB 3.2 or later. Class synopsisMongoDB\Driver\ReadConcern
final
class MongoDB\Driver\ReadConcern
implements
MongoDB\BSON\Serializable
{
/* Constants */
const
string
MongoDB\Driver\ReadConcern::LINEARIZABLE
= "linearizable"
;
const
string
MongoDB\Driver\ReadConcern::LOCAL
= "local"
;
const
string
MongoDB\Driver\ReadConcern::MAJORITY
= "majority"
;
/* Methods */
final public object bsonSerialize
( void
)
final public __construct
([ string
$level
] )
final public string|null getLevel
( void
)
}Predefined Constants
Changelog
The MongoDB\Driver\Cursor classIntroductionThe 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.
Class synopsisMongoDB\Driver\Cursor
class MongoDB\Driver\Cursor
implements
Traversable
{
/* Methods */
final private __construct
( void
)
final public MongoDB\Driver\CursorId getId
( void
)
final public MongoDB\Driver\Server getServer
( void
)
final public bool isDead
( void
)
final public void setTypeMap
( array
$typemap
)
final public array toArray
( void
)
}The MongoDB\Driver\CursorId classIntroduction
Class synopsisMongoDB\Driver\CursorId
final
class MongoDB\Driver\CursorId
{
/* Methods */
final private __construct
( void
)
final public string __toString
( void
)
}The MongoDB\Driver\Server classIntroduction
Class synopsisMongoDB\Driver\Server
final
class MongoDB\Driver\Server
{
/* Constants */
const
integer
MongoDB\Driver\Server::TYPE_UNKNOWN
= 0
;
const
integer
MongoDB\Driver\Server::TYPE_STANDALONE
= 1
;
const
integer
MongoDB\Driver\Server::TYPE_MONGOS
= 2
;
const
integer
MongoDB\Driver\Server::TYPE_POSSIBLE_PRIMARY
= 3
;
const
integer
MongoDB\Driver\Server::TYPE_RS_PRIMARY
= 4
;
const
integer
MongoDB\Driver\Server::TYPE_RS_SECONDARY
= 5
;
const
integer
MongoDB\Driver\Server::TYPE_RS_ARBITER
= 6
;
const
integer
MongoDB\Driver\Server::TYPE_RS_OTHER
= 7
;
const
integer
MongoDB\Driver\Server::TYPE_RS_GHOST
= 8
;
/* Methods */
final private __construct
( void
)
final public MongoDB\Driver\WriteResult executeBulkWrite
( string
$namespace
, MongoDB\Driver\BulkWrite $bulk
[, MongoDB\Driver\WriteConcern $writeConcern
] )
final public MongoDB\Driver\Cursor executeCommand
( string
$db
, MongoDB\Driver\Command $command
[, MongoDB\Driver\ReadPreference $readPreference
] )
final public MongoDB\Driver\Cursor executeQuery
( string
$namespace
, MongoDB\Driver\Query $query
[, MongoDB\Driver\ReadPreference $readPreference
] )
final public string getHost
( void
)
final public array getInfo
( void
)
final public string getLatency
( void
)
final public int getPort
( void
)
final public array getTags
( void
)
final public int getType
( void
)
final public bool isArbiter
( void
)
final public bool isHidden
( void
)
final public bool isPassive
( void
)
final public bool isPrimary
( void
)
final public bool isSecondary
( void
)
}Predefined Constants
The MongoDB\Driver\WriteConcernError classIntroductionThe MongoDB\Driver\WriteConcernError class encapsulates information about a write concern error and may be returned by MongoDB\Driver\WriteResult::getWriteConcernError. Class synopsisMongoDB\Driver\WriteConcernError
final
class MongoDB\Driver\WriteConcernError
{
/* Methods */
final public int getCode
( void
)
final public mixed getInfo
( void
)
final public string getMessage
( void
)
}The MongoDB\Driver\WriteError classIntroductionThe MongoDB\Driver\WriteError class encapsulates information about a write error and may be returned as an array element from MongoDB\Driver\WriteResult::getWriteErrors. Class synopsisMongoDB\Driver\WriteError
final
class MongoDB\Driver\WriteError
{
/* Methods */
final public int getCode
( void
)
final public int getIndex
( void
)
final public mixed getInfo
( void
)
final public string getMessage
( void
)
}The MongoDB\Driver\WriteResult classIntroductionThe MongoDB\Driver\WriteResult class encapsulates information about an executed MongoDB\Driver\BulkWrite and may be returned by MongoDB\Driver\Manager::executeBulkWrite. Class synopsisMongoDB\Driver\WriteResult
final
class MongoDB\Driver\WriteResult
{
/* Methods */
final public int|null getDeletedCount
( void
)
final public int|null getInsertedCount
( void
)
final public int|null getMatchedCount
( void
)
final public int|null getModifiedCount
( void
)
final public MongoDB\Driver\Server getServer
( void
)
final public int|null getUpsertedCount
( void
)
final public array getUpsertedIds
( void
)
final public MongoDB\Driver\WriteConcernError|null getWriteConcernError
( void
)
final public array getWriteErrors
( void
)
final public bool isAcknowledged
( void
)
} |