John Davidson

php - MongoDB insertOne function not found

0 comments
Message:


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.



My Answer:
The issue here is that you are trying to use the `insertOne()` method on the `$collection` object, which is an instance of `MongoDB\Collection`, not `MongoDB\Database`.

To fix this issue, you need to make sure that you are accessing the correct collection within the database. Here is an updated version of your `Database` class:

php

namespace auth;
include_once dirname(__DIR__) . "/config.php";

use MongoDB\Client;

class Database
{
public function __construct(
private string $dbname,
){
$this->run();
}

private function run(): void
{
$client = new Client();
$collection = $client->{$this->dbname}->users;

$insertOneResult = $collection->insertOne([
'username' => 'admin',
'email' => 'admin@example.com',
'name' => 'Admin User',
]);

printf("Inserted %d document(s)\n", $insertOneResult->getInsertedCount());

var_dump($insertOneResult->getInsertedId());
}
}


In this updated version, we are accessing the `users` collection within the specified database. Make sure that the database and collection names are correct in your MongoDB setup.

Also, make sure that you have the MongoDB PHP extension installed and enabled in your PHP configuration. You can check this by running `phpinfo()` and looking for the MongoDB section.

I hope this helps! Let me know if you have any other questions.

Rate this post

5 of 5 based on 2903 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us