zoukankan      html  css  js  c++  java
  • thinkPHP 3.2.3操作MongoDB指南

    今天使用thinkPHP操作MongoDB发现跟用MYSQL有很多不同的地方,在这里特别跟大家分享下.

    暂时没用thinkPHP5一直还在用thinkPHP3.2.3觉得挺好用,MongoDB版本2和3均测试通过.

    config.php

    //连接mongoDB
        'DB_TYPE'    => 'mongo',             // 数据库类型
        'DB_HOST'    => '127.0.0.1',  // 服务器地址
        'DB_NAME'    => 'local',             // 数据库名
        'DB_USER'    => '',                 // 用户名
        'DB_PWD'     => '',                 // 密码
        'DB_PORT'    => '27017',             // 端口
        'DB_CHARSET' =>  'utf8',             // 数据库编码
        'DB_DEBUG'   =>  false,                // 数据库调试模式 开启后可以记录SQL日志

    IndexController.class.php

    <?php
    namespace HomeController;
    use HomeModelColModel;
    use ThinkController;
    class IndexController extends Controller {
        //http://localhost/testmdb/Index
        //增
        public function index(){
            $db = D("Col");
            $data['name'] = '张雷帅哥';
            $data['reg']  = time();
            $returl       = $db ->add($data);
            var_dump($returl);
        }
        //http://localhost/testmdb/Index/chaxun/name/张雷帅哥
        //查
        public function chaxun($name=''){
            $db     = D("Col");
            $returl = $db ->where(array("name"=>$name)) ->select();
            var_dump($returl);
        }
        //http://localhost/testmdb/Index/xiugai/name/张雷帅哥
        //改
        public function xiugai($name=''){
            $db          = D("Col");
            $data['reg'] = '20170310';
            $returl      = $db ->where(array("name"=>$name)) ->save($data);
            var_dump($returl);
        }
        //http://localhost/testmdb/Index/shan/id/58c2483e4b1486d073000032
        //删
        public function shan($id=''){
            $db     = D("Col");
            $returl = $db ->where(array("_id"=>$id)) ->delete();
            var_dump($returl);
        }
    }

    ColModel.class.php

    <?php
    namespace HomeModel;
    use ThinkModelMongoModel;
    Class ColModel extends MongoModel{
    }
     
  • 相关阅读:
    企业级应用框架设计备忘录
    DBHelper
    Oracle客户端精简绿色版 不安装oracle客户端 转载
    回车转TAB
    excel列显示列号 转载
    XtraTreeList Drag Event
    XmlSerializer vs DataContractSerializer: Serialization in Wcf 转载
    转载 在Windows64位环境下.net访问Oracle解决方案
    正则表达式学习笔记
    SQL相关子查询的例子
  • 原文地址:https://www.cnblogs.com/tdalcn/p/7832089.html
Copyright © 2011-2022 走看看