Yii2 with mongodb

hello everyone, im new with mongodb, i have installed mongodb in php 8.1 and add extension mongodb to yii2. but i got error like this when i try to login.


Call to a member function getCollection() on null

here my config db.php

<?php return [ 'class' => '\yii\mongodb\Connection', 'dsn' => 'mongodb://root:root@localhost:27017/veristoretools2', ]; i use windows 10, php 8.1, yii2, mongodb 7.0.5 please help me

Can you post the rest of the error message as well?
For example, a screenshot of the error page?

this is the screenshot

Where exactly do you get error?
Relevant code?

idk the relevant code, i just followed tutorial to connect with mongodb, and when i set user model to get collection from mongodb and try to login i got this error. but when i return the code to connect with mySql and change get collection to get table in user model its work. is my installation of mongodb have something wrong or what ? im so confused

Unfortunately, until I see relevant code you are complaining is not working, I cannot further help.
Hard to guess, and even harder to peep on your screen and see it.

when i change my model file to this
use yii\mongodb\ActiveRecord;

public static function collectionName() {
return ‘user’;
and config file to this
return [
‘class’ => ‘yii\mongodb\Connection’,
‘dsn’ => ‘mongodb://localhost:27017/mydatabase’,

i got error above. when i change to

use yii\db\ActiveRecord;

public static function tableName() {
return ‘user’;
and use mysql connection in config file it work.

my db in mongodb not empty. but i always getting error above if i change the code on model and config. did I do the wrong step or installation?

That code just shows how you define. No code as to how you call it, nothing! Please honor contributor’s time by giving all relevant information. It is not in the best of your interest to give half baked information!

Check out thia tutorial:

thank you for your time and so sorry for my question.

No need to be sorry. You are always welcom!