Collection
TheCollection 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 usingdb.getCollection() with a type parameter:
Collection info
name()
Returns the collection name.
string
The collection name
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 documentfindById()
Finds a document by its ID.
string
required
Document ID to find
T | null
The document if found,
null otherwiseupdateById()
Updates a document by its ID.
string
required
Document ID to update
Partial<T>
required
Partial document with fields to update
deleteById()
Deletes a document by its ID.
string
required
Document ID to delete
findAll()
Retrieves all documents in the collection.
T[]
Array of all documents
count()
Counts all documents in the collection.
number
Number of documents
Query operations
find()
Finds all documents matching a JMESPath filter.
string
required
JMESPath query expression
T[]
Array of matching documents
findOne()
Finds the first document matching a filter.
string
required
JMESPath query expression
T | null
The first matching document, or
null if none foundupdate()
Updates all documents matching a filter.
string
required
JMESPath query expression
Partial<T>
required
Fields to update
number
Number of documents updated
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 otherwisedelete()
Deletes all documents matching a filter.
string
required
JMESPath query expression
number
Number of documents deleted
deleteOne()
Deletes the first document matching a filter.
string
required
JMESPath query expression
boolean
true if a document was deleted, false otherwiseUpsert 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
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
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
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: trueBulkWriteResult
Statistics about the bulk operation
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 falsenumber
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
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 falseT | null
The first matching document, or
null if none founddistinct()
Returns unique values for a field.
K
required
Field name to get distinct values for
T[K][]
Array of unique values
countDistinct()
Counts unique values for a field.
keyof T
required
Field name
number
Number of distinct values
countWithQuery()
Counts documents matching a filter.
string
JMESPath query expression (optional)
number
Number of matching documents
Full-text search
search()
Performs full-text search on indexed fields.
string
required
Search query string
SearchResult[]
Array of search results with scores
Aggregation
aggregate()
Runs an aggregation pipeline on the collection.
AggregationStage[]
required
Array of aggregation stages
R[]
Array of aggregation results (shape depends on pipeline)
Change tracking
watch()
Watches for changes to documents in the collection.
string | undefined
required
JMESPath filter to watch specific documents, or
undefined to watch allWatchCallback<T>
required
Function called when changes occurCallback signature:
WatchHandle
Handle to stop watching