downloads | documentation | faq | getting help | mailing lists | licenses | wiki | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

Mongo::dropDB> <Mongo::connectUtil
Last updated: Fri, 20 Nov 2009

view this page in

Mongo::__construct

(PECL mongo >=0.9.0)

Mongo::__constructCreates a new database connection object

Description

Mongo::__construct ([ string $server = NULL [, boolean $connect = TRUE [, boolean $persistent = FALSE [, boolean $paired = FALSE ]]]] )

If no parameters are passed, this connects to "localhost:27017" and returns.

If you elect not to connect immediately ($connect is FALSE), you will need to call connect(), persistConnect(), pairConnect(), or pairPersistConnect() before doing any database operations.

<?php

$mongo 
= new Mongo("localhost"false);

// throws a MongoException, as $mongo has not been fully initialized yet
$mongo->selectDB("foo")->command(array("distinct" => "bar""key" => "age"));

// okay
$mongo->connect();
$mongo->selectDB("foo")->command(array("distinct" => "bar""key" => "age"));

?>

Parameters

server

The server name.

connect

If the constructor should connect to the database now. If this is FALSE, the constructor will return without connecting to the database.

persistent

If the connection should be persistent.

paired

If the connection should be paired.

Return Values

Returns a new database connection object.

Errors/Exceptions

Throws MongoConnectionException if it tries and fails to connect to the database.

Examples

Example #1 Mongo::__construct() paired connection example

This example shows how to connect the driver to a replica pair of Mongo servers.

<?php

// pass a comma-separated list of server names to the constructor
$m = new Mongo("www.example1.com,www.example2.com"false);
$m->pairConnect();

// if the database servers are not running on the default port (27017),
// you'll need to specify the port
$m2 = new Mongo("www.example1.com:12345,www.example.com:54321"false);
$m2->pairConnect();

// you can also do a paired connection in one line of code
$m3 = new Mongo("www.example1.com,www.example.com"truefalsetrue);

?>

Example #2 Mongo::__construct() persistent connection example

A persistent connection will last for more than one request (usually... milage may vary depending on server). It can save significant time to reuse connections, as creating a connection is a time-intensive process.

A persistent connection is identified by the server string and optional "username" and "password" strings. These strings are arbitrary and should not be sensitive (i.e., a real password). They are merely indended as unique identifiers for a connection.

<?php

// creates a persistent connection
$m1 = new Mongo("localhost"truetrue);

// uses the same connection as $m1
$m2 = new Mongo("localhost"false);
$m2->persistConnect();

// creates a new connection
$m3 = new Mongo("127.0.0.1"false);
$m3->persistConnect();

// creates a new connection
$m4 = new Mongo("127.0.0.1:27017"false);
$m4->persistConnect();

// creates a new connection
$m5 = new Mongo("localhost"false);
$m5->persistConnect("foo");

// uses the $m5 connection
$m6 = new Mongo("localhost"false);
$m6->persistConnect("foo");

// creates a new connection
$m7 = new Mongo("localhost"false);
$m7->persistConnect("foo""bar");

?>


add a note add a note User Contributed Notes
Mongo::__construct
There are no user contributed notes for this page.

Mongo::dropDB> <Mongo::connectUtil
Last updated: Fri, 20 Nov 2009
 
 
show source | credits | sitemap | contact | advertising | mirror sites