Skip to main content

Collection

The Collection class provides a type-safe, high-level API for working with documents in a specific collection. Collections automatically handle transactions and provide rich querying capabilities.

Getting a collection

Collections are obtained using db.getCollection() with a type parameter:

Collection info

name()

Returns the collection name.
string
The collection name
Example

Basic CRUD operations

insert()

Inserts a document into the collection.
Omit<T, '_id'>
required
Document to insert (without _id field)
string
The auto-generated _id of the inserted document
Example

findById()

Finds a document by its ID.
string
required
Document ID to find
T | null
The document if found, null otherwise
Example

updateById()

Updates a document by its ID.
string
required
Document ID to update
Partial<T>
required
Partial document with fields to update
Example

deleteById()

Deletes a document by its ID.
string
required
Document ID to delete
Example

findAll()

Retrieves all documents in the collection.
T[]
Array of all documents
Example

count()

Counts all documents in the collection.
number
Number of documents
Example

Query operations

find()

Finds all documents matching a JMESPath filter.
string
required
JMESPath query expression
T[]
Array of matching documents
Example

findOne()

Finds the first document matching a filter.
string
required
JMESPath query expression
T | null
The first matching document, or null if none found
Example

update()

Updates all documents matching a filter.
string
required
JMESPath query expression
Partial<T>
required
Fields to update
number
Number of documents updated
Example

updateOne()

Updates the first document matching a filter.
string
required
JMESPath query expression
Partial<T>
required
Fields to update
boolean
true if a document was updated, false otherwise
Example

delete()

Deletes all documents matching a filter.
string
required
JMESPath query expression
number
Number of documents deleted
Example

deleteOne()

Deletes the first document matching a filter.
string
required
JMESPath query expression
boolean
true if a document was deleted, false otherwise
Example

Upsert operations

upsertById()

Inserts or updates a document by ID.
string
required
Document ID
Omit<T, '_id'>
required
Document data (without _id)
UpsertResult
Result indicating whether document was inserted or updated
Example

upsert()

Inserts or updates the first document matching a filter.
string
required
JMESPath query expression
Omit<T, '_id'>
required
Document data (without _id)
UpsertResult
Result indicating whether document was inserted or updated
Example

Bulk operations

insertMany()

Inserts multiple documents in a single operation.
Omit<T, '_id'>[]
required
Array of documents to insert
string[]
Array of generated document IDs
Example

bulkWrite()

Executes multiple mixed operations (insert, update, delete) in one call.
BulkOperation<Omit<T, '_id'>>[]
required
Array of bulk operations to execute
boolean
If true, stops on first error. If false, continues on errors. Default: true
BulkWriteResult
Statistics about the bulk operation
Example

Advanced queries

queryWithOptions()

Performs a query with sorting, pagination, and field projection.
string
JMESPath query expression (optional)
keyof T & string
Field to sort by
boolean
Sort ascending if true, descending if false
number
Maximum number of documents to return
number
Number of documents to skip
(keyof T & string)[]
Fields to include in results
(keyof T & string)[]
Fields to exclude from results
T[]
Array of matching documents
Example

queryCount()

Counts documents matching a query with pagination.
string
JMESPath query expression (optional)
number
Number of documents to skip
number
Maximum number of documents to count
number
Number of matching documents

queryFirst()

Returns the first document matching a query with optional sorting.
string
JMESPath query expression (optional)
keyof T & string
Field to sort by
boolean
Sort ascending if true, descending if false
T | null
The first matching document, or null if none found
Example

distinct()

Returns unique values for a field.
K
required
Field name to get distinct values for
T[K][]
Array of unique values
Example

countDistinct()

Counts unique values for a field.
keyof T
required
Field name
number
Number of distinct values
Example

countWithQuery()

Counts documents matching a filter.
string
JMESPath query expression (optional)
number
Number of matching documents
Example
Performs full-text search on indexed fields.
string
required
Search query string
SearchResult[]
Array of search results with scores
Example

Aggregation

aggregate()

Runs an aggregation pipeline on the collection.
AggregationStage[]
required
Array of aggregation stages
R[]
Array of aggregation results (shape depends on pipeline)
Example
See the Types reference for all available aggregation stages.

Change tracking

watch()

Watches for changes to documents in the collection.
string | undefined
required
JMESPath filter to watch specific documents, or undefined to watch all
WatchCallback<T>
required
Function called when changes occurCallback signature:
WatchHandle
Handle to stop watching
Example