Skip to content

MongoDB Operators API

MongoDB query and aggregation operators used to filter, transform and group documents.

1 class · 5 methods

Operators

5 methods

Common query and pipeline operators.

{ $match: <query> }

Aggregation stage that filters documents, behaving like a collection find query.

Parameters

NameTypeDescription
queryobjectSelector document.

Returns

Stage

Example

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

Aggregation stage that groups documents by _id expression and applies accumulators per field.

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> }

Aggregation stage that passes only specified fields (with optional computed values) downstream.

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 } }

Performs a left outer join to another collection in the same database.

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 } }