diff --git a/src/Mongodb/Mongodb.php b/src/Mongodb/Mongodb.php index 1d69b3f..db9ff56 100644 --- a/src/Mongodb/Mongodb.php +++ b/src/Mongodb/Mongodb.php @@ -11,6 +11,8 @@ class Mongo_db private $username; private $password; private $debug = false; + private $authSource; + private $replicaSet=null; private $collection = ''; private $selects; @@ -40,15 +42,21 @@ private function prepareConfig() if (isset($this->config['port'])) { $this->port = trim($this->config['port']); } - if (isset($this->config['username'])) { + if (!empty($this->config['username'])) { $this->username = trim($this->config['username']); } - if (isset($this->config['password'])) { + if (!empty($this->config['password'])) { $this->password = trim($this->config['password']); } if (isset($this->config['database'])) { $this->database = trim($this->config['database']); } + if (!empty($this->config['authSource'])) { + $this->authSource = trim($this->config['authSource']); + } + if (!empty($this->config['replicaSet'])) { + $this->replicaSet = trim($this->config['replicaSet']); + } if (isset($this->config['db_debug'])) { $this->debug = $this->config['db_debug']; } @@ -61,10 +69,18 @@ private function connect() { $this->prepareConfig(); try { - $dsn = "mongodb://{$this->hostname}:{$this->port}/{$this->database}"; + if (strstr($this->hostname, ',')) { + $dsn = "mongodb://{$this->hostname}/{$this->database}"; + + }else { + $dsn = "mongodb://{$this->hostname}:{$this->port}/{$this->database}"; + + } $options = array( 'username' => $this->username, - 'password' => $this->password + 'password' => $this->password, + 'authSource'=>$this->authSource, + 'replicaSet'=>$this->replicaSet ); $this->manager = new \MongoDB\Driver\Manager($dsn, $options); } catch (\Exception $e) { @@ -394,10 +410,10 @@ public function find($id = null) } $filter = $this->wheres; $options = [ - 'projection' => $this->selects, - "sort" => $this->sorts, - "skip" => 0, - "limit" => 1, + 'projection' => $this->selects, + "sort" => $this->sorts, + "skip" => 0, + "limit" => 1, ]; $query = new \MongoDB\Driver\Query($filter, $options); $dbc = $this->database . '.' . $this->collection; @@ -505,8 +521,8 @@ public function aggregate($commands) $db = $this->database; $commands = new \MongoDB\Driver\Command( [ - 'aggregate' => $this->collection, - 'pipeline' => [$commands] + 'aggregate' => $this->collection, + 'pipeline' => [$commands] ] ); $cursor = $this->command($db, $commands); @@ -524,9 +540,9 @@ public function distinct($key) $db = $this->database; $commands = new \MongoDB\Driver\Command( [ - 'distinct' => $this->collection, - 'key' => $key, - 'query' => $this->wheres + 'distinct' => $this->collection, + 'key' => $key, + 'query' => $this->wheres ] ); $cursor = $this->command($db, $commands); @@ -544,8 +560,8 @@ public function count() $db = $this->database; $commands = new \MongoDB\Driver\Command( [ - "count" => $this->collection, - "query" => $this->wheres + "count" => $this->collection, + "query" => $this->wheres ] ); $cursor = $this->command($db, $commands); @@ -563,10 +579,10 @@ public function get() try { $filter = (array)$this->wheres; $options = [ - 'projection' => (array)$this->selects, - "sort" => (array)$this->sorts, - "skip" => (int)$this->offset, - "limit" => (int)$this->limit, + 'projection' => (array)$this->selects, + "sort" => (array)$this->sorts, + "skip" => (int)$this->offset, + "limit" => (int)$this->limit, ]; $query = new \MongoDB\Driver\Query($filter, $options); $dbc = $this->database . '.' . $this->collection; @@ -894,6 +910,19 @@ public function timestamp($stamp = false) } } + + + /** + * 生成mongo uuid + * @param bool $stamp + * @return + */ + public function uuid($uuid) + { + return new \MongoDB\BSON\Binary($uuid,\MongoDB\BSON\Binary::TYPE_UUID); + + } + /** * 抛出异常 * @param $e @@ -902,4 +931,4 @@ public function showError($e) { exit($e->getMessage()); } -} \ No newline at end of file +}