public function ensureIndex($keys, $options = array()) { try { $options = array_merge($options, array('safe' => TRUE)); return parent::ensureIndex($keys, $options); } catch (MongoCursorException $e) { show_error("MongoDB ensureIndex failed: {$e->getMessage()}", 500); } }
public function testGetIndexInfo() { $info = $this->object->getIndexInfo(); $this->assertEquals(count($info), 0); $this->object->ensureIndex((object) array('foo' => 1)); $this->object->ensureIndex((object) array('foo' => -1)); $this->object->ensureIndex((object) array('bar' => 1, 'baz' => -1)); $info = $this->object->getIndexInfo(); $this->assertEquals(4, count($info), json_encode($info)); $this->assertEquals($info[1]['key']['foo'], 1); $this->assertEquals($info[1]['name'], 'foo_1'); $this->assertEquals($info[2]['key']['foo'], -1); $this->assertEquals($info[2]['name'], 'foo_-1'); $this->assertEquals($info[3]['key']['bar'], 1); $this->assertEquals($info[3]['key']['baz'], -1); $this->assertEquals($info[3]['name'], 'bar_1_baz_-1'); }
private function __initMongoSession() { // generate server connection strings $connections = array(); if (!empty($this->_settings['servers'])) { foreach ($this->_settings['servers'] as $server) { $str = ''; if (!empty($server['username']) && !empty($server['password'])) { $str .= $server['username'] . ':' . $server['password'] . '@'; } $str .= !empty($server['host']) ? $server['host'] : Mongo::DEFAULT_HOST; $str .= ':' . (!empty($server['port']) ? (int) $server['port'] : Mongo::DEFAULT_PORT); array_push($connections, $str); } } else { // use default connection settings array_push($connections, Mongo::DEFAULT_HOST . ':' . Mongo::DEFAULT_PORT); } // add immediate connection $opts = array('connect' => true); // support persistent connections if ($this->_settings['persistent'] && !empty($this->_settings['persistentId'])) { $opts['persist'] = $this->_settings['persistentId']; } // support replica sets if ($this->_settings['replicaSet']) { $opts['replicaSet'] = true; } // load mongo server connection try { $this->_connection = new Mongo('mongodb://' . implode(',', $connections), $opts); } catch (Exception $e) { throw new Exception('Can\'t connect to the MongoDB server.'); } // load the db try { $mongo = $this->_connection->selectDB($this->_settings['database']); } catch (InvalidArgumentException $e) { throw new Exception('The MongoDB database specified in the config does not exist.'); } // load collection try { $this->_mongo = $mongo->selectCollection($this->_settings['collection']); } catch (Exception $e) { throw new Exception('The MongoDB collection specified in the config does not exist.'); } // proper indexing on the expiration $this->_mongo->ensureIndex(array('expiry' => 1), array('name' => 'expiry', 'unique' => true, 'dropDups' => true, 'safe' => true)); // proper indexing of session id and lock $this->_mongo->ensureIndex(array('session_id' => 1, 'lock' => 1), array('name' => 'session_id', 'unique' => true, 'dropDups' => true, 'safe' => true)); }