AggregationPipeline¶
interface AggregationPipeline<Document : Any> : Pipeline<Document> , HasCount<Document> , HasGroup<Document> , HasLimit<Document> , HasLookup<Document> , HasLookupPipelineCompatibility<Document> , HasMatch<Document> , HasProject<Document> , HasSample<Document> , HasSet<Document> , HasSkip<Document> , HasSort<Document> , HasUnionWith<Document> , HasUnionWithCompatibility<Document> , HasUnset<Document> , HasUnwind<Document>
An aggregation pipeline.
Aggregation pipelines read data from one or more collections and transform it in a manner of ways. Finally, the data can be sent to the server, or written to another collection.
Example¶
invoices.aggregate()
.match { Invoice::isDraft eq false }
.set {
Invoice::anomaly set (of(Invoice::modificationDate) lt of(Invoice::creationDate))
}
.sort { ascending(Invoice::creationDate) }
.toList()
External resources¶
Properties¶
context¶
@LowLevelApi
abstract val context: BsonContext
The context used to generate this pipeline.
Can be accessed within children expressions.
Functions¶
countTo¶
Counts how many elements exist in the pipeline and outputs a single document containing a single field containing the count.
Counts how many elements exist in the pipeline and outputs a single document containing a single field containing the count.
embedInLookup¶
@LowLevelApi
abstract fun embedInLookup(writer: BsonFieldWriter)
Writes this pipeline's contribution into a $lookup stage.
embedInUnionWith¶
@LowLevelApi
abstract fun embedInUnionWith(writer: BsonFieldWriter)
Writes this pipeline into a $unionWith stage.
group¶
Combines many documents into few documents.
limit¶
Limits the number of elements passed to the next stage to amount.
Limits the number of elements passed to the next stage to amount.
lookup¶
open fun <ForeignDocument : Any> lookup(block: LookupStageOperators<Document, ForeignDocument>.() -> Unit): Pipeline<Document>
Performs an equality match join between this collection and another collection.
match¶
Filters documents based on a specified filter.
matchExpr¶
Filters documents based on a specific filter, written using aggregation syntax.
project¶
Specifies a list of fields which should be kept in the document.
sample¶
set¶
Adds new fields to documents, or overwrites existing fields.
skip¶
Skips over the specified amount of documents that pass into the stage, and passes the remaining documents to the next stage.
Skips over the specified amount of documents that pass into the stage, and passes the remaining documents to the next stage.
sort¶
Specifies in which order elements should be sorted.
toString¶
unionWith¶
open fun unionWith(other: HasUnionWithCompatibility<Document>): Pipeline<Document>
Combines two aggregations into a single result set.
unsafeCast¶
abstract fun <New : Any> unsafeCast(): Pipeline<New>
Changes the type of the returned document, with no type-safety.
Every subsequent step in the pipeline will think the returned document is of type New. It is your responsibility to ensure that this corresponds to the reality; the KtMongo DSL makes no verifications.
If you want to edit the representation of the documents in the pipeline, prefer using HasProject.project.
See also
withStage: Add a new stage to this pipeline.
unset¶
Removes fields from documents.
unwind¶
Unwinds an array.
withStage¶
@DangerousMongoApi
@LowLevelApi
abstract fun withStage(stage: BsonNode): Pipeline<Output>
Creates a new pipeline that expands on the current one by adding stage.
This method is analogous to CompoundBsonNode.accept, with the main difference that the latter mutates the current expression, whereas this method returns a new pipeline on which the stage is applied (because pipelines are immutable).
End-users should not need to call this function. All implemented stages provide an extension function on the Pipeline type. This function is provided for cases in which you need a stage that is not yet provided by the library. If that is your situation, start by reading AbstractBsonNode and AbstractCompoundBsonNode. If you want to proceed and implement your own stage, consider getting in touch with the maintainers of the library so it can be shared to all users.
The provided stage must validate the entire contract of BsonNode. Additionally, it should always emit the name of the stage first. For example, this is a valid stage:
but this isn't:
because it doesn't start with a stage name.
Similarly, this isn't a valid stage, because it declares two different stage names:
See also
unsafeCast: Change the output type of this pipeline.
writeTo¶
@LowLevelApi
abstract fun writeTo(writer: BsonValueWriter)
Writes the entire pipeline into writer.
This function is similar to BsonNode.writeTo, with the difference that expressions generate documents, and pipelines generate arrays.
Using this method will thus write an array containing the different stages.