function mongodb in MongoDB 7
Same name and namespace in other branches
- 6 mongodb.module \mongodb()
Returns an MongoDB object.
Parameters
string $alias: The name of a MongoDB connection alias. If it is not passed, or if the alias does not match a connection definition, the function will fall back to the 'default' alias.
int $retry: The number of retry attemps when a connection fails.
Return value
\MongoDB|\MongodbDummy A MongodbDummy is returned in case a MongoConnectionException is thrown.
Throws
\MongoConnectionException If the connection cannot be estaslished even after retries.
\InvalidArgumentException If the database cannot be selected.
\MongoConnectionException If the connection cannot be established.
See also
7 calls to mongodb()
- drush_mongodb_clean_tests in ./
mongodb.drush.inc - Drush callback: drop the Simpletest leftover collections.
- mongodb_collection in ./
mongodb.module - mongodb_next_id in ./
mongodb.module - Return the next id in a sequence.
- mongodb_queue_cron in mongodb_queue/
mongodb_queue.module - Implements hook_cron().
- mongodb_requirements in ./
mongodb.install - Implements hook_requirements().
8 string references to 'mongodb'
- MongoDbEntityFieldQueryTestCase::setUp in mongodb_field_storage/
mongodb_field_storage.test - Sets up a Drupal site for running functional and integration tests.
- MongoDBFieldAttachStorageTestCase::setUp in mongodb_field_storage/
mongodb_field_storage.test - Set the default field storage backend for fields created during tests.
- MongoDBLogTestCase::setUp in mongodb_watchdog/
mongodb_watchdog.test - Enable modules and create users with specific permissions.
- MongoDBSessionHttpsTestCase::setUp in mongodb_session/
mongodb_session.test - Sets up a Drupal site for running functional and integration tests.
- MongoDBSessionTestCase::setUp in mongodb_session/
mongodb_session.test - Sets up a Drupal site for running functional and integration tests.
File
- ./
mongodb.module, line 43 - Contains the main module connecting Drupal to MongoDB.
Code
function mongodb($alias = 'default', $retry = 3) {
static $mongo_objects;
$connections = variable_get('mongodb_connections', array());
if (!isset($connections[$alias])) {
$alias = 'default';
}
$connection = isset($connections[$alias]) ? $connections[$alias] : array();
$connection += array(
'host' => 'localhost',
'db' => 'drupal',
'connection_options' => array(),
);
$host = $connection['host'];
$db = $connection['db'];
$options = $connection['connection_options'] + array(
'connect' => TRUE,
'db' => $db,
);
if (!isset($mongo_objects[$host][$db])) {
try {
// Use the 1.3 client if available.
if (class_exists(MongoClient::class)) {
$mongo = new MongoClient($host, $options);
// Enable read preference and tags if provided. This can also be
// controlled on a per query basis at the cursor level if more control
// is required.
if (!empty($connection['read_preference'])) {
$tags = !empty($connection['read_preference']['tags']) ? $connection['read_preference']['tags'] : array();
$mongo
->setReadPreference($connection['read_preference']['preference'], $tags);
}
}
else {
$mongo = new Mongo($host, $options);
if (!empty($connection['slave_ok'])) {
$mongo
->setSlaveOkay(TRUE);
}
}
$mongo_objects[$host][$db] = $mongo
->selectDB($db);
$mongo_objects[$host][$db]->connection = $mongo;
} catch (MongoConnectionException $e) {
if ($retry > 0) {
return mongodb($alias, --$retry);
}
$mongo_objects[$host][$db] = new MongodbDummy();
throw $e;
}
}
return $mongo_objects[$host][$db];
}