Пример #1
0
 public function __construct($database, $connectionString = '', $server = 'localhost', $port = '27017')
 {
     if (empty($connectionString)) {
         $connectionString = 'mongodb://' . $server . ':' . $port . '/' . $database;
     }
     $this->mongo = Monga::connection($connectionString);
     $this->mongo = $this->mongo->database($database);
 }
Пример #2
0
 /**
  * [getInstance 获取实例]
  * @param  [type] $dbName           [description]
  * @param  array  $connectionOption [description]
  * @return [type]                   [description]
  */
 public static function getInstance($dbName, $connectionOption = array())
 {
     if (isset(self::$databases[$dbName])) {
         return self::$databases[$dbName];
     }
     $config = self::getConfig($dbName);
     $dns = str_replace(array('{host}', '{port}'), array($config['host'], $config['port']), 'mongodb://{host}:{port}');
     $connectionOption['connect'] = true;
     $connection = Monga::connection($dns, $connectionOption);
     $database = $connection->database($dbName);
     return $database;
 }
Пример #3
0
 /**
  *
  */
 public function index()
 {
     $connection = Monga::connection();
     $database = $connection->database('sky_1');
     // Get a collection
     $collection = $database->collection('collection_name');
     // Drop the collection
     // $collection->drop();
     // Truncate the collection
     // $collection->truncate();
     // Insert some values into the collection
     $insertIds = $collection->insert([['name' => 'John', 'surname' => 'Doe', 'nick' => 'The Unknown Man', 'age' => 20], ['name' => 'Frank', 'surname' => 'de Jonge', 'nick' => 'Unknown', 'nik' => 'No Man', 'age' => 23]]);
     echo "<pre>";
     print_r($insertIds);
     exit(__FILE__ . __LINE__);
     // Update a collection
     $collection->update(function ($query) {
         $query->increment('age')->remove('nik')->set('nick', 'FrenkyNet');
     });
     // Find Frank
     $frank = $collection->findOne(function ($query) {
         $query->where('name', 'Frank')->whereLike('surname', '%e Jo%');
     });
     // Or find him using normal array syntax
     $frank = $collection->find(['name' => 'Frank', 'surname' => new MongoRegex('/e Jo/imxsu')]);
     $frank['age']++;
     $collection->save($frank);
     // Also supports nested queries
     $users = $collection->find(function ($query) {
         $query->where(function ($query) {
             $query->where('name', 'Josh')->orWhere('surname', 'Doe');
         })->orWhere(function () {
             $query->where('name', 'Frank')->where('surname', 'de Jonge');
         });
     });
     // get the users as an array
     $arr = $users->toArray();
     var_dump($connection);
 }
Пример #4
0
 /**
  * Goes and try's to create a connection to the MongoClient through the Monga Connection class. If a connection
  * cannot be made it tries to attempt a new connection until it has reached its maximum retry's. If this is the
  * case the function throws an exception stating that it cannot currently connect to the MongoDB Client. This code
  * is based off of an issue in the PHP Legacy driver, issue PHP-854. The URL to that issue is:
  *
  * 		https://jira.mongodb.org/browse/PHP-854
  *
  * The workaround to the issue was posted on Nov 14 2013 07:21:18 PM GMT+0000 by Hannes Magnusson.
  *
  * @param 	string 			$dsn		The Information for what mongo client(s) to connect to.
  * @param   array 			$options    All of the options for creating the MongoClient
  * @param   int				$retry		How many times we should try and retry creating a connection
  * @return  \League\Monga\Connection			The connection for the Monga object.
  * @throws \Exception
  */
 protected static function get_connection($dsn, $options, $retry = self::MAX_CONNECTION_RETRY_COUNT)
 {
     try {
         $instance = Monga::connection($dsn, $options);
         return $instance;
     } catch (\Exception $e) {
         Log::error('Exception thrown: (' . __FILE__ . '#' . __LINE__ . '): ' . $e->getMessage());
     }
     if ($retry > 0) {
         $retry--;
         return static::get_connection($dsn, $options, $retry);
     } else {
         throw new \Exception(self::CANNOT_CONNECT_EXCEPTION);
     }
 }
Пример #5
0
 private function __construct()
 {
     Config::load('mongo', true);
     $connection = Monga::connection(Config::get('mongo.dsn'), Config::get('mongo.options', []));
     $this->database = $connection->database(Config::get('mongo.database'));
 }
Пример #6
0
 /**
  * Construct a new MongaRepository instance.
  *
  * @param array $configuration An array of configuration data for the instance.
  */
 public function __construct(array $configuration = array())
 {
     /* for Monga, we want the "Database" object as the "native connection" */
     $this->connection = Monga::connection($configuration['server'], $configuration['options'])->database($configuration['repository']);
 }