本文整理汇总了PHP中MongoCollection::ensureIndex方法的典型用法代码示例。如果您正苦于以下问题:PHP MongoCollection::ensureIndex方法的具体用法?PHP MongoCollection::ensureIndex怎么用?PHP MongoCollection::ensureIndex使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类MongoCollection
的用法示例。
在下文中一共展示了MongoCollection::ensureIndex方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: __construct
public function __construct()
{
$mongo = new Mongo();
$mongoDb = $mongo->selectDB('ContactsManager');
$this->contactsManagerCollection = new MongoCollection($mongoDb, 'Contact');
$this->contactsManagerCollection->ensureIndex(array('email' => 1), array('unique' => true, 'dropDups' => true));
}
示例2: __construct
/**
* Construct a new instance of MongoCache.
*
* @param \MongoCollection $collection The collection containing the cached data.
* @param integer $defaultTimeToLive The default time to live in seconds.
*
* @throws \RuntimeException Throw if mongo extension is not loaded.
* @throws \InvalidArgumentException Throw if $defaultTimeToLive is not an integer between 0 and 86400.
*/
public function __construct(\MongoCollection $collection, $defaultTimeToLive = CacheInterface::MAX_TTL)
{
if (!extension_loaded('mongo')) {
throw new \RuntimeException('The mongo extension is required for MongoCache');
}
$this->setDefaultTTL($defaultTimeToLive);
$this->collection = $collection;
$this->collection->ensureIndex(['expires' => 1], ['expireAfterSeconds' => 0, 'background' => true]);
}
示例3: connect
/**
* Connects to mongodb.
*
* @return \MongoCollection
*/
public function connect()
{
if (!$this->collection) {
$client = new \MongoClient($this->server);
$db = $client->selectDB($this->db);
$this->collection = $db->selectCollection('mc_info_sms_subscription_low_balance_reminder_logs');
$this->collection->ensureIndex(array('mobile' => 1), array('unique' => true));
}
return $this->collection;
}
示例4: __construct
public function __construct($server = 'mongodb://localhost:27017', $db = 'phpconsole', $collection = 'phpconsole')
{
$this->mongoClient = new \MongoClient($server);
if (!$this->mongoClient) {
throw new \Exception('Unable to connect to MongoDB server');
}
$this->mongoCollection = $this->mongoClient->selectCollection($db, $collection);
if (!$this->mongoCollection) {
throw new \Exception('Unable to get collection');
}
$this->mongoCollection->ensureIndex(array('expireAt' => 1), array('background' => true, 'name' => 'TTL', 'expireAfterSeconds' => 0));
}
示例5: __construct
/**
* Sets up the MongoDB table and
* initialises the MongoDB connection
*
* @param array $config configuration
* @throws Cache_Exception
*/
protected function __construct(array $config)
{
parent::__construct($config);
$database = Arr::get($this->_config, 'database', NULL);
if ($database === NULL) {
throw new Cache_Exception('Database path not available in Kohana Cache configuration');
}
$this->_client = new MongoClient('mongodb://' . $this->_config['host'] . ':' . $this->_config['port']);
$this->_database = $this->_client->{$database};
$this->_collection = $this->_database->{$this->_config['collection']};
$this->_collection->ensureIndex(array('tags' => 1));
$this->_collection->ensureIndex(array('lifetime' => 1));
}
示例6: __construct
/**
* @return void
*/
public function __construct($options)
{
if (!extension_loaded('mongo')) {
\Zend_Cache::throwException('The MongoDB extension must be loaded for using this backend !');
}
parent::__construct($options);
// Merge the options passed in; overridding any default options
$this->_options = array_merge($this->_options, $options);
$this->_conn = new \MongoClient($this->getServerConnectionUrl());
$this->_db = $this->_conn->selectDB($this->_options['dbname']);
$this->_collection = $this->_db->selectCollection($this->_options['collection']);
$this->_collection->ensureIndex(array('t' => 1), array('background' => true));
$this->_collection->ensureIndex(array('expires_at' => 1), array('background' => true));
}
示例7: __construct
/**
* @return void
*/
public function __construct($options)
{
if (!extension_loaded('mongo') && !extension_loaded('mongodb')) {
\Zend_Cache::throwException('The MongoDB extension must be loaded for using this backend !');
}
parent::__construct($options);
// Merge the options passed in; overridding any default options
$this->_options = array_merge($this->_options, $options);
$this->_conn = new \MongoClient('mongodb://' . $this->_options['host'] . ':' . $this->_options['port'], $this->_options['optional']);
$this->_db = $this->_conn->selectDB($this->_options['dbname']);
$this->_collection = $this->_db->selectCollection($this->_options['collection']);
$this->_collection->ensureIndex(['t' => 1], ['background' => true]);
$this->_collection->ensureIndex(['expire' => 1], ['background' => true]);
}
示例8: index
/**
* Specify an index for the collection.
*
* @param string|array $columns
* @param array $options
* @return Blueprint
*/
public function index($columns = null, $options = [])
{
$columns = $this->fluent($columns);
// Columns are passed as a default array.
if (is_array($columns) && is_int(key($columns))) {
// Transform the columns to the required array format.
$transform = [];
foreach ($columns as $column) {
$transform[$column] = 1;
}
$columns = $transform;
}
$this->collection->ensureIndex($columns, $options);
return $this;
}
示例9: __construct
/**
* Constructor. Sets the Mongo DB adapter.
*
* @param \MongoClient $Mongo A \MongoClient instance.
* @param array $options Array of options.
*/
public function __construct(\MongoClient $Mongo, array $options = null)
{
// default options
$this->options['db_name'] = 'apix';
$this->options['collection_name'] = 'cache';
$this->options['object_serializer'] = 'php';
// null, php, json, igBinary.
// Set the adapter and merge the user+default options
parent::__construct($Mongo, $options);
$this->db = $this->adapter->selectDB($this->options['db_name']);
$this->collection = $this->db->createCollection($this->options['collection_name'], false);
$this->collection->ensureIndex(array('key' => 1), array('unique' => true, 'dropDups' => true));
// Using MongoDB TTL collections (MongoDB 2.2+)
$this->collection->ensureIndex(array('expire' => 1), array('expireAfterSeconds' => 1));
$this->setSerializer($this->options['object_serializer']);
}
示例10: ensureIndex
/**
* Wrapper method for MongoCollection::ensureIndex().
*
* @see http://php.net/manual/en/mongocollection.ensureindex.php
* @param array $keys
* @param array $options
* @return array|boolean
*/
public function ensureIndex(array $keys, array $options = array())
{
$options = isset($options['safe']) ? $this->convertWriteConcern($options) : $options;
$options = isset($options['timeout']) ? $this->convertSocketTimeout($options) : $options;
$options = isset($options['wtimeout']) ? $this->convertWriteTimeout($options) : $options;
return $this->mongoCollection->ensureIndex($keys, $options);
}
示例11: createIndex
/**
* @param array $index
*/
public function createIndex($index, $options = [])
{
if (!count($options)) {
$options = ['name' => $this->table . 'TextIndex'];
}
$this->collection->ensureIndex($index, $options);
}
示例12: _ensureIndex
/**
* Ensure index of correct specification and a unique name whether the specification or name already exist or not.
* Will not create index if $index is a prefix of an existing index
*
* @param array $index index to create in same format as \MongoCollection::ensureIndex()
*
* @return void
*
* @throws \Exception couldnt create index after 5 attempts
*/
private function _ensureIndex(array $index)
{
//if $index is a prefix of any existing index we are good
foreach ($this->_collection->getIndexInfo() as $existingIndex) {
$slice = array_slice($existingIndex['key'], 0, count($index), true);
if ($slice === $index) {
return;
}
}
for ($i = 0; $i < 5; ++$i) {
for ($name = uniqid(); strlen($name) > 0; $name = substr($name, 0, -1)) {
//creating an index with same name and different spec does nothing.
//creating an index with same spec and different name does nothing.
//so we use any generated name, and then find the right spec after we have called, and just go with that name.
try {
$this->_collection->ensureIndex($index, array('name' => $name, 'background' => true));
} catch (\MongoException $e) {
//this happens when the name was too long, let continue
}
foreach ($this->_collection->getIndexInfo() as $existingIndex) {
if ($existingIndex['key'] === $index) {
return;
}
}
}
}
throw new \Exception('couldnt create index after 5 attempts');
}
示例13: createIndex
/**
* Create an index.
*
* Effectively, just a wrapper around the collections own ensureIndex method.
*
* @param array $keys
* An array specifying the index's fields as its keys. For each field, the
* value is either the index direction or » index type. If specifying
* direction, specify 1 for ascending or -1 for descending.
* @param array $options
* (Optional) An array of options for the index creation.
*
* @throws \Vultan\Exception\VultanIndexException
* @see http://www.php.net/manual/en/mongocollection.createindex.php
*/
public function createIndex(array $keys, array $options = array())
{
try {
$this->collection->ensureIndex($keys, $options);
} catch (\MongoException $e) {
throw new VultanIndexException('Unable to create index: ' . $e->getMessage());
}
}
示例14: initialise
/**
* Initialises the store instance for use.
*
* Once this has been done the cache is all set to be used.
*
* @param cache_definition $definition
* @throws coding_exception
*/
public function initialise(cache_definition $definition)
{
if ($this->is_initialised()) {
throw new coding_exception('This mongodb instance has already been initialised.');
}
$this->database = $this->connection->selectDB($this->databasename);
$this->definitionhash = $definition->generate_definition_hash();
$this->collection = $this->database->selectCollection($this->definitionhash);
$this->collection->ensureIndex(array('key' => 1), array('safe' => $this->usesafe, 'name' => 'idx_key'));
}
示例15: loadOrder
public function loadOrder($id, $field)
{
$this->setData(array());
$this->_tblSales->ensureIndex("order.{$field}");
$result = $this->_tblSales->findOne(array("order.{$field}" => $id));
if ($result['_id']) {
$this->setData($result);
}
return $this;
}