Search code examples
phpmongodbxamppundefined-reference

MongoDB insertOne function not found


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' => 'admin@example.com',
            '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.


Solution

  • 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.