Thinkphp 操作mongodb数据库的增删改查 转

转自:https://blog.csdn.net/x007008009/article/details/108491682?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522160190382319724835803327%2522%252C%2522scm%2522%253A%252220140713.130102334.pc%255Fall.%2522%257D&request_id=160190382319724835803327&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~all~first_rank_ecpm_v3~pc_rank_v2-7-108491682.first_rank_ecpm_v3_pc_rank_v2&utm_term=thinkphp+mongodb&spm=1018.2118.3001.4187

首先安装 mongodb扩展

<?php

namespace app\admin\model;
use think\Model;
class Monmodel extends Model
{
public $dbName ='moba_game';//(要连接的数据库名称)
public $manager;
public function __construct()
{
$this->manager = new \MongoDB\Driver\Manager("mongodb://账号:密码@Ip地址:27017");
}

/**
* 查询数据
* name 查询的表名
* filter
* options
*/
public function Looktable($name ,$filter = [], $options = [] )
{
$block = $this->dbName .'.'. $name;
$query = new \MongoDB\Driver\Query($filter, $options);
$cursor = $this->manager->executeQuery($block, $query);
return $cursor;
}
/**
* 插入数据
* name 块名
* array 插入的数据
*/
public function Inserttable($name , $array)
{
$bulk = new \MongoDB\Driver\BulkWrite;
$block = $this->dbName .'.'. $name;
$bulk->insert($array);
$result = $this->manager->executeBulkWrite($block, $bulk);
return $result;
}
/**
* 删除数据
*/
public function deleteTable($name , $array)
{
$bulk = new \MongoDB\Driver\BulkWrite;
$block = $this->dbName .'.'. $name;
$bulk->delete($array);
$result = $this->manager->executeBulkWrite($block, $bulk);
return $result;
}
/**
* 修改数据
*/
public function updateTable($name , $where , $newdata)
{
$bulk = new \MongoDB\Driver\BulkWrite;
$block = $this->dbName .'.'. $name;
$bulk->update($where, array('$set' => $newdata), array('multi' => false, 'upsert' => false));
$result = $this->manager->executeBulkWrite($block, $bulk);
return $result;
}
}

增删改查都有了  目前在做求和 各种更详细的操作

点赞

发表回复

电子邮件地址不会被公开。必填项已用 * 标注