Codeigniter使用MongoDB

Reference

安装PECL

PECL is a repository for PHP Extensions, providing a directory of all known extensions and hosting facilities for downloading and development of PHP extensions.The packaging and distribution system used by PECL is shared with its sister, PEAR.

http://www.runoob.com/mongodb/mongodb-install-php-driver.html

下载扩展

https://pecl.php.net/package/mongodb 找到对应的版本 – 对应的是否线程安全(nts vs ts)

放入目录

下载完你需要的二进制包后,解压压缩包,将 php_mongodb.dll文件添加到你的PHP扩展目录中(ext)。ext目录通常在PHP安装目录下的ext目录。

打开php配置文件 php.ini 添加以下配置:

extension=php_mongodb.dll

重启服务器。

《Codeigniter使用MongoDB》 image

这样就安装完成了扩展。

下载codeigniter-mongodb-library

用git clone下载后目录结构为

codeigniter-mongodb-library
├── application
│   ├── config
│   │   └── mongo_db.php
│   └── libraries
│       └── Mongo_db.php
├── LICENSE
└── README.md

3 directories, 4 files

添加文件

config

然后将config\mongo_db.php放到Codeigniter框架application的conf目录下,并配置对应的选项。

<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/*
| -------------------------------------------------------------------------
| DATABASE CONNECTIVITY SETTINGS
| -------------------------------------------------------------------------
| This file will contain the settings needed to access your Mongo database.
|
|
| ------------------------------------------------------------------------
| EXPLANATION OF VARIABLES
| ------------------------------------------------------------------------
|
|   ['hostname'] The hostname of your database server.
|   ['username'] The username used to connect to the database
|   ['password'] The password used to connect to the database
|   ['database'] The name of the database you want to connect to
|   ['db_debug'] TRUE/FALSE - Whether database errors should be displayed.
|   ['write_concerns'] Default is 1: acknowledge write operations.  ref(http://php.net/manual/en/mongo.writeconcerns.php)
|   ['journal'] Default is TRUE : journal flushed to disk. ref(http://php.net/manual/en/mongo.writeconcerns.php)
|   ['read_preference'] Set the read preference for this connection. ref (http://php.net/manual/en/mongoclient.setreadpreference.php)
|   ['read_preference_tags'] Set the read preference for this connection.  ref (http://php.net/manual/en/mongoclient.setreadpreference.php)
|
| The $config['mongo_db']['active'] variable lets you choose which connection group to
| make active.  By default there is only one group (the 'default' group).
|
*/

$config['mongo_db']['active'] = 'default';

$config['mongo_db']['default']['no_auth'] = true;
$config['mongo_db']['default']['hostname'] = 'localhost';
$config['mongo_db']['default']['port'] = '27017';
$config['mongo_db']['default']['username'] = '';
$config['mongo_db']['default']['password'] = '';
$config['mongo_db']['default']['database'] = 'ci_test'; // 测试数据库ci_test
$config['mongo_db']['default']['db_debug'] = TRUE;
$config['mongo_db']['default']['return_as'] = 'array';
$config['mongo_db']['default']['write_concerns'] = (int)1;
$config['mongo_db']['default']['journal'] = TRUE;
$config['mongo_db']['default']['read_preference'] = 'primary'; 
$config['mongo_db']['default']['read_concern'] = 'local'; //'local', 'majority' or 'linearizable'
$config['mongo_db']['default']['legacy_support'] = TRUE;

/* End of file database.php */
/* Location: ./application/config/database.php */

可以修改 mongo_db下面的键名 default ,但是我这里保持默认。

修改autoload.php

《Codeigniter使用MongoDB》 image

mongo_db加入到$autoload['libraries']的array中。

libraries

将 下载文件夹中的 libraries\Mongo_db.php 放到对应框架的application中去application/libraries/Mongo_db.php

使用

在controller的方法中这样使用

public function index()
    {
        $this->load->library('mongo_db',array('activate'=>'default'),'mongo_db');
        $res = $this->mongo_db->get('test');
        echo 'codeigniter + MongoDB';
        echo '<pre>';
        print_r($res);
    }

《Codeigniter使用MongoDB》 image
《Codeigniter使用MongoDB》 image

具体的使用方法可以看手册:https://github.com/intekhabrizvi/codeigniter-mongodb-library

更好的是直接看 lib 中的源码!注释里面都有usage

get

@usage : $this->mongo_db->get('foo');

get($collection = "")

选择查看的 集合名称 string类型,返回该集合所有内容(所有字段)

get_where

@usage : $this->mongo_db->get_where('foo', array('bar' => 'something'));

get_where($collection = "", $where = array())
  • 参数一:集合名称 字符串
  • 参数二:条件 array数组

就是和find命令一样,返回符合where这个条件的内容。

select

@usage: $this->mongo_db->select(array('foo', 'bar'))->get('foobar');

select($includes = array(), $excludes = array())

select第一个参数是包括的字段,第二个是除外的字段,然后再调用get,就可以只显示固定的字段了。(但是好像_id都会返回)

find_one

@usage : $this->mongo_db->find_one('foo');

find_one($collection = "")

只返回集合中的一个文件

where

@usage : $this->mongo_db->where(array('foo' => 'bar'))->get('foobar');

where($wheres, $value = null)

这个$wheres就是一个 键名 – 键值 和 列名 – 查找值 对应的关系

update

@usage: $this->mongo_db->update('foo', $data = array());

update($collection = "", $options = array())

一般和where和set连用吧。where找到具体记录 set后update更新

  • 参数1:集合名 string
  • 参数2:选项

set

@usage: $this->mongo_db->where(array('blog_id'=>123))->set('posted', 1)->update('blog_posts');

@usage: $this->mongo_db->where(array('blog_id'=>123))->set(array('posted' => 1, 'time' => time()))->update('blog_posts');

set($fields, $value = NULL)

这个set用来设置具体字段的内容的,如usage上的写法,可以先调用where在用set然后显式的update

delete

@usage : $this->mongo_db->delete('foo');

delete($collection = "")

应该也是做最后的一个显式提交,先用where或者其他方式找到某个文件,然后delete下。

    原文作者:二歪求知iSk2y
    原文地址: https://www.jianshu.com/p/9a85c9bd47b5
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞