thinkPHP5框架mongodb扩展安装及特殊操作示例

2018-12-1917:53:48后端程序开发Comments3,268 views字数 1483阅读模式

实例讲述了TP5(thinkPHP5)框架mongodb扩展安装及特殊操作。具体如下:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/8860.html

ThinkPHP 5.0 MongoDb驱动文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/8860.html

首先安装官方的mongodb扩展:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/8860.html

http://pecl.php.net/package/mongodb文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/8860.html

找到对应的php版本的扩展文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/8860.html

然后,配置应用的数据库配置文件database.php的type参数为:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/8860.html

'type' => '\think\mongo\Connection',

即可正常使用MongoDb,例如:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/8860.html

使用最新mongodb扩展文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/8860.html

Db::name('demo')
  ->find();
Db::name('demo')
  ->field('id,name')
  ->limit(10)
  ->order('id','desc')
  ->select();

或者使用模型操作:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/8860.html

User::get(1);
User::all('1,2,3');

MongoDb默认的主键是_id并且是一个ObjectID对象,如果需要和mysql一样使用id作为主键,可以如下参数:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/8860.html

// 强制把_id转换为id
'pk_convert_id' => true,

tp5 Mongodb特殊操作文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/8860.html

Push操作文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/8860.html

添加数据文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/8860.html

public function add(){
    $this->data = [
      '作者'  => 'tuzi',
      '年龄'  => '22',
      '标题'  => '防塔与补兵',
      '评论'  => [
        [
          '序号'  => '001',
          '内容'  => '五杀'
        ]
      ]
    ];
    $res = Db::table('document')->insert($this->data);
    if($res){
      echo "success";
    }else{
      echo "error";
    }
}

结果文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/8860.html

array(1) {
[0] => array(5) {
["_id"] => object(MongoDB\BSON\ObjectId)#12 (1) {
["oid"] => string(24) "5a51f73083869e4b65549c36"
}
["作者"] => string(4) "tuzi"
["年龄"] => string(2) "22"
["标题"] => string(15) "防塔与补兵"
["评论"] => array(1) {
[0] => array(2) {
["序号"] => string(3) "001"
["内容"] => string(6) "五杀"
}
}
}
}文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/8860.html

利用push更新数据文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/8860.html

public function update()
{
    $update_data['评论'] = [
      '$push',
      [
        '序号'  => '002',
        '内容'  => '三杀'
      ]
    ];
    $update_res = Db::table('document')->where('标题','防塔与补兵')->update($update_data);
    if($update_res){
      echo "success";
    }else{
      echo "error";
    }
}

结果文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/8860.html

array(1) {
[0] => array(5) {
["_id"] => object(MongoDB\BSON\ObjectId)#12 (1) {
["oid"] => string(24) "5a51f73083869e4b65549c36"
}
["作者"] => string(4) "tuzi"
["年龄"] => string(2) "22"
["标题"] => string(15) "防塔与补兵"
["评论"] => array(2) {
[0] => array(2) {
["序号"] => string(3) "001"
["内容"] => string(6) "五杀"
}
[1] => array(2) {
["序号"] => string(3) "002"
["内容"] => string(6) "三杀"
}
}
}
}文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/8860.html

文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/8860.html
  • 本站内容整理自互联网,仅提供信息存储空间服务,以方便学习之用。如对文章、图片、字体等版权有疑问,请在下方留言,管理员看到后,将第一时间进行处理。
  • 转载请务必保留本文链接:https://www.cainiaoxueyuan.com/bc/8860.html

Comment

匿名网友 填写信息

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定