如何在codeigniter中集成mongodb库,以便与mongodb进行数据库连接

use*_*540 5 codeigniter mongodb-php

我正在使用codeigniter和mongodb数据库创建应用程序.由于codeigniter不包含mongodbs驱动程序,我使用内置库连接mongodb.我用这个链接创建了mongodb configration文件和Mongo_db库.配置文件就像application\config\mongodb:

<?php
$config['default']['mongo_hostbase'] = 'localhost:27017';
$config['default']['mongo_database'] = 'test';
$config['default']['mongo_username'] = '';
$config['default']['mongo_password'] = '';
$config['default']['mongo_persist']  = FALSE;
$config['default']['mongo_persist_key']  = 'ci_persist';
$config['default']['mongo_replica_set']  = FALSE;
$config['default']['mongo_query_safety'] = 'safe';
$config['default']['mongo_suppress_connect_error'] = FALSE;
$config['default']['mongo_host_db_flag']   = FALSE; 

?> 
Run Code Online (Sandbox Code Playgroud)

和Mongo_db库是这样的:

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Mongo_db
{
  public function __construct()
    {

                //Check mongodb is installed in your server otherwise display an error
        if ( ! class_exists('Mongo'))
        {
            $this->_show_error('The MongoDB PECL extension has not been installed or enabled', 500);
        }

                //get instance of CI class
        if (function_exists('get_instance'))
        {
            $this->_ci = get_instance();
        }

        else
        {
            $this->_ci = NULL;
        }   

                //load the config file which we have created in 'config' directory
        $this->_ci->load->config('mongodb');


            $config='default';
                // Fetch Mongo server and database configuration from config file which we have created in 'config' directory
                $config_data = $this->_ci->config->item($config);  

            try{
                   //connect to the mongodb server
           $this->mb = new Mongo('mongodb://'.$config_data['mongo_hostbase']);
                   //select the mongodb database 
                   $this->db=$this->mb->selectDB($config_data['mongo_database']);
        } 
        catch (MongoConnectionException $exception)
        {
                     //if mongodb is not connect, then display the error
            show_error('Unable to connect to Database', 500);           
        }


}
?>
Run Code Online (Sandbox Code Playgroud)

当我运行应用程序时,它给我一个错误,如遇到错误您的application/config/mongodb.php文件似乎不包含有效的配置数组.

任何人都可以告诉如何解决此错误并设置与mongodb的连接?

God*_*lla 3

您可能想尝试这个存储库: https: //github.com/vesparny/cimongo-codeigniter-mongodb-library,似乎很容易使用。

PHP 库的完整列表可以在这里找到: http://docs.mongodb.org/ecosystem/drivers/php-libraries/