PHP7对mongodb的curd操作

1.获取mongodb的连接对象

  $mongo = new \MongoDB\Driver\Manager("mongodb://192.168.0.127:27017");

2.插入一个集合

  /**
      * 创建一个集合
      */
     public function  createCollection()
     {
         $mongo = new \MongoDB\Driver\Manager("mongodb://192.168.0.127:27017",array(
                 'username' =>"admin",
                 'password' => "123456",
             )
         );
         $writeConcern = new \MongoDB\Driver\WriteConcern(\MongoDB\Driver\WriteConcern::MAJORITY, 1000);
         $bulk = new \MongoDB\Driver\BulkWrite;
         $document = ['_id' => new \MongoDB\BSON\ObjectID, 'name' => '张三',"age"=>18];
         $_id= $bulk->insert($document);
         $result = $mongo->executeBulkWrite('local.runoob', $bulk, $writeConcern);
         print_r($result);
     }

3.查询mongoDb文档

/**
    * 查询mongodb文档
    */
   public function  find()
   {
             $manager = new \MongoDB\Driver\Manager("mongodb://192.168.0.127:27017",array(
               'username' =>"admin",
               'password' => "123456",
           )
       );
       $filter = ['ip' => ['$gt' => "\"192.168.0.124\""]];
       $options = [
//           'projection' => ['oid' =>1,"createTime"=>1,"ip"=>1],  //指定返回文档的键值,1 为显示,0 不展示
           'sort' => ['createTime' => -1], //排序 -1 为降序 ,1 升序
           "limit"=>5, //分页查询的数量
       ];

// 查询数据
       $query = new \MongoDB\Driver\Query($filter, $options);
       $result = $manager->executeQuery('test.user', $query);
       $data=[];
       $rows=[];
       foreach ($result as $v){
           $rows[]=json_decode(json_encode($v,JSON_UNESCAPED_UNICODE),true);
       }
       $data['rows']=$rows;
       $data['total']=count($rows);
       print_r($data);
   }

4.更新文档

   /**
    * 更新文档
    */
   public function  update()
   {
       $manager = new \MongoDB\Driver\Manager("mongodb://192.168.0.127:27017",array(
               'username' =>"admin",
               'password' => "123456",
           )
       );

       $bulk = new \MongoDB\Driver\BulkWrite;
       $where=["account"=>89312135258];
       $param=['$set'=> ["updateTime"=>date("Y-m-d H:i:s",time())]];
       $bulk->update($where,$param);
       $writeConcern = new \MongoDB\Driver\WriteConcern(\MongoDB\Driver\WriteConcern::MAJORITY, 1000);//可选,修改确认
       $res = $manager->executeBulkWrite('test.user', $bulk, $writeConcern);
       print_r($res);
   }

5.删除一个文档

 /**
    * 删除一个文档
    */
   public function  del()
   {
       $manager = new \MongoDB\Driver\Manager("mongodb://192.168.0.127:27017",array(
               'username' =>"admin",
               'password' => "123456",
           )
       );
       $bulk = new \MongoDB\Driver\BulkWrite;
       $bulk->delete(['ip' => "192.168.0.125"]);
       $writeConcern = new \MongoDB\Driver\WriteConcern(\MongoDB\Driver\WriteConcern::MAJORITY, 1000);//可选,修改确认
       $res = $manager->executeBulkWrite('test.user', $bulk, $writeConcern);
       print_r($res);
   }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值