Creates an index on the given field(s), or does nothing if the index already exists
/** * Note that we use TTL Collections to have the Mongo deamon automatically clean * expired entries. * * @link http://docs.mongodb.org/manual/tutorial/expire-data/ * @param array $options */ public function __construct(array $options = array()) { 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; overriding any default options $this->_options = array_merge($this->_options, $options); $conn = new \MongoClient($this->_getServerConnectionUrl()); $this->_database = $conn->{$this->_options['dbname']}; $this->_collection = $this->_database->selectCollection($this->_options['collection']); $this->_collection->createIndex(array('tags' => 1), array('background' => true)); $this->_collection->createIndex(array('expires_at' => 1), array('background' => true, 'expireAfterSeconds' => 0)); }
/** * 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->createIndex($columns, $options); return $this; }
public function postUp(Schema $schema) { $upgradeHelper = new UpgradeHelper($this->container); if ($upgradeHelper->areProductsStoredInMongo()) { $database = $upgradeHelper->getMongoInstance(); $tableHelper = new SchemaHelper($this->container); echo "Add index to Version document on column loggetAt...\n"; $versionCollection = new \MongoCollection($database, $tableHelper->getTableOrCollection('version')); $versionCollection->createIndex(['loggedAt' => -1], ['background' => true]); echo "Done."; } }
/** * Run the migrations. * * @return void */ public function up() { set_time_limit(0); $db = \DB::getMongoDB(); $statementsCollection = new MongoCollection($db, 'statements'); $statementsCollection->createIndex(['stored' => 1], ['background' => 1, 'socketTimeoutMS' => -1]); $statementsCollection->createIndex(['stored' => -1], ['background' => 1, 'socketTimeoutMS' => -1]); $statementsCollection->createIndex(['lrs_id' => 1, 'stored' => 1], ['background' => 1, 'socketTimeoutMS' => -1]); $statementsCollection->createIndex(['lrs_id' => 1, 'stored' => -1], ['background' => 1, 'socketTimeoutMS' => -1]); $statementsCursor = $statementsCollection->find(); $remaining = $statementsCursor->count(); print $remaining . ' statements total' . PHP_EOL; $maxBatchSize = 10000; while ($statementsCursor->hasNext()) { $batch = new MongoUpdateBatch($statementsCollection); $batchSize = 0; while ($batchSize < $maxBatchSize && $statementsCursor->hasNext()) { $batchSize++; $statement = $statementsCursor->next(); $statementStored = new Carbon\Carbon($statement['statement']['stored']); $query = ['q' => ['_id' => $statement['_id']], 'u' => ['$set' => ["stored" => new \MongoDate($statementStored->timestamp, $statementStored->micro)]], 'multi' => false, 'upsert' => false]; if (isset($statement['refs'])) { foreach ($statement['refs'] as $key => $refStatement) { if (isset($refStatement['timestamp']) && !$refStatement['timestamp'] instanceof MongoDate) { $timestamp = new Carbon\Carbon($refStatement['timestamp']); $query['u']['$set']['refs.' . $key . '.timestamp'] = new \MongoDate($timestamp->timestamp, $timestamp->micro); } if (isset($refStatement['stored']) && !$refStatement['stored'] instanceof MongoDate) { $stored = new Carbon\Carbon($refStatement['stored']); $query['u']['$set']['refs.' . $key . '.stored'] = new \MongoDate($stored->timestamp, $stored->micro); } } } $batch->add((object) $query); } $batch->execute(); $remaining -= $batchSize; print $remaining . ' remaining' . PHP_EOL; } }
$keys['flatsale'] = ['', 'state', 'city', 'county', 'region', 'aptName', 'area']; $keys['houserent'] = ['', 'state', 'city', 'county', 'region', 'monthlyType']; $keys['aptrent'] = ['', 'state', 'city', 'county', 'region', 'aptName', 'area', 'monthlyType']; $keys['flatrent'] = ['', 'state', 'city', 'county', 'region', 'aptName', 'area', 'monthlyType']; foreach ($keys as $key => $val) { echo 'Adding index $key...\\n'; $col = new MongoCollection($db, $key); $col_agg = new MongoCollection($db, $key . '_agg'); $idx_key = []; foreach ($val as $id) { if ($id) { $idx_key[$id] = 1; } $added_idx_key = array_merge($idx_key, ['year' => 1]); print_r($added_idx_key); $r = $col->createIndex($added_idx_key); print_r($r); $added_idx_key = array_merge($idx_key, ['year' => 1, 'month' => 1]); $r = $col->createIndex($added_idx_key); print_r($r); $added_idx_key = array_merge($idx_key, ['year' => -1, 'month' => -1]); $r = $col->createIndex($added_idx_key); print_r($r); $col->createIndex(['year' => 1, 'month' => 1]); $col->createIndex(['year' => -1, 'month' => -1]); $r = $col_agg->createIndex($added_idx_key); print_r($r); $col_agg->createIndex(['year' => 1, 'month' => 1]); $col_agg->createIndex(['year' => -1, 'month' => -1]); } }
/** * Run the migrations. * * @return void */ public function up() { $db = \DB::getMongoDB(); $indexOptions = ['background' => 1, 'socketTimeoutMS' => -1]; $statements = new MongoCollection($db, 'statements'); $statements->createIndex(['statement.id' => 1, 'lrs_id' => 1], $indexOptions); $statements->createIndex(['statement.actor.mbox' => 1], $indexOptions); $statements->createIndex(['stored' => 1], $indexOptions); $statements->createIndex(['timestamp' => 1], $indexOptions); $statements->createIndex(['active' => 1], $indexOptions); $statements->createIndex(['voided' => 1], $indexOptions); $statements->createIndex(['lrs_id' => 1], $indexOptions); $statements->createIndex(['lrs_id' => 1, 'active' => -1, 'voided' => 1], $indexOptions); $statements->createIndex(['lrs_id' => 1, 'statement.actor.account.name' => 1, 'statement.actor.account.homePage' => 1], $indexOptions); $statements->createIndex(['lrs_id' => 1, 'statement.actor.mbox' => 1], $indexOptions); $statements->createIndex(['lrs_id' => 1, 'statement.verb.id' => 1], $indexOptions); $statements->createIndex(['lrs_id' => 1, 'statement.object.id' => 1], $indexOptions); $statements->createIndex(['lrs_id' => 1, 'stored' => -1], $indexOptions); $statements->createIndex(['lrs_id' => 1, 'timestamp' => -1], $indexOptions); }
function __construct($host = 'localhost', $port = 27017) { $this->connection = new \MongoClient('mongodb://' . $host . ':' . $port); $this->index = $this->connection->search->index; $this->index->createIndex(array('token' => 1), array('unique' => true)); }
public function createIndex() { $this->collection->createIndex(["packageName" => 1, "packageVersion" => 1]); }
public function ensureIndex($fieldName) { $this->collection->createIndex([$fieldName => 1]); }