Skip to content

MongoAggregationPipeline

A multi-stage aggregation pipeline that transforms documents from a MongoDB collection.

Pipelines are immutable. Each stage method returns a new pipeline with the stage appended.

To obtain a pipeline, use MongoCollection.aggregate.

Example

class User(
    val name: String,
    val age: Int,
)

users.aggregate()
    .match { User::age gt 18 }
    .sort { ascending(User::name) }
    .toList()

External resources

Properties

context

Functions

asFlow

Streams the results into a Flow.

The flow is lazy: new elements are streamed in when the consumer requests them.

MongoDB cursors are batched: a batch is queried, processed, then another batch is requested, etc. The batch size can be configured in the operation creating this iterable.

If you intend to query a large number of batches and perform complex operations on them, we recommend using buffer with a low capacity, to reduce latency between two batches.

See also

asIterable

Access the data of this pipeline as a MongoIterable.

The methods of MongoIterable are available directly on this type as extension methods, there is no need to convert to a MongoIterable yourself.

If type doesn't match Document, the behavior is unspecified.

countTo

abstract override fun <Out : Any> countTo(field: Field<Out, Number>): MongoAggregationPipeline<Out>
abstract override fun <Out : Any> countTo(field: KProperty1<Out, Number>): MongoAggregationPipeline<Out>

embedInLookup

@LowLevelApi
abstract fun embedInLookup(writer: BsonFieldWriter)

embedInUnionWith

first

Returns the first document found by this query, or throws an exception.

See also

  • firstOrNull: Return null instead of throwing an exception.

Throws

NoSuchElementException

If this query returned no results.

firstOrNull

Returns the first document found by this query, or returns null.

See also

  • first: Throw an exception instead of returning null.

forEach

inline suspend fun <Document : Any> MongoAggregationPipeline<Document>.forEach(noinline action: suspend (Document) -> Unit)

Executes action for each document returned by this query.

This method streams all returned documents into the action function. The entire response set is not loaded at once into memory.

MongoDB cursors are batched: a batch is queried, processed, then another batch is requested, etc. The batch size can be configured in the operation creating this iterable.

If the operation contains a sort without an index, MongoDB will load all results into memory. The driver will still stream the results.

See also

group

abstract override fun <Out : Any> group(block: AccumulationOperators<Document, Out>.() -> Unit): MongoAggregationPipeline<Out>

limit

abstract override fun limit(amount: Long): MongoAggregationPipeline<Document>
abstract override fun limit(amount: Int): MongoAggregationPipeline<Document>

lookup

match

abstract override fun match(filter: FilterQuery<Document>.() -> Unit): MongoAggregationPipeline<Document>

matchExpr

project

reinterpret

sample

abstract override fun sample(size: Int): MongoAggregationPipeline<Document>

set

abstract override fun set(block: SetStageOperators<Document>.() -> Unit): MongoAggregationPipeline<Document>

skip

abstract override fun skip(amount: Long): MongoAggregationPipeline<Document>
abstract override fun skip(amount: Int): MongoAggregationPipeline<Document>

sort

abstract override fun sort(block: SortOptionDsl<Document>.() -> Unit): MongoAggregationPipeline<Document>

toList

Reads the entirety of this iterable into a List.

Since lists are in-memory, this method loads the entirety of the results into memory.

See also

toSet

Reads the entirety of this iterable into a Set.

Since sets are in-memory, this method loads the entirety of the results into memory.

See also

toString

abstract override fun toString(): String

unionWith

unset

abstract override fun unset(block: UnsetStageOperators<Document>.() -> Unit): MongoAggregationPipeline<Document>

withStage

writeTo

@LowLevelApi
abstract fun writeTo(writer: BsonValueWriter)