kmongo / org.litote.kmongo.coroutine / CoroutineCollection

CoroutineCollection

class CoroutineCollection<T : Any> (source)

A wrapper around MongoCollection. Provides coroutine methods for Reactive Streams driver.

Constructors

<init>

A wrapper around MongoCollection. Provides coroutine methods for Reactive Streams driver.

CoroutineCollection(collection: MongoCollection<T>)

Properties

codecRegistry

Get the codec registry for the MongoCollection.

val codecRegistry: CodecRegistry

collection

val collection: MongoCollection<T>

documentClass

Get the class of documents stored in this collection.

val documentClass: Class<T>

namespace

Gets the namespace of this collection.

val namespace: MongoNamespace

readConcern

Get the read concern for the MongoCollection.

val readConcern: ReadConcern

readPreference

Get the read preference for the MongoCollection.

val readPreference: ReadPreference

writeConcern

Get the write concern for the MongoCollection.

val writeConcern: WriteConcern

Functions

aggregate

Aggregates documents according to the specified aggregation pipeline.

fun <T : Any> aggregate(pipeline: List<Bson>): CoroutineAggregatePublisher<T>
fun <T : Any> aggregate(clientSession: ClientSession, pipeline: List<Bson>): CoroutineAggregatePublisher<T>

bulkWrite

Executes a mix of inserts, updates, replaces, and deletes.

suspend fun bulkWrite(requests: List<WriteModel<out T>>, options: BulkWriteOptions = BulkWriteOptions()): BulkWriteResult
suspend fun bulkWrite(clientSession: ClientSession, requests: List<WriteModel<out T>>, options: BulkWriteOptions = BulkWriteOptions()): BulkWriteResult
suspend fun bulkWrite(vararg requests: WriteModel<T>, options: BulkWriteOptions = BulkWriteOptions()): BulkWriteResult

countDocuments

Counts the number of documents in the collection according to the given options.

suspend fun countDocuments(filter: Bson = EMPTY_BSON, options: CountOptions = CountOptions()): Long
suspend fun countDocuments(clientSession: ClientSession, filter: Bson = EMPTY_BSON, options: CountOptions = CountOptions()): Long
suspend fun countDocuments(filter: String, options: CountOptions = CountOptions()): Long
suspend fun countDocuments(clientSession: ClientSession, filter: String, options: CountOptions = CountOptions()): Long

createIndex

Creates an index.

suspend fun createIndex(key: Bson, options: IndexOptions = IndexOptions()): String
suspend fun createIndex(clientSession: ClientSession, key: Bson, options: IndexOptions = IndexOptions()): String

Creates an index. If successful, the callback will be executed with the name of the created index as the result.

suspend fun createIndex(key: String, options: IndexOptions = IndexOptions()): String

createIndexes

Create multiple indexes.

suspend fun createIndexes(indexes: List<IndexModel>, createIndexOptions: CreateIndexOptions = CreateIndexOptions()): List<String>
suspend fun createIndexes(clientSession: ClientSession, indexes: List<IndexModel>, createIndexOptions: CreateIndexOptions = CreateIndexOptions()): List<String>

deleteMany

Removes all documents from the collection that match the given query filter. If no documents match, the collection is not modified.

suspend fun deleteMany(filter: Bson, options: DeleteOptions = DeleteOptions()): DeleteResult
suspend fun deleteMany(clientSession: ClientSession, filter: Bson, options: DeleteOptions = DeleteOptions()): DeleteResult
suspend fun deleteMany(filter: String = EMPTY_JSON, options: DeleteOptions = DeleteOptions()): DeleteResult
suspend fun deleteMany(clientSession: ClientSession, filter: String = EMPTY_JSON, options: DeleteOptions = DeleteOptions()): DeleteResult
suspend fun deleteMany(vararg filters: Bson?, options: DeleteOptions = DeleteOptions()): DeleteResult
suspend fun deleteMany(clientSession: ClientSession, vararg filters: Bson?, options: DeleteOptions = DeleteOptions()): DeleteResult

deleteOne

Removes at most one document from the collection that matches the given filter. If no documents match, the collection is not modified.

suspend fun deleteOne(filter: Bson, options: DeleteOptions = DeleteOptions()): DeleteResult
suspend fun deleteOne(clientSession: ClientSession, filter: Bson, options: DeleteOptions = DeleteOptions()): DeleteResult
suspend fun deleteOne(filter: String, deleteOptions: DeleteOptions = DeleteOptions()): DeleteResult
suspend fun deleteOne(clientSession: ClientSession, filter: String, deleteOptions: DeleteOptions = DeleteOptions()): DeleteResult
suspend fun deleteOne(vararg filters: Bson?, deleteOptions: DeleteOptions = DeleteOptions()): DeleteResult
suspend fun deleteOne(clientSession: ClientSession, vararg filters: Bson?, deleteOptions: DeleteOptions = DeleteOptions()): DeleteResult

deleteOneById

Removes at most one document from the id parameter. If no documents match, the collection is not modified.

suspend fun deleteOneById(id: Any): DeleteResult
suspend fun deleteOneById(clientSession: ClientSession, id: Any): DeleteResult

distinct

Gets the distinct values of the specified field name.

fun <T : Any> distinct(fieldName: String, filter: Bson = EMPTY_BSON): CoroutineDistinctPublisher<T>
fun <T : Any> distinct(clientSession: ClientSession, fieldName: String, filter: Bson): CoroutineDistinctPublisher<T>
fun <Type : Any> distinct(fieldName: String, filter: String): CoroutineDistinctPublisher<Type>

Gets the distinct values of the specified field.

fun <Type : Any> distinct(field: KProperty1<T, Type>, filter: Bson = EMPTY_BSON): CoroutineDistinctPublisher<Type>

drop

Drops this collection from the Database.

suspend fun drop(): <ERROR CLASS>
suspend fun drop(clientSession: ClientSession): <ERROR CLASS>

dropIndex

Drops the given index.

suspend fun dropIndex(indexName: String, dropIndexOptions: DropIndexOptions = DropIndexOptions()): <ERROR CLASS>
suspend fun dropIndex(clientSession: ClientSession, indexName: String, dropIndexOptions: DropIndexOptions = DropIndexOptions()): <ERROR CLASS>

Drops the index given the keys used to create it.

suspend fun dropIndex(keys: Bson, dropIndexOptions: DropIndexOptions = DropIndexOptions()): <ERROR CLASS>
suspend fun dropIndex(clientSession: ClientSession, keys: Bson, dropIndexOptions: DropIndexOptions = DropIndexOptions()): <ERROR CLASS>

dropIndexes

Drop all the indexes on this collection, except for the default on _id.

suspend fun dropIndexes(dropIndexOptions: DropIndexOptions = DropIndexOptions()): <ERROR CLASS>
suspend fun dropIndexes(clientSession: ClientSession, dropIndexOptions: DropIndexOptions = DropIndexOptions()): <ERROR CLASS>

ensureIndex

Create an index with the given keys and options. If the creation of the index is not doable because an index with the same keys but with different IndexOptions already exists, then drop the existing index and create a new one.

suspend fun ensureIndex(keys: String, indexOptions: IndexOptions = IndexOptions()): String?
suspend fun ensureIndex(keys: Bson, indexOptions: IndexOptions = IndexOptions()): String?
suspend fun ensureIndex(vararg properties: KProperty<*>, indexOptions: IndexOptions = IndexOptions()): String?

ensureUniqueIndex

Create an IndexOptions.unique index with the given keys and options. If the creation of the index is not doable because an index with the same keys but with different IndexOptions already exists, then drop the existing index and create a new one.

suspend fun ensureUniqueIndex(vararg properties: KProperty<*>, indexOptions: IndexOptions = IndexOptions()): String?

estimatedDocumentCount

Gets an estimate of the count of documents in a collection using collection metadata.

suspend fun estimatedDocumentCount(options: EstimatedDocumentCountOptions = EstimatedDocumentCountOptions()): Long

find

Finds all documents in the collection.

fun find(filter: Bson = EMPTY_BSON): CoroutineFindPublisher<T>
fun find(clientSession: ClientSession, filter: Bson = EMPTY_BSON): CoroutineFindPublisher<T>
fun find(vararg filters: Bson?): CoroutineFindPublisher<T>

Finds all documents that match the filter in the collection.

fun find(filter: String): CoroutineFindPublisher<T>

findAndCast

Finds all documents in the collection.

fun <T : Any> findAndCast(filter: Bson = EMPTY_BSON): CoroutineFindPublisher<T>
fun <T : Any> findAndCast(clientSession: ClientSession, filter: Bson): CoroutineFindPublisher<T>

findOne

Finds the first document that match the filter in the collection.

suspend fun findOne(filter: String = KMongoUtil.EMPTY_JSON): T?
suspend fun findOne(clientSession: ClientSession, filter: String = KMongoUtil.EMPTY_JSON): T?
suspend fun findOne(filter: Bson): T?
suspend fun findOne(clientSession: ClientSession, filter: Bson = EMPTY_BSON): T?

Finds the first document that match the filters in the collection.

suspend fun findOne(vararg filters: Bson?): T?

findOneAndDelete

Atomically find a document and remove it.

suspend fun findOneAndDelete(filter: Bson, options: FindOneAndDeleteOptions = FindOneAndDeleteOptions()): T?
suspend fun findOneAndDelete(clientSession: ClientSession, filter: Bson, options: FindOneAndDeleteOptions = FindOneAndDeleteOptions()): T?
suspend fun findOneAndDelete(filter: String, options: FindOneAndDeleteOptions = FindOneAndDeleteOptions()): T?
suspend fun findOneAndDelete(clientSession: ClientSession, filter: String, options: FindOneAndDeleteOptions = FindOneAndDeleteOptions()): T?

findOneAndReplace

Atomically find a document and replace it.

suspend fun findOneAndReplace(filter: Bson, replacement: T, options: FindOneAndReplaceOptions = FindOneAndReplaceOptions()): T?
suspend fun findOneAndReplace(clientSession: ClientSession, filter: Bson, replacement: T, options: FindOneAndReplaceOptions = FindOneAndReplaceOptions()): T?
suspend fun findOneAndReplace(filter: String, replacement: T, options: FindOneAndReplaceOptions = FindOneAndReplaceOptions()): T?
suspend fun findOneAndReplace(clientSession: ClientSession, filter: String, replacement: T, options: FindOneAndReplaceOptions = FindOneAndReplaceOptions()): T?

findOneAndUpdate

Atomically find a document and update it.

suspend fun findOneAndUpdate(filter: Bson, update: Bson, options: FindOneAndUpdateOptions = FindOneAndUpdateOptions()): T?
suspend fun findOneAndUpdate(clientSession: ClientSession, filter: Bson, update: Bson, options: FindOneAndUpdateOptions = FindOneAndUpdateOptions()): T?
suspend fun findOneAndUpdate(filter: String, update: String, options: FindOneAndUpdateOptions = FindOneAndUpdateOptions()): T?
suspend fun findOneAndUpdate(clientSession: ClientSession, filter: String, update: String, options: FindOneAndUpdateOptions = FindOneAndUpdateOptions()): T?

findOneById

Finds the document that match the id parameter.

suspend fun findOneById(id: Any): T?
suspend fun findOneById(id: Any, clientSession: ClientSession): T?

insertMany

Inserts a batch of documents. The preferred way to perform bulk inserts is to use the BulkWrite API. However, when talking with a server < 2.6, using this method will be faster due to constraints in the bulk API related to error handling.

suspend fun insertMany(documents: List<T>, options: InsertManyOptions = InsertManyOptions()): InsertManyResult
suspend fun insertMany(clientSession: ClientSession, documents: List<T>, options: InsertManyOptions = InsertManyOptions()): InsertManyResult

insertOne

Inserts the provided document. If the document is missing an identifier, the driver should generate one.

suspend fun insertOne(document: T, options: InsertOneOptions = InsertOneOptions()): InsertOneResult
suspend fun insertOne(clientSession: ClientSession, document: T, options: InsertOneOptions = InsertOneOptions()): InsertOneResult

listIndexes

Get all the indexes in this collection.

fun <T : Any> listIndexes(): CoroutineListIndexesPublisher<T>
fun <T : Any> listIndexes(clientSession: ClientSession): CoroutineListIndexesPublisher<T>

mapReduce

Aggregates documents according to the specified map-reduce function.

fun <T : Any> mapReduce(mapFunction: String, reduceFunction: String): CoroutineMapReducePublisher<T>
fun <T : Any> mapReduce(clientSession: ClientSession, mapFunction: String, reduceFunction: String): CoroutineMapReducePublisher<T>

renameCollection

Rename the collection with oldCollectionName to the newCollectionName.

suspend fun renameCollection(newCollectionNamespace: MongoNamespace, options: RenameCollectionOptions = RenameCollectionOptions()): <ERROR CLASS>
suspend fun renameCollection(clientSession: ClientSession, newCollectionNamespace: MongoNamespace, options: RenameCollectionOptions = RenameCollectionOptions()): <ERROR CLASS>

replaceOne

Replace a document in the collection according to the specified arguments.

suspend fun replaceOne(filter: Bson, replacement: T, options: ReplaceOptions = ReplaceOptions()): UpdateResult
suspend fun replaceOne(clientSession: ClientSession, filter: Bson, replacement: T, options: ReplaceOptions = ReplaceOptions()): UpdateResult
suspend fun replaceOne(filter: String, replacement: T, options: ReplaceOptions = ReplaceOptions()): UpdateResult

replaceOneById

Replace a document in the collection according to the specified arguments.

suspend fun <T : Any> replaceOneById(id: Any, replacement: T, options: ReplaceOptions = ReplaceOptions()): UpdateResult
suspend fun replaceOneById(clientSession: ClientSession, id: Any, replacement: T, options: ReplaceOptions = ReplaceOptions()): UpdateResult

replaceOneWithoutId

Replace a document in the collection according to the specified arguments. The id of the provided document is not used, in order to avoid updated id error. You may have to use UpdateResult.getUpsertedId in order to retrieve the generated id.

suspend fun <T : Any> replaceOneWithoutId(filter: Bson, replacement: T, options: ReplaceOptions = ReplaceOptions()): UpdateResult
suspend fun replaceOneWithoutId(clientSession: ClientSession, filter: Bson, replacement: T, options: ReplaceOptions = ReplaceOptions()): UpdateResult

save

Save the document. If the document has no id field, or if the document has a null id value, insert the document. Otherwise, call replaceOneById with upsert true.

suspend fun save(document: T): UpdateResult?
suspend fun save(clientSession: ClientSession, document: T): UpdateResult?

updateMany

Update all documents in the collection according to the specified arguments.

suspend fun updateMany(filter: Bson, update: Bson, options: UpdateOptions = UpdateOptions()): UpdateResult
suspend fun updateMany(clientSession: ClientSession, filter: Bson, update: Bson, options: UpdateOptions = UpdateOptions()): UpdateResult
suspend fun updateMany(filter: String, update: String, updateOptions: UpdateOptions = UpdateOptions()): UpdateResult
suspend fun updateMany(clientSession: ClientSession, filter: String, update: String, updateOptions: UpdateOptions = UpdateOptions()): UpdateResult
suspend fun updateMany(filter: Bson, vararg updates: SetTo<*>, updateOptions: UpdateOptions = UpdateOptions()): UpdateResult
suspend fun updateMany(clientSession: ClientSession, filter: Bson, vararg updates: SetTo<*>, updateOptions: UpdateOptions = UpdateOptions()): UpdateResult

updateOne

Update a single document in the collection according to the specified arguments.

suspend fun updateOne(filter: Bson, update: Bson, options: UpdateOptions = UpdateOptions()): UpdateResult
suspend fun updateOne(clientSession: ClientSession, filter: Bson, update: Bson, options: UpdateOptions = UpdateOptions()): UpdateResult
suspend fun updateOne(filter: String, update: String, options: UpdateOptions = UpdateOptions()): UpdateResult
suspend fun updateOne(clientSession: ClientSession, filter: String, update: String, options: UpdateOptions = UpdateOptions()): UpdateResult
suspend fun updateOne(filter: String, update: Any, options: UpdateOptions = UpdateOptions(), updateOnlyNotNullProperties: Boolean = UpdateConfiguration.updateOnlyNotNullProperties): UpdateResult
suspend fun updateOne(clientSession: ClientSession, filter: String, update: Any, options: UpdateOptions = UpdateOptions(), updateOnlyNotNullProperties: Boolean = UpdateConfiguration.updateOnlyNotNullProperties): UpdateResult
suspend fun updateOne(filter: Bson, target: T, options: UpdateOptions = UpdateOptions(), updateOnlyNotNullProperties: Boolean = UpdateConfiguration.updateOnlyNotNullProperties): UpdateResult
suspend fun updateOne(clientSession: ClientSession, filter: Bson, target: T, options: UpdateOptions = UpdateOptions(), updateOnlyNotNullProperties: Boolean = UpdateConfiguration.updateOnlyNotNullProperties): UpdateResult

updateOneById

Update a single document in the collection according to the specified arguments.

suspend fun updateOneById(id: Any, update: Any, options: UpdateOptions = UpdateOptions(), updateOnlyNotNullProperties: Boolean = UpdateConfiguration.updateOnlyNotNullProperties): UpdateResult
suspend fun updateOneById(clientSession: ClientSession, id: Any, update: Any, options: UpdateOptions = UpdateOptions(), updateOnlyNotNullProperties: Boolean = UpdateConfiguration.updateOnlyNotNullProperties): UpdateResult

watch

Creates a change stream for this collection.

fun <T : Any> watch(pipeline: List<Bson> = emptyList()): CoroutineChangeStreamPublisher<T>
fun <T : Any> watch(clientSession: ClientSession, pipeline: List<Bson>): CoroutineChangeStreamPublisher<T>

withCodecRegistry

Create a new MongoCollection instance with a different codec registry.

fun withCodecRegistry(codecRegistry: CodecRegistry): CoroutineCollection<T>

withDocumentClass

Create a new MongoCollection instance with a different default class to cast any documents returned from the database into..

fun <NewT : Any> withDocumentClass(): CoroutineCollection<NewT>

withReadConcern

Create a new MongoCollection instance with a different read concern.

fun withReadConcern(readConcern: ReadConcern): CoroutineCollection<T>

withReadPreference

Create a new MongoCollection instance with a different read preference.

fun withReadPreference(readPreference: ReadPreference): CoroutineCollection<T>

withWriteConcern

Create a new MongoCollection instance with a different write concern.

fun withWriteConcern(writeConcern: WriteConcern): CoroutineCollection<T>

Extension Functions

aggregate

Aggregates documents according to the specified aggregation pipeline. If the pipeline ends with a $out stage, the returned iterable will be a query of the collection that the aggregation was written to. Note that in this case the pipeline will be executed even if the iterable is never iterated.

fun <T : Any> CoroutineCollection<*>.aggregate(vararg pipeline: String): CoroutineAggregatePublisher<T>
fun <T : Any> CoroutineCollection<*>.aggregate(vararg pipeline: Bson): CoroutineAggregatePublisher<T>

bulkWrite

Executes a mix of inserts, updates, replaces, and deletes.

suspend fun <T : Any> CoroutineCollection<T>.bulkWrite(clientSession: ClientSession, vararg requests: String, options: BulkWriteOptions = BulkWriteOptions()): BulkWriteResult
suspend fun <T : Any> CoroutineCollection<T>.bulkWrite(vararg requests: String, options: BulkWriteOptions = BulkWriteOptions()): BulkWriteResult

insertOne

Inserts the provided document. If the document is missing an identifier, the driver should generate one.

suspend fun <T : Any> CoroutineCollection<T>.insertOne(document: String, options: InsertOneOptions = InsertOneOptions()): InsertOneResult
suspend fun <T : Any> CoroutineCollection<T>.insertOne(clientSession: ClientSession, document: String, options: InsertOneOptions = InsertOneOptions()): InsertOneResult

projection

Returns the specified field for all matching documents.

fun <F : Any> CoroutineCollection<*>.projection(property: KProperty<F?>, query: Bson = EMPTY_BSON, options: (CoroutineFindPublisher<SingleProjection<F>>) -> CoroutineFindPublisher<SingleProjection<F>> = { it }): CoroutineFindPublisher<F>

Returns the specified two fields for all matching documents.

fun <F1 : Any, F2 : Any> CoroutineCollection<*>.projection(property1: KProperty<F1?>, property2: KProperty<F2?>, query: Bson = EMPTY_BSON, options: (CoroutineFindPublisher<PairProjection<F1, F2>>) -> CoroutineFindPublisher<PairProjection<F1, F2>> = { it }): CoroutineFindPublisher<Pair<F1?, F2?>>

Returns the specified three fields for all matching documents.

fun <F1 : Any, F2 : Any, F3 : Any> CoroutineCollection<*>.projection(property1: KProperty<F1?>, property2: KProperty<F2?>, property3: KProperty<F3?>, query: Bson = EMPTY_BSON, options: (CoroutineFindPublisher<TripleProjection<F1, F2, F3>>) -> CoroutineFindPublisher<TripleProjection<F1, F2, F3>> = { it }): CoroutineFindPublisher<Triple<F1?, F2?, F3?>>

replaceOne

Replace a document in the collection according to the specified arguments.

suspend fun <T : Any> CoroutineCollection<T>.replaceOne(replacement: T, options: ReplaceOptions = ReplaceOptions()): UpdateResult

updateOne

Update a single document in the collection according to the specified arguments.

suspend fun <T : Any> CoroutineCollection<T>.updateOne(target: T, options: UpdateOptions = UpdateOptions(), updateOnlyNotNullProperties: Boolean = UpdateConfiguration.updateOnlyNotNullProperties): UpdateResult
suspend fun <T : Any> CoroutineCollection<T>.updateOne(clientSession: ClientSession, target: T, options: UpdateOptions = UpdateOptions(), updateOnlyNotNullProperties: Boolean = UpdateConfiguration.updateOnlyNotNullProperties): UpdateResult