PHPKonf: Istanbul PHP Conference 2017

The MongoDB class

(PECL mongo >=0.9.0)


Instances of this class are used to interact with a database. To get a database:

Пример #1 Selecting a database


= new MongoClient(); // connect
$db $m->selectDB("example");

Database names can use almost any character in the ASCII range. However, they cannot contain " ", "." or be the empty string. The name "system" is also reserved.

A few unusual, but valid, database names: "null", "[x,y]", "3", "\"", "/".

Unlike collection names, database names may contain "$".

Обзор классов

MongoDB {
/* Константы */
const int PROFILING_OFF = 0 ;
const int PROFILING_SLOW = 1 ;
const int PROFILING_ON = 2 ;
/* Fields */
public integer $w = 1 ;
public integer $wtimeout = 10000 ;
/* Методы */
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 )

Предопределенные константы

MongoDB Log Levels

Profiling is off.
Profiling is on for slow operations (>100 ms).
Profiling is on for all operations.



The number of servers to replicate a change to before returning success. Inherited by instances of MongoCollection derived from this. w functionality is only available in version 1.5.1+ of the MongoDB server and 1.0.8+ of the driver.

w is used whenever you need to adjust the acknowledgement level (MongoCollection::insert(), MongoCollection::update(), MongoCollection::remove(), MongoCollection::save(), and MongoCollection::ensureIndex() all support this option). With the default value (1), an acknowledged operation will return once the database server has the operation. If the server goes down before the operation has been replicated to a secondary, it is possible to lose the operation forever. Thus, you can specify w to be higher than one and guarantee that at least one secondary has the operation before it is considered successful.

For example, if w is 2, the primary and one secondary must have a record of the operation or the driver will throw a MongoCursorException. It is tempting to set w to the total number of secondaries + primary, but then if one secondary is down the operation will fail and an exception will be thrown, so usually w=2 is safest (primary and one secondary).


The number of milliseconds to wait for MongoDB::$w replications to take place. Inherited by instances of MongoCollection derived from this. w functionality is only available in version 1.5.1+ of the MongoDB server and 1.0.8+ of the driver.

Unless wtimeout is set, the server waits forever for replicating to w servers to finish. The driver defaults to waiting for 10 seconds, you can change this value to alter its behavior.

Смотрите также

MongoDB core docs on » databases.


add a note add a note

User Contributed Notes 3 notes

jeromakay at yahoo dot com
5 years ago
based on what I've read and then applied, you don't have to specifically create a database or table, you just initialize it.

Indeed, files are not being written inside /data/db, but they will the first moment you start adding data.

So, I'm taking as an example Twitter, with no db defined, I'm still going to have the db available if I run this code:




$m = new Mongo(); // connect
$db = $m->selectDB("example");
catch (
MongoConnectionException $e )
'<p>Couldn\'t connect to mongodb, is the "mongo" process running?</p>';

$updates = file_get_contents( "". TWITTER_API_VERSION ."/statuses/public_timeline.json" );
$updates = json_decode( $updates );

if (
$updates && is_array( $updates ) && count( $updates ) )
    foreach (
$updates as $update )
$db->users->insert( $update );


Hope this was helpful!

Good luck!
Vladimir Ghetau
careddumattia at gmail dot com
1 year ago
mongo extension is deprecated.

pecl install mongodb
m dot espositoii at yahoo dot com
5 years ago
With Mongo it'll automatically create the collection, so just start using it and it'll do the creation itself.

In other words... just use SelectCollection, if it doesn't exist, it will after that so you can drop it.
To Top