123
This commit is contained in:
@@ -19,7 +19,7 @@ class MongoBase
|
||||
|
||||
private static $instance;
|
||||
|
||||
public static function getInstance(string $uri, string $dbName): self
|
||||
public static function getInstance(): self
|
||||
{
|
||||
|
||||
// return [
|
||||
@@ -28,7 +28,7 @@ class MongoBase
|
||||
// ];
|
||||
|
||||
if (!self::$instance) {
|
||||
self::$instance = new self($uri, $dbName);
|
||||
self::$instance = new self();
|
||||
}
|
||||
return self::$instance;
|
||||
}
|
||||
@@ -38,11 +38,14 @@ class MongoBase
|
||||
* @param string $uri MongoDB 连接字符串
|
||||
* @param string $dbName 数据库名称
|
||||
*/
|
||||
public function __construct(string $uri, string $dbName)
|
||||
public function __construct()
|
||||
{
|
||||
try {
|
||||
$this->client = new Client($uri);
|
||||
$this->db = $this->client->selectDatabase($dbName);
|
||||
$arrConfig = config('mongodb');
|
||||
$strUri = sprintf('mongodb://%s:%s@%s:%s', $arrConfig['username'], $arrConfig['password'], $arrConfig['hostname'], $arrConfig['hostport']);
|
||||
|
||||
$this->client = new Client($strUri);
|
||||
$this->db = $this->client->selectDatabase($arrConfig['database']);
|
||||
} catch (MongoDBException $e) {
|
||||
throw new \Exception("MongoDB connection failed: " . $e->getMessage());
|
||||
}
|
||||
@@ -89,6 +92,11 @@ class MongoBase
|
||||
}
|
||||
}
|
||||
|
||||
public function getDb(): \MongoDB\Database
|
||||
{
|
||||
return $this->db;
|
||||
}
|
||||
|
||||
/**
|
||||
* 插入多条数据
|
||||
* @param string $collection 集合名称
|
||||
|
||||
Reference in New Issue
Block a user