Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 49 additions & 20 deletions src/Mongodb/Mongodb.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ class Mongo_db
private $username;
private $password;
private $debug = false;
private $authSource;
private $replicaSet=null;

private $collection = '';
private $selects;
Expand Down Expand Up @@ -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'];
}
Expand All @@ -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) {
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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;
Expand Down Expand Up @@ -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
Expand All @@ -902,4 +931,4 @@ public function showError($e)
{
exit($e->getMessage());
}
}
}