kmongo / org.litote.kmongo / com.mongodb.client.MongoCollection

Extensions for com.mongodb.client.MongoCollection

aggregate

Aggregates documents according to the specified aggregation pipeline.

fun <TResult : Any> MongoCollection<*>.aggregate(vararg pipeline: String): AggregateIterable<TResult>
fun <TResult : Any> MongoCollection<*>.aggregate(vararg pipeline: Bson): AggregateIterable<TResult>
fun <TResult : Any> MongoCollection<*>.aggregate(clientSession: ClientSession, vararg pipeline: String): AggregateIterable<TResult>
fun <TResult : Any> MongoCollection<*>.aggregate(clientSession: ClientSession, vararg pipeline: Bson): AggregateIterable<TResult>

bulkWrite

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

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

countDocuments

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

fun <T> MongoCollection<T>.countDocuments(filter: String, options: CountOptions = CountOptions()): Long
fun <T> MongoCollection<T>.countDocuments(clientSession: ClientSession, filter: String, options: CountOptions = CountOptions()): Long

createIndex

Create an index with the given keys and options.

fun <T> MongoCollection<T>.createIndex(keys: String, indexOptions: IndexOptions = IndexOptions()): String
fun <T> MongoCollection<T>.createIndex(clientSession: ClientSession, keys: String, indexOptions: IndexOptions = IndexOptions()): String

deleteMany

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

fun <T> MongoCollection<T>.deleteMany(filter: String, options: DeleteOptions = DeleteOptions()): DeleteResult
fun <T> MongoCollection<T>.deleteMany(vararg filters: Bson?, options: DeleteOptions = DeleteOptions()): DeleteResult
fun <T> MongoCollection<T>.deleteMany(clientSession: ClientSession, filter: String, options: DeleteOptions = DeleteOptions()): DeleteResult
fun <T> MongoCollection<T>.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.

fun <T> MongoCollection<T>.deleteOne(filter: String): DeleteResult
fun <T> MongoCollection<T>.deleteOne(vararg filters: Bson?): DeleteResult
fun <T> MongoCollection<T>.deleteOne(clientSession: ClientSession, filter: String): DeleteResult
fun <T> MongoCollection<T>.deleteOne(clientSession: ClientSession, vararg filters: Bson?): DeleteResult

deleteOneById

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

fun <T> MongoCollection<T>.deleteOneById(id: Any): DeleteResult
fun <T> MongoCollection<T>.deleteOneById(clientSession: ClientSession, id: Any): DeleteResult

distinct

Gets the distinct values of the specified field name.

fun <TResult : Any> MongoCollection<*>.distinct(fieldName: String, filter: String = KMongoUtil.EMPTY_JSON): DistinctIterable<TResult>
fun <TResult : Any> MongoCollection<*>.distinct(clientSession: ClientSession, fieldName: String, filter: String = KMongoUtil.EMPTY_JSON): DistinctIterable<TResult>

Gets the distinct values of the specified field.

fun <T : Any, TResult> MongoCollection<T>.distinct(field: KProperty1<T, TResult>, filter: Bson = EMPTY_BSON): DistinctIterable<TResult>
fun <T : Any, TResult> MongoCollection<T>.distinct(clientSession: ClientSession, field: KProperty1<T, TResult>, filter: Bson = EMPTY_BSON): DistinctIterable<TResult>

dropIndex

Drops the index given the keys used to create it.

fun <T> MongoCollection<T>.dropIndex(keys: String): Unit
fun <T> MongoCollection<T>.dropIndex(clientSession: ClientSession, keys: String): Unit

dropIndexOfKeys

Drops the index given the keys used to create it.

fun <T> MongoCollection<T>.dropIndexOfKeys(json: String): Unit
fun <T> MongoCollection<T>.dropIndexOfKeys(clientSession: ClientSession, json: String): Unit

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.

fun <T> MongoCollection<T>.ensureIndex(keys: Bson, indexOptions: IndexOptions = IndexOptions()): String
fun <T> MongoCollection<T>.ensureIndex(keys: String, indexOptions: IndexOptions = IndexOptions()): String
fun <T> MongoCollection<T>.ensureIndex(clientSession: ClientSession, keys: Bson, indexOptions: IndexOptions = IndexOptions()): String
fun <T> MongoCollection<T>.ensureIndex(clientSession: ClientSession, keys: String, indexOptions: IndexOptions = IndexOptions()): String

Create an ascending 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.

fun <T> MongoCollection<T>.ensureIndex(vararg properties: KProperty<*>, indexOptions: IndexOptions = IndexOptions()): String
fun <T> MongoCollection<T>.ensureIndex(clientSession: ClientSession, 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.

fun <T> MongoCollection<T>.ensureUniqueIndex(vararg properties: KProperty<*>, indexOptions: IndexOptions = IndexOptions()): String
fun <T> MongoCollection<T>.ensureUniqueIndex(clientSession: ClientSession, vararg properties: KProperty<*>, indexOptions: IndexOptions = IndexOptions()): String

find

Finds all documents in the collection.

fun <T> MongoCollection<T>.find(filter: String = KMongoUtil.EMPTY_JSON): FindIterable<T>
fun <T> MongoCollection<T>.find(vararg filters: Bson?): FindIterable<T>
fun <T> MongoCollection<T>.find(clientSession: ClientSession, filter: String = KMongoUtil.EMPTY_JSON): FindIterable<T>
fun <T> MongoCollection<T>.find(clientSession: ClientSession, vararg filters: Bson?): FindIterable<T>

findOne

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

fun <T> MongoCollection<T>.findOne(filter: String = KMongoUtil.EMPTY_JSON): T?
fun <T> MongoCollection<T>.findOne(filter: Bson): T?
fun <T : Any> MongoCollection<T>.findOne(filters: () -> Bson): T?
fun <T> MongoCollection<T>.findOne(clientSession: ClientSession, filter: String = KMongoUtil.EMPTY_JSON): T?
fun <T> MongoCollection<T>.findOne(clientSession: ClientSession, filter: Bson): T?
fun <T : Any> MongoCollection<T>.findOne(clientSession: ClientSession, filters: () -> Bson): T?

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

fun <T> MongoCollection<T>.findOne(vararg filters: Bson?): T?
fun <T> MongoCollection<T>.findOne(clientSession: ClientSession, vararg filters: Bson?): T?

findOneAndDelete

Atomically find a document and remove it.

fun <T> MongoCollection<T>.findOneAndDelete(filter: String, options: FindOneAndDeleteOptions = FindOneAndDeleteOptions()): T?
fun <T> MongoCollection<T>.findOneAndDelete(clientSession: ClientSession, filter: String, options: FindOneAndDeleteOptions = FindOneAndDeleteOptions()): T?

findOneAndReplace

Atomically find a document and replace it.

fun <T> MongoCollection<T>.findOneAndReplace(filter: String, replacement: T, options: FindOneAndReplaceOptions = FindOneAndReplaceOptions()): T?
fun <T> MongoCollection<T>.findOneAndReplace(clientSession: ClientSession, filter: String, replacement: T, options: FindOneAndReplaceOptions = FindOneAndReplaceOptions()): T?

findOneAndUpdate

Atomically find a document and update it.

fun <T> MongoCollection<T>.findOneAndUpdate(filter: String, update: String, options: FindOneAndUpdateOptions = FindOneAndUpdateOptions()): T?
fun <T> MongoCollection<T>.findOneAndUpdate(clientSession: ClientSession, filter: String, update: String, options: FindOneAndUpdateOptions = FindOneAndUpdateOptions()): T?

findOneById

Finds the document that match the id parameter.

fun <T> MongoCollection<T>.findOneById(id: Any): T?
fun <T> MongoCollection<T>.findOneById(clientSession: ClientSession, id: Any): T?

insertOne

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

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

listIndexes

Get all the indexes in this collection.

fun <TResult : Any> MongoCollection<*>.listIndexes(): ListIndexesIterable<TResult>
fun <TResult : Any> MongoCollection<*>.listIndexes(clientSession: ClientSession): ListIndexesIterable<TResult>

listTypedIndexes

Get all the indexes in this collection.

fun <TResult : Any> MongoCollection<*>.listTypedIndexes(): ListIndexesIterable<TResult>
fun <TResult : Any> MongoCollection<*>.listTypedIndexes(clientSession: ClientSession): ListIndexesIterable<TResult>

mapReduce

Aggregates documents according to the specified map-reduce function.

fun <TResult : Any> MongoCollection<*>.mapReduce(mapFunction: String, reduceFunction: String): MapReduceIterable<TResult>
fun <TResult : Any> MongoCollection<*>.mapReduce(clientSession: ClientSession, mapFunction: String, reduceFunction: String): MapReduceIterable<TResult>

mapReduceWith

Aggregates documents according to the specified map-reduce function.

fun <TResult : Any> MongoCollection<*>.mapReduceWith(mapFunction: String, reduceFunction: String): MapReduceIterable<TResult>
fun <TResult : Any> MongoCollection<*>.mapReduceWith(clientSession: ClientSession, mapFunction: String, reduceFunction: String): MapReduceIterable<TResult>

projection

Returns the specified field for all matching documents.

fun <T, F> MongoCollection<T>.projection(property: KProperty<F>, query: Bson = EMPTY_BSON, options: (FindIterable<SingleProjection<F>>) -> FindIterable<SingleProjection<F>> = { it }): MongoIterable<F>
fun <T, F> MongoCollection<T>.projection(clientSession: ClientSession, property: KProperty<F>, query: Bson = EMPTY_BSON, options: (FindIterable<SingleProjection<F>>) -> FindIterable<SingleProjection<F>> = { it }): MongoIterable<F>

Returns the specified two fields for all matching documents.

fun <T, F1, F2> MongoCollection<T>.projection(property1: KProperty<F1>, property2: KProperty<F2>, query: Bson = EMPTY_BSON, options: (FindIterable<PairProjection<F1, F2>>) -> FindIterable<PairProjection<F1, F2>> = { it }): MongoIterable<Pair<F1?, F2?>>
fun <T, F1, F2> MongoCollection<T>.projection(clientSession: ClientSession, property1: KProperty<F1>, property2: KProperty<F2>, query: Bson = EMPTY_BSON, options: (FindIterable<PairProjection<F1, F2>>) -> FindIterable<PairProjection<F1, F2>> = { it }): MongoIterable<Pair<F1?, F2?>>

Returns the specified three fields for all matching documents.

fun <T, F1, F2, F3> MongoCollection<T>.projection(property1: KProperty<F1>, property2: KProperty<F2>, property3: KProperty<F3>, query: Bson = EMPTY_BSON, options: (FindIterable<TripleProjection<F1, F2, F3>>) -> FindIterable<TripleProjection<F1, F2, F3>> = { it }): MongoIterable<Triple<F1?, F2?, F3?>>
fun <T, F1, F2, F3> MongoCollection<T>.projection(clientSession: ClientSession, property1: KProperty<F1>, property2: KProperty<F2>, property3: KProperty<F3>, query: Bson = EMPTY_BSON, options: (FindIterable<TripleProjection<F1, F2, F3>>) -> FindIterable<TripleProjection<F1, F2, F3>> = { it }): MongoIterable<Triple<F1?, F2?, F3?>>

replaceOne

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

fun <T : Any> MongoCollection<T>.replaceOne(replacement: T, options: ReplaceOptions = ReplaceOptions()): UpdateResult
fun <T : Any> MongoCollection<T>.replaceOne(filter: String, replacement: T, options: ReplaceOptions = ReplaceOptions()): UpdateResult
fun <T : Any> MongoCollection<T>.replaceOne(clientSession: ClientSession, replacement: T, options: ReplaceOptions = ReplaceOptions()): UpdateResult
fun <T : Any> MongoCollection<T>.replaceOne(clientSession: ClientSession, filter: String, replacement: T, options: ReplaceOptions = ReplaceOptions()): UpdateResult

replaceOneById

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

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

replaceOneWithFilter

Replace a document in the collection according to the specified arguments. Same than MongoCollection.replaceOne but ensure that any _id present in replacement is removed to avoid MongoWriteException such as: "After applying the update, the (immutable) field '_id' was found to have been altered to _id"

fun <T : Any> MongoCollection<T>.replaceOneWithFilter(filter: Bson, replacement: T, replaceOptions: ReplaceOptions = ReplaceOptions()): UpdateResult
fun <T : Any> MongoCollection<T>.replaceOneWithFilter(clientSession: ClientSession, filter: Bson, replacement: T, replaceOptions: 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.

fun <T : Any> MongoCollection<T>.save(document: T): Unit
fun <T : Any> MongoCollection<T>.save(clientSession: ClientSession, document: T): Unit

updateMany

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

fun <T : Any> MongoCollection<T>.updateMany(filter: String, update: String, updateOptions: UpdateOptions = UpdateOptions()): UpdateResult
fun <T : Any> MongoCollection<T>.updateMany(filter: Bson, vararg updates: SetTo<*>, updateOptions: UpdateOptions = UpdateOptions()): UpdateResult
fun <T : Any> MongoCollection<T>.updateMany(clientSession: ClientSession, filter: String, update: String, updateOptions: UpdateOptions = UpdateOptions()): UpdateResult
fun <T : Any> MongoCollection<T>.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.

fun <T> MongoCollection<T>.updateOne(filter: String, update: String, options: UpdateOptions = UpdateOptions()): UpdateResult
fun <T> MongoCollection<T>.updateOne(filter: String, update: Any, options: UpdateOptions = UpdateOptions(), updateOnlyNotNullProperties: Boolean = UpdateConfiguration.updateOnlyNotNullProperties): UpdateResult
fun <T : Any> MongoCollection<T>.updateOne(target: T, options: UpdateOptions = UpdateOptions(), updateOnlyNotNullProperties: Boolean = UpdateConfiguration.updateOnlyNotNullProperties): UpdateResult
fun <T> MongoCollection<T>.updateOne(filter: Bson, target: Any, options: UpdateOptions = UpdateOptions(), updateOnlyNotNullProperties: Boolean = UpdateConfiguration.updateOnlyNotNullProperties): UpdateResult
fun <T : Any> MongoCollection<T>.updateOne(filter: Bson, vararg updates: SetTo<*>, updateOptions: UpdateOptions = UpdateOptions()): UpdateResult
fun <T> MongoCollection<T>.updateOne(clientSession: ClientSession, filter: String, update: String, options: UpdateOptions = UpdateOptions()): UpdateResult
fun <T> MongoCollection<T>.updateOne(clientSession: ClientSession, filter: String, update: Any, options: UpdateOptions = UpdateOptions(), updateOnlyNotNullProperties: Boolean = UpdateConfiguration.updateOnlyNotNullProperties): UpdateResult
fun <T : Any> MongoCollection<T>.updateOne(clientSession: ClientSession, target: T, options: UpdateOptions = UpdateOptions(), updateOnlyNotNullProperties: Boolean = UpdateConfiguration.updateOnlyNotNullProperties): UpdateResult
fun <T> MongoCollection<T>.updateOne(clientSession: ClientSession, filter: Bson, target: Any, options: UpdateOptions = UpdateOptions(), updateOnlyNotNullProperties: Boolean = UpdateConfiguration.updateOnlyNotNullProperties): UpdateResult
fun <T : Any> MongoCollection<T>.updateOne(clientSession: ClientSession, filter: Bson, vararg updates: SetTo<*>, updateOptions: UpdateOptions = UpdateOptions()): UpdateResult

updateOneById

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

fun <T> MongoCollection<T>.updateOneById(id: Any, update: Any, options: UpdateOptions = UpdateOptions(), updateOnlyNotNullProperties: Boolean = UpdateConfiguration.updateOnlyNotNullProperties): UpdateResult
fun <T : Any> MongoCollection<T>.updateOneById(id: Any, vararg updates: SetTo<*>, options: UpdateOptions = UpdateOptions()): UpdateResult
fun <T> MongoCollection<T>.updateOneById(clientSession: ClientSession, id: Any, update: Any, options: UpdateOptions = UpdateOptions(), updateOnlyNotNullProperties: Boolean = UpdateConfiguration.updateOnlyNotNullProperties): UpdateResult

withDocumentClass

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

fun <NewTDocument : Any> MongoCollection<*>.withDocumentClass(): MongoCollection<NewTDocument>

withKMongo

Returns a MongoCollection with a KMongo codec.

fun <T> MongoCollection<T>.withKMongo(): MongoCollection<T>