public static function getConnection($db) { if (!isset(self::$instances[$db])) { $config = Config::loadConfig('database')[$db]; $uri = $config['server'] ? 'mongodb://' . $config['server'] : ''; $options = $config['options'] ?: []; $client = new \MongoDB\Client($uri, $options); self::$instances[$db] = $client->selectDatabase($config['database']); } return self::$instances[$db]; }
/** * Construct * @param string $server Server hostname * @param string $port Port * @throws Exceptions\ConnectionFailException */ function __construct($server = 'localhost', $port = self::PORT) { try { /* @var $MongoDBClient \MongoDB\Client */ $MongoDBClient = new \MongoDB\Client(self::PROTOCOL . $server . ":" . $port, [], ['typeMap' => ['root' => 'array', 'document' => 'array', 'array' => 'array']]); $MongoDBClient->listDatabases(); //info $this->MongoDBClient = $MongoDBClient; } catch (\MongoDB\Driver\Exception\ConnectionTimeoutException $e) { $this->isConnect = false; throw new Exceptions\ConnectionFailException('Error no connect to data base: ' . $e->getMessage()); } catch (\Exception $ex) { $this->isConnect = false; throw new Exceptions\ConnectionFailException('Error no connect to data base: ' . $ex->getMessage()); } }
protected function setUp() { if (!class_exists('\\MongoDB\\Client')) { $this->markTestSkipped('MongoDB is not installed'); } $mongo = new \MongoDB\Client(); $this->module = new MongoDb(make_container()); $this->module->_setConfig($this->mongoConfig); try { $this->module->_initialize(); } catch (ModuleException $e) { $this->markTestSkipped($e->getMessage()); } $this->db = $mongo->selectDatabase('test'); $this->userCollection = $this->db->users; $this->userCollection->insertOne(array('id' => 1, 'email' => '*****@*****.**')); }
public static function connect() { $mongoDB = Registry::get("MongoDB"); if (!$mongoDB) { require_once APP_PATH . '/application/libraries/vendor/autoload.php'; $configuration = Registry::get("configuration"); try { $dbconf = $configuration->parse("configuration/database")->database->mongodb; $mongo = new \MongoDB\Client("mongodb://" . $dbconf->dbuser . ":" . $dbconf->password . "@" . $dbconf->url . "/" . $dbconf->dbname . "?replicaSet=" . $dbconf->replica . "&ssl=true"); $mongoDB = $mongo->selectDatabase($dbconf->dbname); } catch (\Exception $e) { throw new \Framework\Database\Exception("DB Error"); } Registry::set("MongoDB", $mongoDB); } return $mongoDB; }
/** * Construct queue. * * @param \MongoDB\Collection|string $collectionOrUrl A MongoCollection instance or the mongo connection url. * @param string $db the mongo db name * @param string $collection the collection name to use for the queue * * @throws \InvalidArgumentException $collectionOrUrl, $db or $collection was not a string */ public function __construct($collectionOrUrl, $db = null, $collection = null) { if ($collectionOrUrl instanceof \MongoDB\Collection) { $this->collection = $collectionOrUrl; return; } if (!is_string($collectionOrUrl)) { throw new \InvalidArgumentException('$collectionOrUrl was not a string'); } if (!is_string($db)) { throw new \InvalidArgumentException('$db was not a string'); } if (!is_string($collection)) { throw new \InvalidArgumentException('$collection was not a string'); } $mongo = new \MongoDB\Client($collectionOrUrl, [], ['typeMap' => ['root' => 'array', 'document' => 'array', 'array' => 'array']]); $mongoDb = $mongo->selectDatabase($db); $this->collection = $mongoDb->selectCollection($collection); }
<?php require_once dirname(__FILE__) . '/common.inc.php'; /** * Detects un-namespaced subjects or object uris in CBD collections of the target database. Optionally supply a base uri to match against that rather than all uris */ if ($argc != 4 && $argc != 3) { echo "usage: php discoverUnnamespacedUris.php connStr database [baseUri]"; die; } array_shift($argv); /** @var \MongoDB\Client $client */ $client = new \MongoDB\Client($argv[0], [], ['typeMap' => ['root' => 'array', 'document' => 'array', 'array' => 'array']]); /** @var \MongoDB\Database $db */ $db = $client->selectDatabase($argv[1]); /** * @param string $uri * @param string|null $baseUri * @return bool */ function isUnNamespaced($uri, $baseUri = null) { if ($baseUri == null) { return strpos($uri, 'http://') === 0 || strpos($uri, 'https://') === 0; } else { return strpos($uri, $baseUri) === 0; } } $results = array(); foreach ($db->listCollections() as $collectionInfo) { /** @var \MongoDB\Collection $collection*/
/** * Verify Queue can be constructed with \MongoDB\Collection * * @test * @covers ::__construct * * @return void */ public function constructWithCollection() { $mongo = new \MongoDB\Client($this->mongoUrl, [], ['typeMap' => ['root' => 'array', 'document' => 'array', 'array' => 'array']]); $collection = $mongo->selectDatabase('testing')->selectCollection('custom_collection'); $collection->drop(); $queue = new Queue($collection); $payload = ['key1' => 0, 'key2' => true]; $queue->send($payload, 34, 0.8); $expected = ['payload' => $payload, 'running' => false, 'resetTimestamp' => (new UTCDateTime(Queue::MONGO_INT32_MAX))->toDateTime()->getTimestamp(), 'earliestGet' => 34, 'priority' => 0.8]; $this->assertSame(1, $collection->count()); $message = $collection->findOne(); $this->assertLessThanOrEqual(time(), $message['created']->toDateTime()->getTimestamp()); $this->assertGreaterThan(time() - 10, $message['created']->toDateTime()->getTimestamp()); unset($message['_id'], $message['created']); $message['resetTimestamp'] = $message['resetTimestamp']->toDateTime()->getTimestamp(); $message['earliestGet'] = $message['earliestGet']->toDateTime()->getTimestamp(); $this->assertSame($expected, $message); }
public function setUp() { $mongo = new \MongoDB\Client('mongodb://localhost:27017', [], ['typeMap' => ['root' => 'array', 'document' => 'array', 'array' => 'array']]); $this->_collection = $mongo->selectDatabase('testing')->selectCollection('processes'); $this->_collection->drop(); }
public function setUp() { $client = new \MongoDB\Client(); $this->collection = $client->selectCollection('sessionhandlertest', 'sessions'); }