Skip to content

SyncMongoCollection

interface SyncMongoCollection<Document : Any> : MongoCollection<Document> 

A collection stores related documents together.

The Coroutine client provides a coroutine-aware API which internally uses the official Kotlin driver.

Usually, all documents in a collection have the same shape (the same fields). However, heterogeneous structure can be achieved by using:

  • Kotlin collections, like List and Set, the embed an arbitrary number of items.

  • Polymorphism, for example with sealed class, to have different fields based on a discriminator.

To avoid name collisions, collections are grouped into databases.

To obtain a collection, see SyncMongoDatabase.collection.

Size limit

A MongoDB document cannot exceed 16 MiB.

You can measure the size of a document with opensavvy.ktmongo.bson.BsonDocument.toByteArray followed by ByteArray.size.

The maximum nesting is 100 levels. Each document or array adds a level.

External resources

See also

  • asKtMongo: Convert an existing instance from the official Kotlin driver.

Types

UpsertResult

interface UpsertResult : UpdateOperations.UpsertResult

The return value of upsertOne and upsertOneWithPipeline.

Properties

context

factory

abstract override val factory: BsonFactory

fullyQualifiedName

abstract val fullyQualifiedName: String

name

abstract val name: String

objectIdGenerator

propertyNameStrategy

type

@LowLevelApi
abstract val type: KType

Functions

aggregate

abstract override fun aggregate(): SyncMongoAggregationPipeline<Document>

asOfficial

abstract fun asOfficial(): MongoCollection<Document>

Obtains the underlying MongoDB collection from the official Kotlin driver.

bulkWrite

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

count

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

countEstimated

abstract fun countEstimated(): Long

create

abstract fun create(options: CreateCollectionOptions<Document>.() -> Unit)

deleteMany

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

deleteOne

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

drop

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

exists

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

filter

abstract override fun filter(filter: FilterQuery<Document>.() -> Unit): SyncMongoCollection<Document>

find

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

findOne

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

findOneAndUpdate

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

insertMany

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

insertOne

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

newId

open override fun newId(): ObjectId

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
)

unsafeCast

abstract override fun <NewType : Any> unsafeCast(type: KType): SyncMongoCollection<NewType>

inline fun <T : Any> unsafeCast(): SyncMongoCollection<T>

Overwrites the represented type of this value.

This method can be useful to bypass type checks.

Example

If we know there are some documents that have the wrong format, we can use unsafeCast to find them without needing to add their fields to the production DTO.

class User(
    val _id: ObjectId,
    val name: String,
)

users.unsafeCast<BsonDocument>() // Treat all data as arbitrary BSON documents
    .find { BsonDocument.get<String?>("oldField") ne null }
    .forEach {
        println(it["oldField"]?.decodeString())
    }

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 override fun upsertOne(
    options: UpdateOptions<Document>.() -> Unit, 
    filter: FilterQuery<Document>.() -> Unit, 
    update: UpsertQuery<Document>.() -> Unit
): SyncMongoCollection.UpsertResult

upsertOneWithPipeline