當前位置: 首頁>>代碼示例>>PHP>>正文


PHP Mongo::ensureIndex方法代碼示例

本文整理匯總了PHP中Mongo::ensureIndex方法的典型用法代碼示例。如果您正苦於以下問題:PHP Mongo::ensureIndex方法的具體用法?PHP Mongo::ensureIndex怎麽用?PHP Mongo::ensureIndex使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在Mongo的用法示例。


在下文中一共展示了Mongo::ensureIndex方法的3個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的PHP代碼示例。

示例1: ensureIndex

 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);
     }
 }
開發者ID:navruzm,項目名稱:navruz.net,代碼行數:9,代碼來源:Mongo_db.php

示例2: testGetIndexInfo

 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');
 }
開發者ID:redmeadowman,項目名稱:mongo-php-driver,代碼行數:17,代碼來源:MongoObjectsTest.php

示例3: __initMongoSession

 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));
 }
開發者ID:radig,項目名稱:CakeMongoSession,代碼行數:51,代碼來源:cake_mongo_session.php


注:本文中的Mongo::ensureIndex方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。