MongoDB插入函数找不到
我正在尝试在我的项目中使用MongoDB,并且在遇到此问题时才开始使用它。我创建了另一个目录并毫无问题地运行文件。这怎么了?我不断遇到致命错误:未经定义的方法mongodb \ database :: insertone()
错误消息。
这是我的代码,称为数据库,该类别正在产生错误。
<?php
namespace auth;
include_once dirname(__DIR__) . "/config.php";
class Database
{
public function __construct(
private readonly string $dbname,
){
$this->run();
}
private function run(): void
{
$collection = (new \MongoDB\Client)->{$this->dbname};
$insertOneResult = $collection->insertOne([
'username' => 'admin',
'email' => '[email protected]',
'name' => 'Admin User',
]);
printf("Inserted %d document(s)\n", $insertOneResult->getInsertedCount());
var_dump($insertOneResult->getInsertedId());
}
}
我的test.php
文件刚刚包含一些代码行,
include_once "./assets/php/config.php";
$database = new \auth\Database("test->users");
我肯定加载了class mongodb,而我的config.php包括作曲家的供应商文件。
这是完整的错误。
Fatal error: Uncaught Error: Call to undefined method MongoDB\Database::insertOne() in C:\xampp\htdocs\PP\assets\php\Classes\Database.php:18
Stack trace:
#0 C:\xampp\htdocs\PP\assets\php\Classes\Database.php(11): auth\Database->run()
#1 C:\xampp\htdocs\PP\test.php(15): auth\Database->__construct('test->users')
#2 {main}
thrown in C:\xampp\htdocs\PP\assets\php\Classes\Database.php on line 18
PHP Fatal error: Uncaught Error: Call to undefined method MongoDB\Database::insertOne() in C:\xampp\htdocs\PPa\assets\php\Classes\Database.php:18
Stack trace:
#0 C:\xampp\htdocs\PP\assets\php\Classes\Database.php(11): auth\Database->run()
#1 C:\xampp\htdocs\PP\test.php(15): auth\Database->__construct('test->users')
#2 {main}
thrown in C:\xampp\htdocs\PP\assets\php\Classes\Database.php on line 18
Process finished with exit code 255
config.php
我已包括在内的
include_once dirname(__DIR__, 2) . "/vendor/autoload.php";
include_once "autoload.php";
任何帮助。
I'm trying to use MONGODB in my project and I just started using it when it has this problem. I created another directory and ran the files with no problems whatsoever. What is wrong with this? I keep getting Fatal error: Uncaught Error: Call to undefined method MongoDB\Database::insertOne()
error message.
This is my code to the class called Database which is producing the error.
<?php
namespace auth;
include_once dirname(__DIR__) . "/config.php";
class Database
{
public function __construct(
private readonly string $dbname,
){
$this->run();
}
private function run(): void
{
$collection = (new \MongoDB\Client)->{$this->dbname};
$insertOneResult = $collection->insertOne([
'username' => 'admin',
'email' => '[email protected]',
'name' => 'Admin User',
]);
printf("Inserted %d document(s)\n", $insertOneResult->getInsertedCount());
var_dump($insertOneResult->getInsertedId());
}
}
and my test.php
file just contained some lines of code
include_once "./assets/php/config.php";
$database = new \auth\Database("test->users");
I definitely loaded the class MongoDB and my config.php includes the vendor file of composer's.
This is the error in full.
Fatal error: Uncaught Error: Call to undefined method MongoDB\Database::insertOne() in C:\xampp\htdocs\PP\assets\php\Classes\Database.php:18
Stack trace:
#0 C:\xampp\htdocs\PP\assets\php\Classes\Database.php(11): auth\Database->run()
#1 C:\xampp\htdocs\PP\test.php(15): auth\Database->__construct('test->users')
#2 {main}
thrown in C:\xampp\htdocs\PP\assets\php\Classes\Database.php on line 18
PHP Fatal error: Uncaught Error: Call to undefined method MongoDB\Database::insertOne() in C:\xampp\htdocs\PPa\assets\php\Classes\Database.php:18
Stack trace:
#0 C:\xampp\htdocs\PP\assets\php\Classes\Database.php(11): auth\Database->run()
#1 C:\xampp\htdocs\PP\test.php(15): auth\Database->__construct('test->users')
#2 {main}
thrown in C:\xampp\htdocs\PP\assets\php\Classes\Database.php on line 18
Process finished with exit code 255
The config.php
file that I have included
include_once dirname(__DIR__, 2) . "/vendor/autoload.php";
include_once "autoload.php";
Thanks for any help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
啊是对我的问题的回答,我在编写代码时不正确地思考。 `mongodb \ collection''类接受 来自用户的参数,一个内部设置。用户提供的参数是数据库名称 收集名称。 名称。
数据库字符串而不是集合
但是,我不知道
mongodb
使用db
和collection_name
整个mongodb \ client
文件,没有说明“魔术常数”的代码?Ah yes answer to my question, I'm just well not thinking properly at the time of writing the code. The `MongoDB\Collection" class accepts two arguments from the user and one internally set. The user provided argument is the database name and collection name. Doing it with
(new \MongoDB\Client)->{$this->dbname}
is wrong because it provides just the database string and not the collection name.I resolved this issue by changing how the structure looks of the collection.
md
is just the alias for MongoDB class as I do not want to type such a long string everytime.However, I have no idea how
mongoDB
uses thedb
andcollection_name
as magic constants as I have looked through the entireMongoDB\Client
file and found there is no code stating those "magic constants"? I would appreciate if someone has an answer to this.