Skip to content

MySQL Operators API

MySQL SELECT 语句子句,用于从一个或多个表查询和组合行。

1 class · 5 methods

SELECT

5 methods

SELECT 语句中可用的子句。

{ $match: <query> }

从表中读取行,可选按布尔条件过滤。

Parameters

NameTypeDescription
queryobjectSelector document.

Returns

Stage

Example

mongodb
{ $match: { status: 'active', age: { $gte: 18 } } }
{ $group: { _id: <expr>, <field>: { <accumulator>: <expr> } } }

基于连接谓词组合两个表的行;支持 INNER、LEFT、RIGHT、FULL。

Parameters

NameTypeDescription
_idexpressionGroup key expression; null groups all documents.
accumulatorstring$sum, $avg, $max, $min, $push, $addToSet, etc.

Returns

Stage

Example

mongodb
{ $group: { _id: '$dept', avgSalary: { $avg: '$salary' } } }
{ $project: <specification> }

按列出的列分组行并应用聚合函数,用 HAVING 过滤分组。

Parameters

NameTypeDescription
specificationobjectField inclusion (1), exclusion (0) or expression.

Returns

Stage

Example

mongodb
{ $project: { name: 1, age: 1, _id: 0 } }
{ $lookup: { from, localField, foreignField, as } }

按给定列排序结果集并限制返回行数。

Parameters

NameTypeDescription
fromstringForeign collection name.
localFieldstringField from the input documents.
foreignFieldstringField from the foreign collection documents.
asstringOutput array field name.

Returns

Stage

Example

mongodb
{ $lookup: { from: 'orders', localField: '_id', foreignField: 'userId', as: 'orders' } }
{ $sort: { <field>: 1 | -1 } }

Aggregation stage that sorts documents by the given fields; 1 ascending, -1 descending.

Parameters

NameTypeDescription
fieldstringField path to sort by.

Returns

Stage

Example

mongodb
{ $sort: { createdAt: -1 } }