How can I connect mongodb database with zend framework 3

Question

I am trying to connect mongodb database with zend framework 3 but I am not able to succeed. I have used the DoctrineOdmMongodb module but I am getting cache errors. I have tried almost one day and following tutorials but not able to solve that error. Now I am trying to connect directly to mongodb without any module. I am attaching the code files. Please help me to get out of this.

global.php

    $config = [
    'driver'   => 'mongodb',
    'host'     => '127.0.0.1',
    'port'     => 27017,
    'username'       => 'blog',
    'password'       => 'blog',
    'dbname'   => 'blog',
    'connection_string'=> sprintf('mongodb://%s:%d/%s','127.0.0.1','27017','blog')
],

IndexController.php

private $db;

public function __construct($config) 
{
    $this->connect($config);
}

private function connect($config){
    try{
        if ( !class_exists('Mongo')){
            echo ("The MongoDB PECL extension has not been installed or enabled");
            return false;
        }
        $connection=  new \MongoClient($config['connection_string'],array('username'=>$config['username'],'password'=>$config['password']));
        return $this->db = $connection->selectDB($config['dbname']);
    }catch(Exception $e) {
        return false;
    }
}

public function indexAction(){
    error_reporting(E_ALL);
    ini_set('display_errors', true);
    return new ViewModel();
}

Show source
| php   | zend-framework   | mongodb   2017-10-08 06:10 0 Answers

Answers to How can I connect mongodb database with zend framework 3 ( 0 )

Leave a reply to - How can I connect mongodb database with zend framework 3

◀ Go back