Core ClassesTable of Contents
Warning
This extension is deprecated. Instead, the MongoDB extension should be used. The core classes are the most important part of the driver. The MongoClient classWarning
This extension that defines this class is deprecated. Instead, the MongoDB extension should be used. Alternatives to this class include:
IntroductionA connection manager for PHP and MongoDB. This class is used to create and manage connections. A typical use is: Example #1 MongoClient basic usage
<?php See MongoClient::__construct and the section on connecting for more information about creating connections. Class synopsisMongoClient
class MongoClient
{
/* Constants */
const
string
MongoClient::VERSION
;
const
string
MongoClient::DEFAULT_HOST
= "localhost"
;
const
int
MongoClient::DEFAULT_PORT
= 27017
;
const
string
MongoClient::RP_PRIMARY
= "primary"
;
const
string
MongoClient::RP_PRIMARY_PREFERRED
= "primaryPreferred"
;
const
string
MongoClient::RP_SECONDARY
= "secondary"
;
const
string
MongoClient::RP_SECONDARY_PREFERRED
= "secondaryPreferred"
;
const
string
MongoClient::RP_NEAREST
= "nearest"
;
/* Properties */
public
boolean
$connected
=
FALSE
;
public
string
$status
=
NULL
;
protected
string
$server
=
NULL
;
protected
boolean
$persistent
=
NULL
;/* Methods */
public __construct
([ string
$server = "mongodb://localhost:27017"
[, array $options = array("connect" => TRUE )
[, array $driver_options
]]] )
public bool close
([ boolean|string
$connection
] )
public bool connect
( void
)
public array dropDB
( mixed
$db
)
public MongoDB __get
( string
$dbname
)
public static array getConnections
( void
)
public array getHosts
( void
)
public array getReadPreference
( void
)
public array getWriteConcern
( void
)
public bool killCursor
( string
$server_hash
, int|MongoInt64 $id
)
public array listDBs
( void
)
public MongoCollection selectCollection
( string
$db
, string $collection
)
public MongoDB selectDB
( string
$name
)
public bool setReadPreference
( string
$read_preference
[, array $tags
] )
public bool setWriteConcern
( mixed
$w
[, int $wtimeout
] )
public string __toString
( void
)
}Predefined ConstantsMongoClient Constants
Fields
See Also
The MongoDB classIntroductionInstances of this class are used to interact with a database. To get a database: Example #1 Selecting a database
<?php A few unusual, but valid, database names: "null", "[x,y]", "3", "\"", "/". Unlike collection names, database names may contain "$". Class synopsisMongoDB
class MongoDB
{
/* Constants */
const
int
MongoDB::PROFILING_OFF
= 0
;
const
int
MongoDB::PROFILING_SLOW
= 1
;
const
int
MongoDB::PROFILING_ON
= 2
;
/* Fields */
public
integer
$w
= 1
;
public
integer
$wtimeout
= 10000
;
/* Methods */
public array authenticate
( string
$username
, string $password
)
public array command
( array
$command
[, array $options = array()
[, string &$hash
]] )
public __construct
( MongoClient
$conn
, string $name
)
public MongoCollection createCollection
( string
$name
[, array $options
] )
public array createDBRef
( string
$collection
, mixed $document_or_id
)
public array drop
( void
)
public array dropCollection
( mixed
$coll
)
public array execute
( mixed
$code
[, array $args = array()
] )
public bool forceError
( void
)
public MongoCollection __get
( string
$name
)
public array getCollectionInfo
([ array
$options = array()
] )
public array getCollectionNames
([ array
$options = array()
] )
public array getDBRef
( array
$ref
)
public MongoGridFS getGridFS
([ string
$prefix = "fs"
] )
public int getProfilingLevel
( void
)
public array getReadPreference
( void
)
public bool getSlaveOkay
( void
)
public array getWriteConcern
( void
)
public array lastError
( void
)
public array listCollections
([ array
$options = array()
] )
public array prevError
( void
)
public array repair
([ bool
$preserve_cloned_files = FALSE
[, bool $backup_original_files = FALSE
]] )
public array resetError
( void
)
public MongoCollection selectCollection
( string
$name
)
public int setProfilingLevel
( int
$level
)
public bool setReadPreference
( string
$read_preference
[, array $tags
] )
public bool setSlaveOkay
([ bool
$ok = true
] )
public bool setWriteConcern
( mixed
$w
[, int $wtimeout
] )
public string __toString
( void
)
}Predefined ConstantsMongoDB Log Levels
Fields
See AlsoMongoDB core docs on » databases. The MongoCollection classIntroductionRepresents a MongoDB collection. Collection names can use any character in the ASCII set. Some valid collection names are "", "...", "my collection", and "*&#@". User-defined collection names cannot contain the $ symbol. There are certain system collections which use a $ in their names (e.g., local.oplog.$main), but it is a reserved character. If you attempt to create and use a collection with a $ in the name, MongoDB will assert. Class synopsisMongoCollection
class MongoCollection
{
/* Constants */
const
int
MongoCollection::ASCENDING
= 1
;
const
int
MongoCollection::DESCENDING
= -1
;
/* Fields */
public
MongoDB
$db
=
NULL
;
public
integer
$w
;
public
integer
$wtimeout
;
/* Methods */
public array aggregate
( array
$pipeline
[, array $options
] )
public MongoCommandCursor aggregateCursor
( array
$command
[, array $options
] )
public mixed batchInsert
( array
$a
[, array $options = array()
] )
public __construct
( MongoDB
$db
, string $name
)
public int count
([ array
$query = array()
[, array $options = array()
]] )
public array createDBRef
( mixed
$document_or_id
)
public bool createIndex
( array
$keys
[, array $options = array()
] )
public array deleteIndex
( string|array
$keys
)
public array deleteIndexes
( void
)
public array distinct
( string
$key
[, array $query
] )
public array drop
( void
)
public bool ensureIndex
( string|array
$key|keys
[, array $options = array()
] )
public MongoCursor find
([ array
$query = array()
[, array $fields = array()
]] )
public array findAndModify
( array
$query
[, array $update
[, array $fields
[, array $options
]]] )
public array findOne
([ array
$query = array()
[, array $fields = array()
[, array $options = array()
]]] )
public MongoCollection __get
( string
$name
)
public array getDBRef
( array
$ref
)
public array getIndexInfo
( void
)
public string getName
( void
)
public array getReadPreference
( void
)
public bool getSlaveOkay
( void
)
public array getWriteConcern
( void
)
public array group
( mixed
$keys
, array $initial
, MongoCode $reduce
[, array $options = array()
] )
public bool|array insert
( array|object
$document
[, array $options = array()
] )
public array[MongoCommandCursor] parallelCollectionScan
( int
$num_cursors
)
public bool|array remove
([ array
$criteria = array()
[, array $options = array()
]] )
public mixed save
( array|object
$document
[, array $options = array()
] )
public bool setReadPreference
( string
$read_preference
[, array $tags
] )
public bool setSlaveOkay
([ bool
$ok = true
] )
public bool setWriteConcern
( mixed
$w
[, int $wtimeout
] )
static protected string toIndexString
( mixed
$keys
)
public string __toString
( void
)
public bool|array update
( array
$criteria
, array $new_object
[, array $options = array()
] )
public array validate
([ bool
}$scan_data = FALSE
] )Predefined Constants
Fields
See AlsoMongoDB core docs on » collections. The MongoCursor classIntroductionA cursor is used to iterate through the results of a database query. For example, to query the database and see all results, you could do: Example #1 MongoCursor basic usage
<?php You don't generally create cursors using the MongoCursor constructor, you get a new cursor by calling MongoCollection::find (as shown above). Suppose that, in the example above, $collection was a 50GB collection. We certainly wouldn't want to load that into memory all at once, which is what a cursor is for: allowing the client to access the collection in dribs and drabs. If we have a large result set, we can iterate through it, loading a few megabytes of results into memory at a time. For example, we could do: Example #2 Iterating over MongoCursor
<?php Note that this means that a cursor does not "contain" the database results, it just manages them. Thus, if you print a cursor (with, say, var_dump or print_r), you'll just get the cursor object, not your documents. To get the documents themselves, you can use one of the methods shown above. Cursor StagesA MongoCursor has two "life stages": pre- and post- query. When a cursor is created, it has not yet contacted the database, so it is in its pre-query state. In this state, the client can further specify what they want the query to do, including adding limits, skips, sorts, and more advanced options. When the client attempts to get a result (by calling MongoCursor::next, directly or indirectly), the cursor moves into the post-query stage. At this point, the query has been executed by the database and cannot be modified anymore.
Example #3 Adding options to MongoCursor
<?php Class synopsisMongoCursor
class MongoCursor
implements
MongoCursorInterface
,
Iterator
{
/* Static Fields */
static
boolean
$slaveOkay
=
FALSE
;
static
integer
$timeout
= 30000
;
/* Methods */
public MongoCursor addOption
( string
$key
, mixed $value
)
public MongoCursor awaitData
([ bool
$wait = true
] )
public MongoCursor batchSize
( int
$batchSize
)
public __construct
( MongoClient
$connection
, string $ns
[, array $query = array()
[, array $fields = array()
]] )
public int count
([ bool
$foundOnly = FALSE
] )
public array current
( void
)
public bool dead
( void
)
protected void doQuery
( void
)
public array explain
( void
)
public MongoCursor fields
( array
$f
)
public array getNext
( void
)
public array getReadPreference
( void
)
public bool hasNext
( void
)
public MongoCursor hint
( mixed
$index
)
public MongoCursor immortal
([ bool
$liveForever = true
] )
public array info
( void
)
public string|int key
( void
)
public MongoCursor limit
( int
$num
)
public MongoCursor maxTimeMS
( int
$ms
)
public array next
( void
)
public MongoCursor partial
([ bool
$okay = true
] )
public void reset
( void
)
public void rewind
( void
)
public MongoCursor setFlag
( int
$flag
[, bool $set = true
] )
public MongoCursor setReadPreference
( string
$read_preference
[, array $tags
] )
public MongoCursor skip
( int
$num
)
public MongoCursor slaveOkay
([ bool
$okay = true
] )
public MongoCursor snapshot
( void
)
public MongoCursor sort
( array
$fields
)
public MongoCursor tailable
([ bool
$tail = true
] )
public MongoCursor timeout
( int
$ms
)
public bool valid
( void
)
}Static Variables
See AlsoMongoDB core docs on » cursors. The MongoCursorInterface interfaceIntroductionInterface for cursors, which can be used to iterate through results of a database query or command. This interface is implemented by the MongoCursor and MongoCommandCursor classes.
Class synopsisMongoCursorInterface
class MongoCursorInterface
extends
Iterator
{
/* Methods */
abstract public MongoCursorInterface batchSize
( int
$batchSize
)
abstract public bool dead
( void
)
abstract public array getReadPreference
( void
)
abstract public array info
( void
)
abstract public MongoCursorInterface setReadPreference
( string
$read_preference
[, array $tags
] )
abstract public MongoCursorInterface timeout
( int
$ms
)/* Inherited methods */
abstract public mixed Iterator::current
( void
)
abstract public scalar Iterator::key
( void
)
abstract public void Iterator::next
( void
)
abstract public void Iterator::rewind
( void
)
abstract public boolean Iterator::valid
( void
)
}The MongoCommandCursor classIntroductionA command cursor is similar to a MongoCursor except that you use it for iterating through the results of a database command instead of a normal query. Command cursors are useful for iterating over large result sets that might exceed the document size limit (currently 16MB) of a single MongoDB::command response. While you can create command cursors using MongoCommandCursor::__construct or the MongoCommandCursor::createFromDocument factory method, you will generally want to use command-specific helpers such as MongoCollection::aggregateCursor. Note that the cursor does not "contain" the database command's results; it just manages iteration through them. Thus, if you print a cursor (f.e. with var_dump or print_r), you will see the cursor object but not the result documents. Cursor StagesA MongoCommandCursor has two "life stages": pre- and post- command. When a cursor is created, it has not yet contacted the database, so it is in its pre-command state. When the client first attempts to get a result (by calling MongoCommandCursor::rewind, directly or indirectly), the cursor moves into the post-command state. The command cursor's batch size and socket timeout may be configured in both the pre- and post- command states.
Example #1 Adding options to MongoCommandCursor
<?php Class synopsisMongoCommandCursor
class MongoCommandCursor
implements
MongoCursorInterface
,
Iterator
{
/* Methods */
public MongoCommandCursor batchSize
( int
$batchSize
)
public __construct
( MongoClient
$connection
, string $ns
, array $command = array()
)
public static MongoCommandCursor createFromDocument
( MongoClient
$connection
, string $hash
, array $document
)
public array current
( void
)
public bool dead
( void
)
public array getReadPreference
( void
)
public array info
( void
)
public int key
( void
)
public void next
( void
)
public array rewind
( void
)
public MongoCommandCursor setReadPreference
( string
$read_preference
[, array $tags
] )
public MongoCommandCursor timeout
( int
$ms
)
public bool valid
( void
)
}See Also
|