Properties

$connection

$connection : 

Type

$builder

$builder : 

Type

$model

$model : 

Type

$table

$table : 

Type

$name

$name : 

Type

$pk

$pk : 

Type

$prefix

$prefix : 

Type

$options

$options : 

Type

$info

$info : 

Type

$event

$event : 

Type

Methods

__construct()

__construct(\think\mongo\Connection  $connection = null, string  $model = '') 

架构函数

Parameters

\think\mongo\Connection $connection

数据库对象实例

string $model

模型名

__call()

__call(string  $method, array  $args) : mixed

利用__call方法实现一些特殊的Model方法

Parameters

string $method

方法名称

array $args

调用参数

Throws

\think\exception\DbException
\think\Exception

Returns

mixed

getConnection()

getConnection() : \think\mongo\Connection

获取当前的数据库Connection对象

Returns

\think\mongo\Connection

connect()

connect(mixed  $config) : $this

切换当前的数据库连接

Parameters

mixed $config

Returns

$this

name()

name(string  $name) : $this

指定默认的数据表名(不含前缀)

Parameters

string $name

Returns

$this

setTable()

setTable(string  $table) : $this

指定默认数据表名(含前缀)

Parameters

string $table

表名

Returns

$this

getTable()

getTable(string  $name = '') : string

得到当前或者指定名称的数据表

Parameters

string $name

Returns

string

pk()

pk(string  $pk) : $this

指定数据表主键

Parameters

string $pk

主键

Returns

$this

removeWhereField()

removeWhereField(string  $field, string  $logic = 'and') : $this

去除某个查询条件

Parameters

string $field

查询字段

string $logic

查询逻辑 and or xor

Returns

$this

removeOption()

removeOption(string|boolean  $option = true) : $this

去除查询参数

Parameters

string|boolean $option

参数名 true 表示去除所有参数

Returns

$this

parseSqlTable()

parseSqlTable(string  $sql) : string

将SQL语句中的__TABLE_NAME__字符串替换成带前缀的表名(小写)

Parameters

string $sql

sql语句

Returns

string

query()

query(string  $namespace, \MongoDB\Driver\Query  $query, \MongoDB\Driver\ReadPreference  $readPreference = null, boolean|string  $class = false, string|array  $typeMap = null) : mixed

执行查询 返回数据集

Parameters

string $namespace
\MongoDB\Driver\Query $query

查询对象

\MongoDB\Driver\ReadPreference $readPreference

readPreference

boolean|string $class

指定返回的数据集对象

string|array $typeMap

指定返回的typeMap

Throws

\MongoDB\Driver\Exception\AuthenticationException
\MongoDB\Driver\Exception\InvalidArgumentException
\MongoDB\Driver\Exception\ConnectionException
\MongoDB\Driver\Exception\RuntimeException

Returns

mixed

command()

command(\MongoDB\Driver\Command  $command, string  $dbName = '', \MongoDB\Driver\ReadPreference  $readPreference = null, boolean|string  $class = false, string|array  $typeMap = null) : mixed

执行指令 返回数据集

Parameters

\MongoDB\Driver\Command $command

指令

string $dbName
\MongoDB\Driver\ReadPreference $readPreference

readPreference

boolean|string $class

指定返回的数据集对象

string|array $typeMap

指定返回的typeMap

Throws

\MongoDB\Driver\Exception\AuthenticationException
\MongoDB\Driver\Exception\InvalidArgumentException
\MongoDB\Driver\Exception\ConnectionException
\MongoDB\Driver\Exception\RuntimeException

Returns

mixed

execute()

execute(string  $namespace, \MongoDB\Driver\BulkWrite  $bulk, \MongoDB\Driver\WriteConcern  $writeConcern = null) : integer

执行语句

Parameters

string $namespace
\MongoDB\Driver\BulkWrite $bulk
\MongoDB\Driver\WriteConcern $writeConcern

Throws

\MongoDB\Driver\Exception\AuthenticationException
\MongoDB\Driver\Exception\InvalidArgumentException
\MongoDB\Driver\Exception\ConnectionException
\MongoDB\Driver\Exception\RuntimeException
\MongoDB\Driver\Exception\BulkWriteException

Returns

integer

getLastInsID()

getLastInsID() : string

获取最近插入的ID

Returns

string

getLastSql()

getLastSql() : string

获取最近一次执行的指令

Returns

string

getConfig()

getConfig(string  $name = '') : boolean

获取数据库的配置参数

Parameters

string $name

参数名称

Returns

boolean

value()

value(string  $field, mixed  $default = null) : mixed

得到某个字段的值

Parameters

string $field

字段名

mixed $default

默认值

Returns

mixed

column()

column(string  $field, string  $key = '') : array

得到某个列的数组

Parameters

string $field

字段名 多个字段用逗号分隔

string $key

索引

Returns

array

cmd()

cmd(string|array|object  $command, mixed  $extra = null, string  $db = null) : array

执行command

Parameters

string|array|object $command

指令

mixed $extra

额外参数

string $db

数据库名

Returns

array

distinct()

distinct(string  $field) : array

指定distinct查询

Parameters

string $field

字段名

Returns

array

listCollections()

listCollections(string  $db = '') 

获取数据库的所有collection

Parameters

string $db

数据库名称 留空为当前数据库

Throws

\think\Exception

count()

count() : integer

COUNT查询

Returns

integer

multiAggregate()

multiAggregate(array  $aggregate, array  $groupBy) : array

多聚合操作

Parameters

array $aggregate

聚合指令, 可以聚合多个参数, 如 ['sum' => 'field1', 'avg' => 'field2']

array $groupBy

类似mysql里面的group字段, 可以传入多个字段, 如 ['field_a', 'field_b', 'field_c']

Returns

array —

查询结果

aggregate()

aggregate(string  $aggregate, string  $field) : mixed

聚合查询

Parameters

string $aggregate

聚合指令

string $field

字段名

Returns

mixed

max()

max(string  $field) : float

MAX查询

Parameters

string $field

字段名

Returns

float

min()

min(string  $field) : mixed

MIN查询

Parameters

string $field

字段名

Returns

mixed

sum()

sum(string  $field) : float

SUM查询

Parameters

string $field

字段名

Returns

float

avg()

avg(string  $field) : float

AVG查询

Parameters

string $field

字段名

Returns

float

setField()

setField(string|array  $field, mixed  $value = '') : integer

设置记录的某个字段值 支持使用数据库字段和方法

Parameters

string|array $field

字段名

mixed $value

字段值

Returns

integer

setInc()

setInc(string  $field, integer  $step = 1, integer  $lazyTime) : integer|true

字段值(延迟)增长

Parameters

string $field

字段名

integer $step

增长值

integer $lazyTime

延时时间(s)

Throws

\think\Exception

Returns

integer|true

setDec()

setDec(string  $field, integer  $step = 1, integer  $lazyTime) : integer|true

字段值(延迟)减少

Parameters

string $field

字段名

integer $step

减少值

integer $lazyTime

延时时间(s)

Throws

\think\Exception

Returns

integer|true

data()

data(mixed  $field, mixed  $value = null) : $this

设置数据

Parameters

mixed $field

字段名或者数据

mixed $value

字段值

Returns

$this

inc()

inc(string|array  $field, integer  $step = 1) : $this

字段值增长

Parameters

string|array $field

字段名

integer $step

增长值

Returns

$this

dec()

dec(string|array  $field, integer  $step = 1) : $this

字段值减少

Parameters

string|array $field

字段名

integer $step

减少值

Returns

$this

where()

where(mixed  $field, mixed  $op = null, mixed  $condition = null) : $this

指定AND查询条件

Parameters

mixed $field

查询字段

mixed $op

查询表达式

mixed $condition

查询条件

Returns

$this

whereOr()

whereOr(mixed  $field, mixed  $op = null, mixed  $condition = null) : $this

指定OR查询条件

Parameters

mixed $field

查询字段

mixed $op

查询表达式

mixed $condition

查询条件

Returns

$this

whereNor()

whereNor(mixed  $field, mixed  $op = null, mixed  $condition = null) : $this

指定NOR查询条件

Parameters

mixed $field

查询字段

mixed $op

查询表达式

mixed $condition

查询条件

Returns

$this

whereNull()

whereNull(mixed  $field, string  $logic = 'and') : $this

指定Null查询条件

Parameters

mixed $field

查询字段

string $logic

查询逻辑 and or xor

Returns

$this

whereNotNull()

whereNotNull(mixed  $field, string  $logic = 'and') : $this

指定NotNull查询条件

Parameters

mixed $field

查询字段

string $logic

查询逻辑 and or xor

Returns

$this

whereIn()

whereIn(mixed  $field, mixed  $condition, string  $logic = 'and') : $this

指定In查询条件

Parameters

mixed $field

查询字段

mixed $condition

查询条件

string $logic

查询逻辑 and or xor

Returns

$this

whereNotIn()

whereNotIn(mixed  $field, mixed  $condition, string  $logic = 'and') : $this

指定NotIn查询条件

Parameters

mixed $field

查询字段

mixed $condition

查询条件

string $logic

查询逻辑 and or xor

Returns

$this

whereLike()

whereLike(mixed  $field, mixed  $condition, string  $logic = 'and') : $this

指定Like查询条件

Parameters

mixed $field

查询字段

mixed $condition

查询条件

string $logic

查询逻辑 and or xor

Returns

$this

whereBetween()

whereBetween(mixed  $field, mixed  $condition, string  $logic = 'and') : $this

指定Between查询条件

Parameters

mixed $field

查询字段

mixed $condition

查询条件

string $logic

查询逻辑 and or xor

Returns

$this

whereNotBetween()

whereNotBetween(mixed  $field, mixed  $condition, string  $logic = 'and') : $this

指定NotBetween查询条件

Parameters

mixed $field

查询字段

mixed $condition

查询条件

string $logic

查询逻辑 and or xor

Returns

$this

whereExp()

whereExp(mixed  $field, mixed  $condition, string  $logic = 'and') : $this

指定Exp查询条件

Parameters

mixed $field

查询字段

mixed $condition

查询条件

string $logic

查询逻辑 and or xor

Returns

$this

whereTime()

whereTime(string  $field, string  $op, string|array  $range = null) : $this

查询日期或者时间

Parameters

string $field

日期字段名

string $op

比较运算符或者表达式

string|array $range

比较范围

Returns

$this

paginate()

paginate(integer|null  $listRows = null, boolean  $simple = false, array  $config = array()) : \think\Paginator

分页查询

Parameters

integer|null $listRows

每页数量

boolean $simple

简洁模式

array $config

配置参数 page:当前页, path:url路径, query:url额外参数, fragment:url锚点, var_page:分页变量, list_rows:每页数量 type:分页类名, namespace:分页类命名空间

Throws

\think\exception\DbException

Returns

\think\Paginator

table()

table(string  $table) : $this

指定当前操作的数据表

Parameters

string $table

表名

Returns

$this

collection()

collection(string  $collection) : $this

指定当前操作的collection

Parameters

string $collection

Returns

$this

cache()

cache(mixed  $key = true, integer  $expire = null, string  $tag = null) : $this

查询缓存

Parameters

mixed $key

缓存key

integer $expire

缓存有效期

string $tag

缓存标签

Returns

$this

useSoftDelete()

useSoftDelete(false|string  $field, mixed  $condition = null) : $this

设置软删除字段及条件(暂无支持)

Parameters

false|string $field

查询字段

mixed $condition

查询条件

Returns

$this

fetchCursor()

fetchCursor(boolean  $cursor = true) : $this

不主动获取数据集

Parameters

boolean $cursor

是否返回 Cursor 对象

Returns

$this

typeMap()

typeMap(string|array  $typeMap) : $this

设置typeMap

Parameters

string|array $typeMap

Returns

$this

master()

master() : $this

设置从主服务器读取数据

Returns

$this

failException()

failException(boolean  $fail = true) : $this

设置查询数据不存在是否抛出异常

Parameters

boolean $fail

是否严格检查字段

Returns

$this

awaitData()

awaitData(boolean  $awaitData) : $this

awaitData

Parameters

boolean $awaitData

Returns

$this

batchSize()

batchSize(integer  $batchSize) : $this

batchSize

Parameters

integer $batchSize

Returns

$this

exhaust()

exhaust(boolean  $exhaust) : $this

exhaust

Parameters

boolean $exhaust

Returns

$this

modifiers()

modifiers(array  $modifiers) : $this

设置modifiers

Parameters

array $modifiers

Returns

$this

noCursorTimeout()

noCursorTimeout(boolean  $noCursorTimeout) : $this

设置noCursorTimeout

Parameters

boolean $noCursorTimeout

Returns

$this

oplogReplay()

oplogReplay(boolean  $oplogReplay) : $this

设置oplogReplay

Parameters

boolean $oplogReplay

Returns

$this

partial()

partial(boolean  $partial) : $this

设置partial

Parameters

boolean $partial

Returns

$this

comment()

comment(string  $comment) : $this

查询注释

Parameters

string $comment

注释

Returns

$this

maxTimeMS()

maxTimeMS(string  $maxTimeMS) : $this

maxTimeMS

Parameters

string $maxTimeMS

Returns

$this

collation()

collation(array  $collation) : $this

collation

Parameters

array $collation

Returns

$this

field()

field(array  $field, boolean  $except = false) : $this

设置返回字段

Parameters

array $field
boolean $except

是否排除

Returns

$this

skip()

skip(integer  $skip) : $this

设置skip

Parameters

integer $skip

Returns

$this

slaveOk()

slaveOk(boolean  $slaveOk) : $this

设置slaveOk

Parameters

boolean $slaveOk

Returns

$this

with()

with(mixed  $with) : $this

关联预载入查询

Parameters

mixed $with

Returns

$this

withCount()

withCount(string|array  $relation) : $this

关联统计

Parameters

string|array $relation

关联方法名

Returns

$this

limit()

limit(mixed  $offset, mixed  $length = null) : $this

指定查询数量

Parameters

mixed $offset

起始位置

mixed $length

查询数量

Returns

$this

page()

page(mixed  $page, mixed  $listRows = null) : $this

指定分页

Parameters

mixed $page

页数

mixed $listRows

每页数量

Returns

$this

order()

order(array|string|object  $field, string  $order = '') : $this

设置sort

Parameters

array|string|object $field
string $order

Returns

$this

tailable()

tailable(boolean  $tailable) : $this

设置tailable

Parameters

boolean $tailable

Returns

$this

writeConcern()

writeConcern(\MongoDB\Driver\WriteConcern  $writeConcern) : $this

设置writeConcern对象

Parameters

\MongoDB\Driver\WriteConcern $writeConcern

Returns

$this

getPk()

getPk() : string|array

获取当前数据表的主键

Returns

string|array

getOptions()

getOptions(string  $name = '') : mixed

获取当前的查询参数

Parameters

string $name

参数名

Returns

mixed

relation()

relation(string  $relation) : $this

设置关联查询

Parameters

string $relation

关联名称

Returns

$this

insert()

insert(mixed  $data, boolean  $replace = null, boolean  $getLastInsID = false) : \think\mongo\WriteResult

插入记录

Parameters

mixed $data

数据

boolean $replace

是否replace(目前无效)

boolean $getLastInsID

返回自增主键

Throws

\MongoDB\Driver\Exception\AuthenticationException
\MongoDB\Driver\Exception\InvalidArgumentException
\MongoDB\Driver\Exception\ConnectionException
\MongoDB\Driver\Exception\RuntimeException
\MongoDB\Driver\Exception\BulkWriteException

Returns

\think\mongo\WriteResult

insertGetId()

insertGetId(mixed  $data) : integer

插入记录并获取自增ID

Parameters

mixed $data

数据

Throws

\MongoDB\Driver\Exception\AuthenticationException
\MongoDB\Driver\Exception\InvalidArgumentException
\MongoDB\Driver\Exception\ConnectionException
\MongoDB\Driver\Exception\RuntimeException
\MongoDB\Driver\Exception\BulkWriteException

Returns

integer

insertAll()

insertAll(mixed  $dataSet) : integer

批量插入记录

Parameters

mixed $dataSet

数据集

Throws

\MongoDB\Driver\Exception\AuthenticationException
\MongoDB\Driver\Exception\InvalidArgumentException
\MongoDB\Driver\Exception\ConnectionException
\MongoDB\Driver\Exception\RuntimeException
\MongoDB\Driver\Exception\BulkWriteException

Returns

integer

update()

update(mixed  $data) : integer

更新记录

Parameters

mixed $data

数据

Throws

\think\Exception
\MongoDB\Driver\Exception\AuthenticationException
\MongoDB\Driver\Exception\InvalidArgumentException
\MongoDB\Driver\Exception\ConnectionException
\MongoDB\Driver\Exception\RuntimeException
\MongoDB\Driver\Exception\BulkWriteException

Returns

integer

delete()

delete(array  $data = null) : integer

删除记录

Parameters

array $data

表达式 true 表示强制删除

Throws

\think\Exception
\MongoDB\Driver\Exception\AuthenticationException
\MongoDB\Driver\Exception\InvalidArgumentException
\MongoDB\Driver\Exception\ConnectionException
\MongoDB\Driver\Exception\RuntimeException
\MongoDB\Driver\Exception\BulkWriteException

Returns

integer

getCursor()

getCursor() : \MongoDB\Driver\Cursor

执行查询但只返回Cursor对象

Returns

\MongoDB\Driver\Cursor

select()

select(array|string|\think\mongo\Query|\Closure  $data = null) : \think\Collection|false|\MongoDB\Driver\Cursor|string

查找记录

Parameters

array|string|\think\mongo\Query|\Closure $data

Throws

\think\db\exception\ModelNotFoundException
\think\db\exception\DataNotFoundException
\MongoDB\Driver\Exception\AuthenticationException
\MongoDB\Driver\Exception\InvalidArgumentException
\MongoDB\Driver\Exception\ConnectionException
\MongoDB\Driver\Exception\RuntimeException

Returns

\think\Collection|false|\MongoDB\Driver\Cursor|string

find()

find(array|string|\think\mongo\Query|\Closure  $data = null) : array|null|\MongoDB\Driver\Cursor|string|\think\Model

查找单条记录

Parameters

array|string|\think\mongo\Query|\Closure $data

Throws

\think\db\exception\ModelNotFoundException
\think\db\exception\DataNotFoundException
\MongoDB\Driver\Exception\AuthenticationException
\MongoDB\Driver\Exception\InvalidArgumentException
\MongoDB\Driver\Exception\ConnectionException
\MongoDB\Driver\Exception\RuntimeException

Returns

array|null|\MongoDB\Driver\Cursor|string|\think\Model

selectOrFail()

selectOrFail(array|string|\think\mongo\Query|\Closure  $data = null) : array|\PDOStatement|string|\think\Model

查找多条记录 如果不存在则抛出异常

Parameters

array|string|\think\mongo\Query|\Closure $data

Throws

\think\db\exception\ModelNotFoundException
\think\db\exception\DataNotFoundException
\MongoDB\Driver\Exception\AuthenticationException
\MongoDB\Driver\Exception\InvalidArgumentException
\MongoDB\Driver\Exception\ConnectionException
\MongoDB\Driver\Exception\RuntimeException

Returns

array|\PDOStatement|string|\think\Model

findOrFail()

findOrFail(array|string|\think\mongo\Query|\Closure  $data = null) : array|\PDOStatement|string|\think\Model

查找单条记录 如果不存在则抛出异常

Parameters

array|string|\think\mongo\Query|\Closure $data

Throws

\think\db\exception\ModelNotFoundException
\think\db\exception\DataNotFoundException
\MongoDB\Driver\Exception\AuthenticationException
\MongoDB\Driver\Exception\InvalidArgumentException
\MongoDB\Driver\Exception\ConnectionException
\MongoDB\Driver\Exception\RuntimeException

Returns

array|\PDOStatement|string|\think\Model

chunk()

chunk(integer  $count, callable  $callback, string  $column = null) : boolean

分批数据返回处理

Parameters

integer $count

每次处理的数据数量

callable $callback

处理回调方法

string $column

分批处理的字段名

Returns

boolean

getTableInfo()

getTableInfo(string  $tableName = '', string  $fetch = '') : mixed

获取数据表信息

Parameters

string $tableName

数据表名 留空自动获取

string $fetch

获取信息类型 包括 fields type pk

Returns

mixed

event()

event(string  $event, callable  $callback) : void

注册回调方法

Parameters

string $event

事件名

callable $callback

回调方法

setBuilder()

setBuilder() : void

设置当前的数据库Builder对象

lazyWrite()

lazyWrite(string  $guid, integer  $step, integer  $lazyTime) : false|integer

延时更新检查 返回false表示需要延时 否则返回实际写入的数值

Parameters

string $guid

写入标识

integer $step

写入步进值

integer $lazyTime

延时时间(s)

Returns

false|integer

parseWhereExp()

parseWhereExp(string  $logic, string|array|\Closure  $field, mixed  $op, mixed  $condition, array  $param = array()) : void

分析查询表达式

Parameters

string $logic

查询逻辑 and or xor

string|array|\Closure $field

查询字段

mixed $op

查询表达式

mixed $condition

查询条件

array $param

查询参数

options()

options(array  $options) : $this

查询参数赋值

Parameters

array $options

表达式参数

Returns

$this

parsePkWhere()

parsePkWhere(array|string  $data, mixed  $options) : void

把主键值转换为查询条件 支持复合主键

Parameters

array|string $data

主键数据

mixed $options

表达式参数

Throws

\think\Exception

cacheData()

cacheData(string  $key, mixed  $data, array  $config = array()) 

缓存数据

Parameters

string $key

缓存标识

mixed $data

缓存数据

array $config

缓存参数

getCacheKey()

getCacheKey(mixed  $value, array  $options) 

生成缓存标识

Parameters

mixed $value

缓存数据

array $options

缓存参数

throwNotFound()

throwNotFound(array  $options = array()) 

查询失败 抛出异常

Parameters

array $options

查询参数

Throws

\think\db\exception\ModelNotFoundException
\think\db\exception\DataNotFoundException

parseExpress()

parseExpress() : array

分析表达式(可用于查询或者写入操作)

Returns

array

trigger()

trigger(string  $event, mixed  $params = array()) : boolean

触发事件

Parameters

string $event

事件名

mixed $params

额外参数

Returns

boolean