Creates a collection
public createCollection ( string $name, array $options ) : MongoCollection | ||
$name | string | The name of the collection. |
$options | array | |
return | MongoCollection | Returns a collection object representing the new collection. |
public function setUp() { if (is_null($this->database)) { $client = new \MongoClient(); $this->database = $client->selectDB('DbMockLibraryTest'); } $this->database->dropCollection('testCollection'); $this->database->createCollection('testCollection'); Mongo::initMongo(['testCollection' => [1 => ['foo' => 0, '_id' => 0]]], 'DbMockLibraryTest', []); }
/** * 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']); }
/** * createCollection. */ public function createCollection($name, $capped = null, $capped_size = null, $max_elements = null) { $this->time->start(); $return = parent::createCollection($name, $capped, $size, $max); $time = $this->time->stop(); $this->log(array('type' => 'createCollection', 'name' => $name, 'options' => $options)); return $return; }
protected function execute(InputInterface $input, OutputInterface $output) { $categoryCol = $this->mongo->createCollection(CategoryMeta::SHORT_NAME); var_dump($categoryCol->ensureIndex([CategoryMeta::ESHOP_ID => 1, CategoryMeta::HASH => 1], ["unique" => true])); $productCol = $this->mongo->createCollection(ProductMeta::SHORT_NAME); var_dump($productCol->ensureIndex([ProductMeta::ESHOP_ID => 1, ProductMeta::ITEM_GROUP_ID => 1, ProductMeta::ITEM_ID => 1], ["unique" => true])); var_dump($productCol->ensureIndex(["_id" => 1, ProductMeta::V => 1], ["unique" => true])); $eshopCol = $this->mongo->createCollection(EshopMeta::SHORT_NAME); var_dump($eshopCol->ensureIndex([EshopMeta::SLUG => 1]), ["unique" => true]); $f = fopen($input->getArgument("csv"), "r"); while (list($slug, $url, $feedUrl) = fgetcsv($f)) { $eshop = new Eshop(); $eshop->setName($slug)->setSlug($slug)->setUrl($url)->setFeeds([(new Feed())->setId(new \MongoId())->setUrl($feedUrl)]); $eshopDoc = $this->eshopRepository->findAndModify([EshopMeta::SLUG => $slug], EshopMeta::toArray($eshop), ["_id"], ["upsert" => true, "new" => true]); var_dump($eshopDoc); } }
/** * createCollection. */ public function createCollection($name, $capped = false, $size = 0, $max = 0) { $this->time->start(); $return = parent::createCollection($name, $capped, $size, $max); $time = $this->time->stop(); $this->log(array('type' => 'createCollection', 'name' => $name, 'capped' => $capped, 'size' => $size, 'max' => $max)); return $return; }
/** * Creates a collection. * * @see Database::createCollection() * @param string $name * @param array $options * @return Collection */ protected function doCreateCollection($name, array $options) { if (version_compare(phpversion('mongo'), '1.4.0', '>=')) { $this->mongoDB->createCollection($name, $options); } else { $this->mongoDB->createCollection($name, $options['capped'], $options['size'], $options['max']); } return $this->doSelectCollection($name); }
/** * @param string $collectionname * @return boolean Whether the operation was successful */ protected function _createCollection($collectionname) { try { $this->dbcon->createCollection($collectionname); $this->numQueries++; return true; } catch (Exception $e) { $this->errors[] = $e->__toString() . ', caught in ' . __CLASS__ . '::' . __METHOD__ . '()'; } return false; }
public function testDBCommand() { $x = $this->object->command(array()); $this->assertEquals(0, strpos($x['errmsg'], "no such cmd"), json_encode($x)); $this->assertEquals((bool) $x['ok'], false); $created = $this->object->createCollection("system.profile", true, 5000); $this->object->command(array('profile' => 0)); $x = $this->object->command(array('profile' => 1)); $this->assertEquals($x['was'], 0, json_encode($x)); $this->assertEquals((bool) $x['ok'], true, json_encode($x)); }
/** * Creates a collection * @param string $collection */ public function createCollection($collection) { if ($collection) { $this->mongo->createCollection($collection); } }
/** * Creates a collection. * * @see Database::createCollection() * @param string $name * @param array $options * @return Collection */ protected function doCreateCollection($name, array $options) { $this->mongoDB->createCollection($name, $options); return $this->doSelectCollection($name); }
public function createCollection($name, $options = array()) { parent::createCollection($name, $options); return $this->selectCollection($name); }
/** * Create collection * * @param MongoDB $db MongoDB * @param string $name Collection name * @param array $options Options, capped, size, max */ public static function createCollection(MongoDB $db, $name, array $options) { if (RMongo::compareVersion("1.4.0") >= 0) { $db->createCollection($name, $options); } else { $db->createCollection($name, isset($options["capped"]) ? $options["capped"] : false, isset($options["size"]) ? $options["size"] : 0, isset($options["max"]) ? $options["max"] : 0); } }
function forOneStringCollect(MongoDB $db) { $streets = $db->streets; $cities = $db->cities; $districts = $db->district; $regions = $db->regions; $buildings = $db->buildings; /* * Массив всех элементов будущей таблицы */ //удаляем-создаём таблицу $db->complex->drop(); $db->createCollection('complex'); // Здания $allBuildings = $buildings->find(array(), array('NormalizedName' => 1, 'Name' => 1, 'Id' => 1, 'ZipCode' => 1, 'TypeShort' => 1, 'Type' => 1, 'Okato' => 1, 'CodeRegion' => 1, 'CodeDistrict' => 1, 'CodeCity' => 1, 'CodeStreet' => 1, 'CodeBuilding' => 1)); echo 'buildings'; $i = 0; foreach ($allBuildings as $arBuilding) { if ($i++ % 10000 == 0) { echo $i . '; '; } //здания с одним id $building = $arBuilding; $building['NormalizedName'] = $arBuilding['NormalizedName']; $building['Sort'] = 50; $building['BuildingId'] = $arBuilding['Id']; // $building['Address'] = array(); // $building['Address'] = array_merge($building['Address'], $arBuilding['NormalizedName']); //$building['FullName'] .= $arBuilding['NormalizedName']; //без имени дома $building['FullName'] = null; $building['StreetId'] = null; $building['CityId'] = null; $building['DistrictId'] = null; $building['RegionId'] = null; $building['NormalizedBuildingName'] = $arBuilding['NormalizedName']; $building['NormalizedStreetName'] = null; $building['NormalizedCityName'] = null; $building['NormalizedDistrictName'] = null; $building['NormalizedRegionName'] = null; $building['ContentType'] = 'building'; //ищем айдишники её городов и заполняем поле address т.п. $street = $streets->findOne(array('CodeStreet' => $building['CodeStreet'], 'CodeCity' => $building['CodeCity'], 'CodeDistrict' => $building['CodeDistrict'], 'CodeRegion' => $building['CodeRegion'], 'Bad' => false), array('Name' => 1, 'NormalizedName' => 1, 'Id' => 1, 'TypeShort' => 1, 'Type' => 1)); if ($street) { $building['StreetId'] = $street['Id']; // $building['Address'] = array_merge($building['Address'], $street['NormalizedName']); $building['NormalizedStreetName'] = $street['NormalizedName']; } $city = $cities->findOne(array('CodeCity' => $building['CodeCity'], 'CodeDistrict' => $building['CodeDistrict'], 'CodeRegion' => $building['CodeRegion'], 'Bad' => false), array('Name' => 1, 'NormalizedName' => 1, 'Id' => 1, 'TypeShort' => 1, 'Type' => 1)); if ($city) { $building['CityId'] = $city['Id']; $building['NormalizedCityName'] = $city['NormalizedName']; } $cityOwner = $cities->findOne(array('Id' => getCityOwnerId($building['BuildingId']), 'Bad' => false), array('Name' => 1, 'NormalizedName' => 1, 'Id' => 1, 'TypeShort' => 1, 'Type' => 1)); if ($cityOwner) { $building['CityOwnerId'] = $cityOwner['Id']; $building['NormalizedCityOwnerName'] = $cityOwner['NormalizedName']; } $district = $districts->findOne(array('CodeDistrict' => $building['CodeDistrict'], 'CodeRegion' => $building['CodeRegion'], 'Bad' => false), array('Name' => 1, 'NormalizedName' => 1, 'Id' => 1, 'TypeShort' => 1, 'Type' => 1)); if ($district) { $building['DistrictId'] = $district['Id']; // $building['Address'] = array_merge($building['Address'], $district['NormalizedName']); $building['NormalizedDistrictName'] = $district['NormalizedName']; } $region = $regions->findOne(array('CodeRegion' => $building['CodeRegion'], 'Bad' => false), array('Name' => 1, 'NormalizedName' => 1, 'Id' => 1, 'TypeShort' => 1, 'Type' => 1)); if ($region) { $building['RegionId'] = $region['Id']; // $building['Address'] = array_merge($building['Address'], $region['NormalizedName']); $building['NormalizedRegionName'] = $region['NormalizedName']; } //собираем все тайпы //typesCollect($building, $region, $district, $city, $street, $building); //и имя constructFullName($building, $region, $district, $city, $street, $cityOwner); unset($building['_id']); //избегаем ненужных конфликтов $db->complex->insert($building); } // Улицы $allStreets = $streets->find(array('Bad' => false), array('NormalizedName' => 1, 'Name' => 1, 'Id' => 1, 'ZipCode' => 1, 'TypeShort' => 1, 'Type' => 1, 'Okato' => 1, 'CodeRegion' => 1, 'CodeDistrict' => 1, 'CodeCity' => 1, 'CodeStreet' => 1)); echo 'streets'; $i = 0; foreach ($allStreets as $arStreet) { if ($i++ % 10000 == 0) { echo $i . '; '; } //сама улица $street = $arStreet; $street['Sort'] = 40; $street['StreetId'] = $arStreet['Id']; // $street['Address'] = array(); // $street['Address'] = array_merge($street['Address'], $arStreet['NormalizedName']); $street['FullName'] = null; $street['CityId'] = null; $street['DistrictId'] = null; $street['RegionId'] = null; $street['NormalizedStreetName'] = $arStreet['NormalizedName']; $street['NormalizedCityName'] = null; $street['NormalizedDistrictName'] = null; $street['NormalizedRegionName'] = null; $street['ContentType'] = 'street'; //ищем айдишники, заполняем адрес $city = $cities->findOne(array('CodeCity' => $street['CodeCity'], 'CodeDistrict' => $street['CodeDistrict'], 'CodeRegion' => $street['CodeRegion'], 'Bad' => false), array('Name' => 1, 'NormalizedName' => 1, 'Id' => 1, 'TypeShort' => 1, 'Type' => 1)); if ($city) { $street['CityId'] = $city['Id']; // $street['Address'] = array_merge($street['Address'], $city['NormalizedName']); $street['NormalizedCityName'] = $city['NormalizedName']; } $cityOwner = $cities->findOne(array('Id' => getCityOwnerId($street['StreetId']), 'Bad' => false), array('Name' => 1, 'NormalizedName' => 1, 'Id' => 1, 'TypeShort' => 1, 'Type' => 1)); if ($cityOwner) { $street['CityOwnerId'] = $cityOwner['Id']; $street['NormalizedCityOwnerName'] = $cityOwner['NormalizedName']; } $district = $districts->findOne(array('CodeDistrict' => $street['CodeDistrict'], 'CodeRegion' => $street['CodeRegion'], 'Bad' => false), array('Name' => 1, 'NormalizedName' => 1, 'Id' => 1, 'TypeShort' => 1, 'Type' => 1)); if ($district) { $street['DistrictId'] = $district['Id']; // $street['Address'] = array_merge($street['Address'], $district['NormalizedName']); $street['NormalizedDistrictName'] = $district['NormalizedName']; } $region = $regions->findOne(array('CodeRegion' => $street['CodeRegion'], 'Bad' => false), array('Name' => 1, 'NormalizedName' => 1, 'Id' => 1, 'TypeShort' => 1, 'Type' => 1)); if ($region) { $street['RegionId'] = $region['Id']; // $street['Address'] = array_merge($street['Address'], $region['NormalizedName']); $street['NormalizedRegionName'] = $region['NormalizedName']; } //typesCollect($street, $region, $district, $city, $street); constructFullName($street, $region, $district, $city, $street, $cityOwner); unset($street['_id']); $db->complex->insert($street); } // Города $allCities = $cities->find(array('Bad' => false), array('NormalizedName' => 1, 'Name' => 1, 'Id' => 1, 'ZipCode' => 1, 'TypeShort' => 1, 'Type' => 1, 'Okato' => 1, 'CodeRegion' => 1, 'CodeDistrict' => 1, 'CodeCity' => 1, 'Sort' => 1)); echo 'cities'; $i = 0; foreach ($allCities as $arCity) { if ($i++ % 10000 == 0) { echo $i . '; '; } //город $city = $arCity; //$city['Sort'] = 30; $city['CityId'] = $arCity['Id']; // $city['Address'] = array(); // $city['Address'] = array_merge($city['Address'], $arCity['NormalizedName']); $city['FullName'] = null; $city['DistrictId'] = null; $city['RegionId'] = null; $city['NormalizedCityName'] = $arCity['NormalizedName']; $city['NormalizedDistrictName'] = null; $city['NormalizedRegionName'] = null; $city['ContentType'] = 'city'; //сортировка городов $sort = $city['Sort']; $sort /= 1000; if ($sort > 9) { $sort = 9; } $city['Sort'] = 30 + $sort; //айдишники, address $district = $districts->findOne(array('CodeDistrict' => $city['CodeDistrict'], 'CodeRegion' => $city['CodeRegion'], 'Bad' => false), array('Name' => 1, 'NormalizedName' => 1, 'Id' => 1, 'TypeShort' => 1, 'Type' => 1)); if ($district) { $city['DistrictId'] = $district['Id']; // $city['Address'] = array_merge($city['Address'], $district['NormalizedName']); $city['NormalizedDistrictName'] = $district['NormalizedName']; } $region = $regions->findOne(array('CodeRegion' => $city['CodeRegion'], 'Bad' => false), array('Name' => 1, 'NormalizedName' => 1, 'Id' => 1, 'TypeShort' => 1, 'Type' => 1)); if ($region) { $city['RegionId'] = $region['Id']; // $city['Address'] = array_merge($city['Address'], $region['NormalizedName']); $city['NormalizedRegionName'] = $region['NormalizedName']; } $cityOwner = $cities->findOne(array('Id' => getCityOwnerId($city['CityId']), 'Bad' => false), array('Name' => 1, 'NormalizedName' => 1, 'Id' => 1, 'TypeShort' => 1, 'Type' => 1)); if ($cityOwner) { $city['CityOwnerId'] = $cityOwner['Id']; $city['NormalizedCityOwnerName'] = $cityOwner['NormalizedName']; } //typesCollect($city, $region, $district, $city); constructFullName($city, $region, $district, $city, null, $cityOwner); unset($city['_id']); $db->complex->insert($city); } // Районы $allDistricts = $districts->find(array('Bad' => false), array('NormalizedName' => 1, 'Name' => 1, 'Id' => 1, 'ZipCode' => 1, 'TypeShort' => 1, 'Type' => 1, 'Okato' => 1, 'CodeRegion' => 1, 'CodeDistrict' => 1)); echo 'districts'; $i = 0; foreach ($allDistricts as $arDistrict) { if ($i++ % 10000 == 0) { echo $i . '; '; } //район $district = $arDistrict; $district['Sort'] = 20; $district['DistrictId'] = $arDistrict['Id']; // $district['Address'] = array(); // $district['Address'] = array_merge($district['Address'], $arDistrict['NormalizedName']); $district['FullName'] = null; $district['RegionId'] = null; $district['NormalizedDistrictName'] = $arDistrict['NormalizedName']; $district['NormalizedRegionName'] = null; $district['ContentType'] = 'district'; //айдишники, address $region = $regions->findOne(array('CodeRegion' => $district['CodeRegion'], 'Bad' => false), array('Name' => 1, 'NormalizedName' => 1, 'Id' => 1, 'TypeShort' => 1, 'Type' => 1)); if ($region) { $district['RegionId'] = $region['Id']; // $district['Address'] = array_merge($district['Address'], $region['NormalizedName']); $district['NormalizedRegionName'] = $region['NormalizedName']; } //typesCollect($district, $region, $district); constructFullName($district, $region, $district); unset($district['_id']); $db->complex->insert($district); } // Области $allRegions = $regions->find(array('Bad' => false), array('NormalizedName' => 1, 'Name' => 1, 'Id' => 1, 'ZipCode' => 1, 'TypeShort' => 1, 'Type' => 1, 'Okato' => 1, 'CodeRegion' => 1)); echo 'regions'; $i = 0; foreach ($allRegions as $arRegion) { if ($i++ % 10000 == 0) { echo $i . '; '; } //область $region = $arRegion; $region['Sort'] = 10; $region['RegionId'] = $arRegion['Id']; // $region['Address'] = array(); // $region['Address'] = array_merge($region['Address'], $arRegion['NormalizedName']); $region['NormalizedRegionName'] = $arRegion['NormalizedName']; $region['FullName'] = null; $region['ContentType'] = 'region'; //typesCollect($region, $region); constructFullName($region, $region); unset($region['_id']); $db->complex->insert($region); } echo 'table has done. Indexes...'; $complex = $db->complex; // echo 'address;'; // $complex->ensureIndex( // array('Address' => 1), // array('background' => true) // ); // echo 'rn;'; // $complex->ensureIndex( // array('NormalizedRegionName' => 1), // array('background' => true) // ); // echo 'dn;'; // $complex->ensureIndex( // array('NormalizedDistrictName' => 1), // array('background' => true) // ); // // echo 'cn;'; // $complex->ensureIndex( // array('NormalizedCityName' => 1), // array('background' => true) // ); // echo 'sn;'; // $complex->ensureIndex( // array('NormalizedStreetName' => 1), // array('background' => true) // ); echo 'contentType;'; $complex->ensureIndex(array('ContentType' => 1), array('background' => true)); echo 'bn;'; $complex->ensureIndex(array('NormalizedBuildingName' => 1), array('background' => true)); echo 'sort;'; $complex->ensureIndex(array('Sort' => 1), array('background' => true)); echo 'Id;'; $complex->ensureIndex(array('Id' => 1), array('background' => true)); echo 'StreetId;'; $complex->ensureIndex(array('StreetId' => 1), array('background' => true)); echo 'FullName'; $complex->ensureIndex(array('FullName' => 1), array('background' => true)); echo 'indexes has done'; }
/** create new collection **/ public function doNewCollection() { $this->db = xn("db"); $this->name = x("name"); $this->isCapped = xi("is_capped"); $this->size = xi("size"); $this->max = xi("max"); if ($this->isPost()) { $db = new MongoDB($this->_mongo, $this->db); $db->createCollection($this->name, $this->isCapped, $this->size, $this->max); $this->message = "New collection is created."; //add index if (!$this->isCapped) { $db->selectCollection($this->name)->ensureIndex(array("_id" => 1)); } } $this->display(); }
protected function _call($command, array $arguments = array(), array $values = NULL) { $this->_connected or $this->connect(); extract($arguments); $_bm_name = isset($collection_name) ? $collection_name . '.' . $command : $command; if (isset($collection_name)) { $c = $this->_db->selectCollection($collection_name); } switch ($command) { case 'ensure_index': $r = $c->ensureIndex($keys, $options); break; case 'create_collection': $r = $this->_db->createCollection($name, $capped, $size, $max); break; case 'drop_collection': $r = $this->_db->dropCollection($name); break; case 'command': $r = $this->_db->command($values); break; case 'execute': $r = $this->_db->execute($code, $args); break; case 'batch_insert': $r = $c->batchInsert($values); break; case 'count': $r = $c->count($query); break; case 'find_one': $r = $c->findOne($query, $fields); break; case 'find': $r = $c->find($query, $fields); break; case 'group': $r = $c->group($keys, $initial, $reduce, $condition); break; case 'update': $r = $c->update($criteria, $values, $options); break; case 'insert': $r = $c->insert($values, $options); break; case 'remove': $r = $c->remove($criteria, $options); break; case 'save': $r = $c->save($values, $options); break; case 'get_file': $r = $this->gridFS()->findOne($criteria); break; case 'get_files': $r = $this->gridFS()->find($query, $fields); break; case 'set_file_bytes': $r = $this->gridFS()->storeBytes($bytes, $extra, $options); break; case 'set_file': $r = $this->gridFS()->storeFile($filename, $extra, $options); break; case 'remove_file': $r = $this->gridFS()->remove($criteria, $options); break; } if (isset($_bm)) { Profiler::stop($_bm); } return $r; }
protected function createCollection() { $meta = $this->getMeta(); return $this->mongo->createCollection($meta::SHORT_NAME); }