Skip to content

MongoCollection

Properties

context

@LowLevelApi



abstract val context: BsonContext

Functions

aggregate

Start an aggregation pipeline.

bulkWrite

abstract suspend fun bulkWrite(
    options: BulkWriteOptions<Document>.() -> Unit, 
    filter: FilterQuery<Document>.() -> Unit, 
    operations: BulkWrite<Document>.() -> Unit
)

Performs multiple update operations in a single request.

count

abstract suspend fun count(): Long

Counts how many documents exist in the collection.

abstract suspend fun count(options: CountOptions<Document>.() -> Unit, predicate: FilterQuery<Document>.() -> Unit): Long

Counts how many documents match predicate in the collection.

countEstimated

abstract suspend fun countEstimated(): Long

Counts all documents in the collection.

deleteMany

abstract suspend fun deleteMany(options: DeleteManyOptions<Document>.() -> Unit, filter: FilterQuery<Document>.() -> Unit)

Deletes all documents that match filter.

deleteOne

abstract suspend fun deleteOne(options: DeleteOneOptions<Document>.() -> Unit, filter: FilterQuery<Document>.() -> Unit)

Deletes the first document found that matches filter.

drop

abstract suspend fun drop(options: DropOptions<Document>.() -> Unit)

Removes an entire collection from the database.

exists

open suspend fun exists(options: CountOptions<Document>.() -> Unit, predicate: FilterQuery<Document>.() -> Unit): Boolean

Tests if there exists a document that matches predicate in the collection.

filter

Returns a filtered collection that only contains the elements that match filter.

This function creates a logical view of the collection: by itself, this function does nothing, and MongoDB is never aware of the existence of this logical view. However, operations invoked on the returned collection will only affect elements from the original that match the filter.

Unlike actual MongoDB views, which are read-only, collections returned by this function can also be used for write operations.

Example

A typical usage of this function is to reuse filters for multiple operations. For example, if you have a concept of logical deletion, this function can be used to hide deleted values.

class Order(
    val id: String,
    val date: Instant,
    val deleted: Boolean,
)

val allOrders = database.getCollection<Order>("orders").asKtMongo()
val activeOrders = allOrders.filter { Order::deleted ne true }

allOrders.find()    // Returns all orders, deleted or not
activeOrders.find() // Only returns orders that are not logically deleted

find

abstract fun find(): MongoIterable<Document>

Finds all documents in this collection.

abstract fun find(options: FindOptions<Document>.() -> Unit, filter: FilterQuery<Document>.() -> Unit): MongoIterable<Document>

Finds all documents in this collection that satisfy filter.

findOne

open suspend fun findOne(options: FindOptions<Document>.() -> Unit, filter: FilterQuery<Document>.() -> Unit): Document?

Finds a document in this collection that satisfies filter.

findOneAndUpdate

abstract suspend fun findOneAndUpdate(
    options: UpdateOptions<Document>.() -> Unit, 
    filter: FilterQuery<Document>.() -> Unit, 
    update: UpdateQuery<Document>.() -> Unit
): Document?

Updates one element that matches filter according to update and returns it, atomically.

insertMany

open suspend fun insertMany(vararg documents: Document, options: InsertManyOptions<Document>.() -> Unit)

Inserts multiple documents in a single operation.

abstract suspend fun insertMany(documents: Iterable<Document>, options: InsertManyOptions<Document>.() -> Unit)

Inserts multiple documents in a single operation.

insertOne

abstract suspend fun insertOne(document: Document, options: InsertOneOptions<Document>.() -> Unit)

Inserts a document.

newId

abstract fun newId(): ObjectId

replaceOne

abstract suspend fun replaceOne(
    options: ReplaceOptions<Document>.() -> Unit, 
    filter: FilterQuery<Document>.() -> Unit, 
    document: Document
)

Replaces a document that matches filter by document.

repsertOne

abstract suspend fun repsertOne(
    options: ReplaceOptions<Document>.() -> Unit, 
    filter: FilterQuery<Document>.() -> Unit, 
    document: Document
)

Replaces a document that matches filter by document.

updateMany

Updates all documents that match filter according to update.

updateManyWithPipeline

Updates all documents that match filter according to the update pipeline.

updateOne

Updates a single document that matches filter according to update.

updateOneWithPipeline

Updates a single document that matches filter according to the update pipeline.

upsertOne

Updates a single document that matches filter according to update.

upsertOneWithPipeline

Updates a single document that matches filter according to the update pipeline.