MongoDB插入函数找不到

发布于 2025-02-05 04:08:38 字数 2356 浏览 3 评论 0原文

我正在尝试在我的项目中使用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.phpfile that I have included

include_once dirname(__DIR__, 2) . "/vendor/autoload.php";
include_once "autoload.php";

Thanks for any help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

以为你会在 2025-02-12 04:08:38

啊是对我的问题的回答,我在编写代码时不正确地思考。 `mongodb \ collection''类接受 来自用户的参数,一个内部设置。用户提供的参数是数据库名称 收集名称。 名称。

数据库字符串而不是集合

$collection = (new md\Client)->$db->$collection_name;

但是,我不知道mongodb使用dbcollection_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.

$collection = (new md\Client)->$db->$collection_name;

However, I have no idea how mongoDB uses the db and collection_name as magic constants as I have looked through the entire MongoDB\Client file and found there is no code stating those "magic constants"? I would appreciate if someone has an answer to this.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文