Tuesday, March 8, 2022

[FIXED] Yii2 Can't connect to the MongoDB cluster

Issue

I try to find out what's wrong on https://github.com/yiisoft/yii2-mongodb, but someone just keeps me the link to docs and official chat. There is no any answer how to connect to the cluster. Please help me to solve this issue, I try a lot and spent last 2 days to solve it, but can't understand why it doesn't connect.

I try to configure yii2-mongodb on my project, installed it using composer, then add

in config/web.php

'mongodb' => [
    'class' => '\yii\mongodb\Connection',
    'dsn' => 'mongodb://user:password@insta1-shard-00-00-s0vxe.mongodb.net:27017,insta1-shard-00-01-s0vxe.mongodb.net:27017,insta1-shard-00-02-s0vxe.mongodb.net:27017/database?ssl=true&replicaSet=Insta1-shard-0&authSource=admin',
]

and use this model:

<?php

namespace app\models;

use Yii;
use yii\base\Model;
use yii\mongodb\Query;

/**
 * BlogContent is the model behind the contact form.
 */
class BlogContent extends Model
{

    public function getPostList($page) {
        $query = new Query();
        // compose the query
        $rows = $query->select(['name', 'price', 'title'])
            ->from('services')->all();
    }

}

it should return me all documents from the collection, but it returns to me this stack:

yii\base\UnknownMethodException: Calling unknown method: yii\mongodb\QueryBuilder::build() in /var/www/service/vendor/yiisoft/yii2/base/Object.php:220
Stack trace:
#0 /var/www/service/vendor/yiisoft/yii2/db/Query.php(126): yii\base\Object->__call('build', Array)
#1 /var/www/service/vendor/yiisoft/yii2/db/Query.php(213): yii\db\Query->createCommand(Object(yii\mongodb\Connection))
#2 /var/www/service/models/BlogContent.php(21): yii\db\Query->all()
#3 /var/www/service/controllers/BlogController.php(35): app\models\BlogContent->getPostList(1)
#4 [internal function]: app\controllers\BlogController->actionIndex(1)
#5 /var/www/service/vendor/yiisoft/yii2/base/InlineAction.php(57): call_user_func_array(Array, Array)
#6 /var/www/service/vendor/yiisoft/yii2/base/Controller.php(156): yii\base\InlineAction->runWithParams(Array)
#7 /var/www/service/vendor/yiisoft/yii2/base/Module.php(523): yii\base\Controller->runAction('index', Array)
#8 /var/www/service/vendor/yiisoft/yii2/web/Application.php(102): yii\base\Module->runAction('blog/index', Array)
#9 /var/www/service/vendor/yiisoft/yii2/base/Application.php(380): yii\web\Application->handleRequest(Object(yii\web\Request))
#10 /var/www/service/web/index.php(12): yii\base\Application->run()
#11 {main}

Additional info:

Yii version | ~2.0.5

Yii MongoDB version | ~2.1.0

MongoDB server version | MongoDB Atlas

PHP version | 7.0

Operating system | Ubuntu 16.04

I solve this first issue by passing a $db connection to query as an optional argument, but it can't connect anyway. Also, I test connection to MongoDB on my local machine - for a single database it works as need.


Solution

The issue was because my php-mongodb has a conflict with php7, after I update it all begin to work as need.



Answered By - Alex

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.