MongoCollection

A collection stores related documents together.

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 MongoDatabase.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.

Operations

The following lists the available operations using the mongosh equivalent:

External resources

Properties

Link copied to clipboard

The full BSON configuration, used by the DSL to generate queries.

Link copied to clipboard
abstract val factory: BsonFactory

The BsonFactory used to serialize and deserialize values stored in this collection.

Link copied to clipboard

The concatenation of the database's name and the collection's name, separated by a dot (.).

Link copied to clipboard
abstract val name: String

THe name of this collection.

Link copied to clipboard

The algorithm used to generate new ObjectId instances for this collection.

Link copied to clipboard

The strategy used to convert property names to BSON document keys.

Link copied to clipboard
abstract val type: KType

The KType instance that corresponds to the collection's document type.

Functions

Link copied to clipboard

Starts an aggregation pipeline on this collection.

Link copied to clipboard
abstract suspend fun bulkWrite(options: BulkWriteOptions<Document>.() -> Unit = {}, filter: FilterQuery<Document>.() -> Unit = {}, operations: BulkWrite<Document>.() -> Unit)

Performs multiple update operations in a single request.

Link copied to clipboard
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.

Link copied to clipboard
abstract suspend fun countEstimated(): Long

Counts all documents in the collection.

Link copied to clipboard
abstract suspend fun deleteMany(options: DeleteManyOptions<Document>.() -> Unit = {}, filter: FilterQuery<Document>.() -> Unit)

Deletes all documents that match filter.

Link copied to clipboard
abstract suspend fun deleteOne(options: DeleteOneOptions<Document>.() -> Unit = {}, filter: FilterQuery<Document>.() -> Unit)

Deletes the first document found that matches filter.

Link copied to clipboard
abstract suspend fun drop(options: DropOptions<Document>.() -> Unit = {})

Removes an entire collection from the database.

Link copied to clipboard
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.

Link copied to clipboard
abstract override fun filter(filter: FilterQuery<Document>.() -> Unit): MongoCollection<Document>

Creates a client-side view containing all the documents that match filter.

Link copied to clipboard
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.

Link copied to clipboard
open suspend fun findOne(options: FindOptions<Document>.() -> Unit = {}, filter: FilterQuery<Document>.() -> Unit): Document?

Finds a document in this collection that satisfies filter.

Link copied to clipboard
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.

Link copied to clipboard
abstract suspend fun insertMany(documents: Iterable<Document>, options: InsertManyOptions<Document>.() -> Unit = {})
open suspend fun insertMany(vararg documents: Document, options: InsertManyOptions<Document>.() -> Unit = {})

Inserts multiple documents in a single operation.

Link copied to clipboard
abstract suspend fun insertOne(document: Document, options: InsertOneOptions<Document>.() -> Unit = {})

Inserts a document.

Link copied to clipboard
open override fun newId(): ObjectId
Link copied to clipboard
abstract suspend fun replaceOne(options: ReplaceOptions<Document>.() -> Unit = {}, filter: FilterQuery<Document>.() -> Unit = {}, document: Document)

Replaces a document that matches filter by document.

Link copied to clipboard
abstract suspend fun repsertOne(options: ReplaceOptions<Document>.() -> Unit = {}, filter: FilterQuery<Document>.() -> Unit = {}, document: Document)

Replaces a document that matches filter by document.

Link copied to clipboard
abstract suspend fun updateMany(options: UpdateOptions<Document>.() -> Unit = {}, filter: FilterQuery<Document>.() -> Unit = {}, update: UpdateQuery<Document>.() -> Unit): UpdateOperations.UpdateResult

Updates all documents that match filter according to update.

Link copied to clipboard

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

Link copied to clipboard
abstract suspend fun updateOne(options: UpdateOptions<Document>.() -> Unit = {}, filter: FilterQuery<Document>.() -> Unit = {}, update: UpdateQuery<Document>.() -> Unit): UpdateOperations.UpdateResult

Updates a single document that matches filter according to update.

Link copied to clipboard

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

Link copied to clipboard
abstract suspend fun upsertOne(options: UpdateOptions<Document>.() -> Unit = {}, filter: FilterQuery<Document>.() -> Unit = {}, update: UpsertQuery<Document>.() -> Unit): UpdateOperations.UpsertResult

Updates a single document that matches filter according to update.

Link copied to clipboard

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