Skip to content

HasMatch

Pipeline implementing the $match stage.

Inheritors

Properties

context

The context used to generate this pipeline.

Functions

match

open fun match(filter: FilterQuery<Document>.() -> Unit): Pipeline<Document>

Filters documents based on a specified filter.

Matched documents are passed to the next pipeline stage.

Pipeline optimization

Place the match call as early in the pipeline as possible. Because match limits the total number of elements being processed, earlier match operations minimize the amount of processing down the pipe.

If you place a match at the very beginning of a pipeline, the query can take advantage of indexes.

External resources

matchExpr

Filters documents based on a specific filter, written using aggregation syntax.

Matched documents are passed to the next pipeline stage.

This method is a helper to combine the match stage with the expr operator.

Example

Find all incoherent users: users modified before they were created.

class User(
    val _id: ObjectId,
    val creationDate: Instant,
    val modificationDate: Instant,
)

val incoherent = users.aggregate()
    .matchExpr { User::modificationDate lt User::creationDate }
    .toList()

The above query is syntax sugar for this identical query:

val incoherent = users.aggregate()
    .match {
        expr { User::modificationDate lt User::creationDate }
    }
    .toList()

See also

  • match: The $match stage.

  • FilterQuery.expr: The $expr operator, allowing to use aggregation syntax in a filter.

toString

abstract override fun toString(): String

JSON representation of this pipeline.

unsafeCast

abstract fun <New : Any> unsafeCast(): Pipeline<New>

Changes the type of the returned document, with no type-safety.

withStage

Creates a new pipeline that expands on the current one by adding stage.

writeTo

@LowLevelApi
abstract fun writeTo(writer: BsonValueWriter)

Writes the entire pipeline into writer.