首先安装 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;
}
/**
* 插入数据的时候 获取到目前Id 应该的值
*/
public function max_returnId()
{
//过滤条件
// $filter = ['age' => ['$gt' => 20]];
$filter = [];
//可选项
$option = [
'projection' => ["_id" =>0], //不显示_id,此字段用于指定显示或不显示某些字段
'sort' => ['Id' => -1], //按照某些字段排序 ,-1倒叙,1正序
'limit'=>1, //取多少条数据
// 'skip' => ($num-1)*$pageSize //取第几页数据
];
//查询数据
$query = new \MongoDB\Driver\Query($filter, $option);
//查询结果,返回数组
$cursor = $this->manager->executeQuery('moba_game.shopGoods',$query);
foreach ($cursor as $document){
return $document->Id + 1;
}
}
}
增删改查都有了 目前在做求和 各种更详细的操作