From f611b25da3e70bd809d75fcdfc09447d5bdffc1b Mon Sep 17 00:00:00 2001 From: Lenix Date: Mon, 7 Aug 2017 16:25:55 +0800 Subject: [PATCH 1/2] update update --- src/Mongodb/Mongodb.php | 57 +++++++++++++++++++++++++++-------------- 1 file changed, 38 insertions(+), 19 deletions(-) diff --git a/src/Mongodb/Mongodb.php b/src/Mongodb/Mongodb.php index 1d69b3f..4999a63 100644 --- a/src/Mongodb/Mongodb.php +++ b/src/Mongodb/Mongodb.php @@ -40,10 +40,10 @@ 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'])) { @@ -61,7 +61,13 @@ 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 @@ -394,10 +400,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 +511,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 +530,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 +550,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 +569,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 +900,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 +921,4 @@ public function showError($e) { exit($e->getMessage()); } -} \ No newline at end of file +} From 564aa285ea77566a2bc7c85f30fc5d84c7a39c5a Mon Sep 17 00:00:00 2001 From: Lenix Date: Fri, 1 Sep 2017 16:29:40 +0800 Subject: [PATCH 2/2] update update --- src/Mongodb/Mongodb.php | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/Mongodb/Mongodb.php b/src/Mongodb/Mongodb.php index 4999a63..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; @@ -49,6 +51,12 @@ private function prepareConfig() 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']; } @@ -70,7 +78,9 @@ private function connect() } $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) {