MongoCollection¶
interface MongoCollection<Document : Any> : ObjectIdGenerator, FindOperations<Document> , CountOperations<Document> , UpdateOperations<Document> , DeleteOperations<Document> , CollectionOperations<Document> , InsertOperations<Document> , AggregationOperations<Document> , UpdatePipelineOperations<Document>
Methods to interact with a MongoDB collection.
Operations¶
External resources¶
Inheritors¶
Properties¶
context¶
@LowLevelApi
abstract val context: BsonContext
Functions¶
aggregate¶
abstract fun aggregate(): MongoAggregationPipeline<Document>
Start an aggregation pipeline.
bulkWrite¶
abstract fun bulkWrite(
options: BulkWriteOptions<Document>.() -> Unit,
filter: FilterQuery<Document>.() -> Unit,
operations: BulkWrite<Document>.() -> Unit
)
Performs multiple update operations in a single request.
count¶
Counts how many documents exist in the collection.
abstract fun count(options: CountOptions<Document>.() -> Unit, predicate: FilterQuery<Document>.() -> Unit): Long
Counts how many documents match predicate in the collection.
countEstimated¶
abstract fun countEstimated(): Long
Counts all documents in the collection.
deleteMany¶
abstract fun deleteMany(options: DeleteManyOptions<Document>.() -> Unit, filter: FilterQuery<Document>.() -> Unit)
Deletes all documents that match filter.
deleteOne¶
abstract fun deleteOne(options: DeleteOneOptions<Document>.() -> Unit, filter: FilterQuery<Document>.() -> Unit)
Deletes the first document found that matches filter.
drop¶
abstract fun drop(options: DropOptions<Document>.() -> Unit)
Removes an entire collection from the database.
exists¶
open fun exists(options: CountOptions<Document>.() -> Unit, predicate: FilterQuery<Document>.() -> Unit): Boolean
Tests if there exists a document that matches predicate in the collection.
filter¶
fun <Document : Any> MongoCollection<Document>.filter(filter: FilterQuery<Document>.() -> Unit): MongoCollection<Document>
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 fun findOne(options: FindOptions<Document>.() -> Unit, filter: FilterQuery<Document>.() -> Unit): Document?
Finds a document in this collection that satisfies filter.
findOneAndUpdate¶
abstract fun findOneAndUpdate(
options: UpdateOptions<Document>.() -> Unit,
filter: FilterQuery<Document>.() -> Unit,
update: UpdateQuery<Document>.() -> Unit
): Document?
insertMany¶
open fun insertMany(vararg documents: Document, options: InsertManyOptions<Document>.() -> Unit)
Inserts multiple documents in a single operation.
abstract fun insertMany(documents: Iterable<Document>, options: InsertManyOptions<Document>.() -> Unit)
Inserts multiple documents in a single operation.
insertOne¶
abstract fun insertOne(document: Document, options: InsertOneOptions<Document>.() -> Unit)
Inserts a document.
newId¶
replaceOne¶
abstract fun replaceOne(
options: ReplaceOptions<Document>.() -> Unit,
filter: FilterQuery<Document>.() -> Unit,
document: Document
)
repsertOne¶
abstract fun repsertOne(
options: ReplaceOptions<Document>.() -> Unit,
filter: FilterQuery<Document>.() -> Unit,
document: Document
)
updateMany¶
@IgnorableReturnValue
abstract fun updateMany(
options: UpdateOptions<Document>.() -> Unit,
filter: FilterQuery<Document>.() -> Unit,
update: UpdateQuery<Document>.() -> Unit
): UpdateOperations.UpdateResult
updateManyWithPipeline¶
@IgnorableReturnValue
abstract fun updateManyWithPipeline(
options: UpdateOptions<Document>.() -> Unit,
filter: FilterQuery<Document>.() -> Unit,
update: UpdateWithPipelineQuery<Document>.() -> Unit
): UpdateOperations.UpdateResult
updateOne¶
@IgnorableReturnValue
abstract fun updateOne(
options: UpdateOptions<Document>.() -> Unit,
filter: FilterQuery<Document>.() -> Unit,
update: UpdateQuery<Document>.() -> Unit
): UpdateOperations.UpdateResult
updateOneWithPipeline¶
@IgnorableReturnValue
abstract fun updateOneWithPipeline(
options: UpdateOptions<Document>.() -> Unit,
filter: FilterQuery<Document>.() -> Unit,
update: UpdateWithPipelineQuery<Document>.() -> Unit
): UpdateOperations.UpdateResult
upsertOne¶
@IgnorableReturnValue
abstract fun upsertOne(
options: UpdateOptions<Document>.() -> Unit,
filter: FilterQuery<Document>.() -> Unit,
update: UpsertQuery<Document>.() -> Unit
): UpdateOperations.UpsertResult
upsertOneWithPipeline¶
@IgnorableReturnValue
abstract fun upsertOneWithPipeline(
options: UpdateOptions<Document>.() -> Unit,
filter: FilterQuery<Document>.() -> Unit,
update: UpdateWithPipelineQuery<Document>.() -> Unit
): UpdateOperations.UpsertResult