prorm API Reference
    Preparing search index...

    Class MongoStore

    MongoStore wraps the official mongodb driver's MongoClient/Db/ Collection API in a small, typed convenience layer.

    Implements

    Index
    name: "mongodb" = 'mongodb'

    The name of the store (e.g. 'mongodb', 'redis', 'dynamodb')

    library: "mongodb" = 'mongodb'

    The client library being used

    • Returns the underlying native Db handle for the configured database.

      Returns Db

    • Typed collection accessor, e.g. store.collection<User>('users').

      Type Parameters

      • T extends Document = Document

      Parameters

      • name: string

      Returns Collection<T>

    • Type Parameters

      • T extends Document = Document

      Parameters

      • collectionName: string
      • filter: Filter<T> = {}
      • Optionaloptions: FindOptions<T>

      Returns Promise<T | null>

    • Type Parameters

      • T extends Document = Document

      Parameters

      Returns Promise<T[]>

    • Like find, but returns the native FindCursor instead of materializing the results with toArray(). Use this for large result sets that shouldn't be pulled fully into memory - iterate with for await (const doc of cursor) or use cursor.next()/cursor.forEach().

      Type Parameters

      • T extends Document = Document

      Parameters

      Returns FindCursor<WithId<T>>

    • Type Parameters

      • T extends Document = Document

      Parameters

      • collectionName: string
      • filter: Filter<T> = {}
      • Optionaloptions: CountDocumentsOptions

      Returns Promise<number>

    • Returns a fast, metadata-based approximate count of all documents in collectionName. Unlike countDocuments, this does not accept a filter and does not scan the collection - it reads the collection's cached document count, so it's cheap but can be stale/approximate (e.g. immediately after bulk writes, or on a sharded cluster).

      Parameters

      • collectionName: string
      • Optionaloptions: EstimatedDocumentCountOptions

      Returns Promise<number>

    • Type Parameters

      • T extends Document = Document
      • R extends Document = Document

      Parameters

      • collectionName: string
      • pipeline: Document[]
      • Optionaloptions: AggregateOptions

      Returns Promise<R[]>

    • Returns the distinct values for field across documents matching filter.

      Type Parameters

      • T extends Document = Document

      Parameters

      • collectionName: string
      • field: string
      • filter: Filter<T> = {}
      • Optionaloptions: DistinctOptions

      Returns Promise<unknown[]>

    • Translate an ORM where object (with Op.* operators and Op.and/Op.or/Op.not) into a native MongoDB filter document. Exposed for callers that want to feed the translated filter into other native operations (e.g. aggregate, watch).

      Type Parameters

      • T extends Document = Document

      Parameters

      • where: Record<string | symbol, unknown>

      Returns Filter<T>

    • Fetch all documents in collectionName matching an ORM where clause, with optional ordering, offset, limit, and projection. The where object accepts the full ORM operator surface (Op.gt, Op.in, Op.between, Op.like, Op.and/Op.or, ...), translated to a native MongoDB filter - so standard queries never require hand-written $-filters.

      Type Parameters

      • T extends Document = Document

      Parameters

      • collectionName: string
      • where: Record<string | symbol, unknown> = {}
      • options: OrmFindOptions = {}

      Returns Promise<T[]>

    • Fetch the first document in collectionName matching an ORM where clause (respecting order), or null.

      Type Parameters

      • T extends Document = Document

      Parameters

      • collectionName: string
      • where: Record<string | symbol, unknown> = {}
      • options: Omit<OrmFindOptions, "limit"> = {}

      Returns Promise<T | null>

    • Count documents in collectionName matching an ORM where clause.

      Type Parameters

      • T extends Document = Document

      Parameters

      • collectionName: string
      • where: Record<string | symbol, unknown> = {}

      Returns Promise<number>

    • Update every document in collectionName matching an ORM where clause, applying patch (a raw $-update, or a plain object which is wrapped in $set).

      Type Parameters

      • T extends Document = Document

      Parameters

      • collectionName: string
      • where: Record<string | symbol, unknown>
      • patch: UpdateFilter<T> | Partial<T>
      • Optionaloptions: UpdateOptions

      Returns Promise<Document | UpdateResult<T>>

    • Delete every document in collectionName matching an ORM where clause.

      Type Parameters

      • T extends Document = Document

      Parameters

      • collectionName: string
      • where: Record<string | symbol, unknown>
      • Optionaloptions: DeleteOptions

      Returns Promise<DeleteResult>

    • Insert-or-update by an ORM where clause: applies patch to a matching document, inserting one (with the patch merged over the filter's implied fields) if none matches. Thin wrapper over updateOne with upsert: true.

      Type Parameters

      • T extends Document = Document

      Parameters

      • collectionName: string
      • where: Record<string | symbol, unknown>
      • patch: UpdateFilter<T> | Partial<T>
      • Optionaloptions: Omit<UpdateOptions, "upsert">

      Returns Promise<UpdateResult<T>>

    • Grouped aggregation over collectionName using a native $match/$group pipeline (count/sum/avg/min/max), with an optional ORM where pre-filter. Returns one entry per group: { group, ...aggregates } (group is null when no groupBy is given).

      Parameters

      • collectionName: string
      • options: OrmAggregateOptions = {}

      Returns Promise<Record<string, unknown>[]>

    • Fetch documents from collectionName matching an ORM where clause, with one or more associations eager-loaded via native $lookup (the MongoDB equivalent of a SQL join / ORM include). Each MongoInclude attaches matched documents under its as key; single: true unwinds to a single document for to-one associations.

      Type Parameters

      • T extends Document = Document
      • R extends Document = Document

      Parameters

      • collectionName: string
      • where: Record<string | symbol, unknown> = {}
      • includes: MongoInclude[] = []
      • options: OrmFindOptions = {}

      Returns Promise<R[]>

    • Type Parameters

      • T extends Document = Document

      Parameters

      • collectionName: string
      • doc: OptionalUnlessRequiredId<T>
      • Optionaloptions: InsertOneOptions

      Returns Promise<InsertOneResult<T>>

    • Type Parameters

      • T extends Document = Document

      Parameters

      • collectionName: string
      • docs: OptionalUnlessRequiredId<T>[]
      • Optionaloptions: BulkWriteOptions

      Returns Promise<InsertManyResult<T>>

    • Executes a mix of insert/update/delete/upsert operations against a single collection in one round trip. Thin passthrough to the native driver's Collection.bulkWrite().

      Type Parameters

      • T extends Document = Document

      Parameters

      • collectionName: string
      • operations: AnyBulkWriteOperation<T>[]
      • Optionaloptions: BulkWriteOptions

      Returns Promise<BulkWriteResult>

    • Type Parameters

      • T extends Document = Document

      Parameters

      • collectionName: string
      • filter: Filter<T>
      • update: UpdateFilter<T> | Partial<T>
      • Optionaloptions: UpdateOptions

      Returns Promise<UpdateResult<T>>

    • Type Parameters

      • T extends Document = Document

      Parameters

      • collectionName: string
      • filter: Filter<T>
      • update: UpdateFilter<T> | Partial<T>
      • Optionaloptions: UpdateOptions

      Returns Promise<Document | UpdateResult<T>>

    • Atomically finds a document matching filter and updates it, returning it (or null).

      Type Parameters

      • T extends Document = Document

      Parameters

      • collectionName: string
      • filter: Filter<T>
      • update: Document[] | UpdateFilter<T>
      • Optionaloptions: FindOneAndUpdateOptions

      Returns Promise<WithId<T> | null>

    • Type Parameters

      • T extends Document = Document

      Parameters

      • collectionName: string
      • filter: Filter<T>
      • Optionaloptions: DeleteOptions

      Returns Promise<DeleteResult>

    • Type Parameters

      • T extends Document = Document

      Parameters

      • collectionName: string
      • filter: Filter<T>
      • Optionaloptions: DeleteOptions

      Returns Promise<DeleteResult>

    • Atomically finds a document matching filter and deletes it, returning it (or null).

      Type Parameters

      • T extends Document = Document

      Parameters

      • collectionName: string
      • filter: Filter<T>
      • Optionaloptions: FindOneAndDeleteOptions

      Returns Promise<WithId<T> | null>

    • Parameters

      • collectionName: string
      • indexSpec: IndexSpecification
      • Optionaloptions: CreateIndexesOptions

      Returns Promise<string>

    • Parameters

      • collectionName: string
      • indexName: string

      Returns Promise<Document>

    • Lists the indexes defined on collectionName. Fills the asymmetry with createIndex/dropIndex, which have no built-in way to inspect what indexes already exist. Thin passthrough to the native driver's Collection.listIndexes().toArray().

      Parameters

      • collectionName: string
      • Optionaloptions: AbstractCursorOptions

      Returns Promise<Document[]>

    • Explicitly creates collectionName, rather than relying on MongoDB's implicit creation of a collection on first write. This is the only way to create a timeseries collection (options.timeseries: { timeField, metaField?, granularity? }) or a view (options.viewOn + options.pipeline), since both must be declared at creation time - previously this required dropping down to getDb().createCollection(...) directly.

      Type Parameters

      • T extends Document = Document

      Parameters

      • collectionName: string
      • Optionaloptions: CreateCollectionOptions

      Returns Promise<Collection<T>>

    • Lists the collections (and views) defined in the configured database. Thin passthrough to the native driver's Db.listCollections().toArray().

      Parameters

      • filter: Document = {}
      • Optionaloptions: ListCollectionsOptions

      Returns Promise<CollectionInfo[]>

    • Renames oldName to newName. Uses Db.renameCollection() (rather than Collection.rename()) because it's the driver API that stays generic over the document type, matching the rest of this class's typed-collection conventions.

      Type Parameters

      • T extends Document = Document

      Parameters

      • oldName: string
      • newName: string
      • Optionaloptions: RenameOptions

      Returns Promise<Collection<T>>

    • Drops collectionName entirely. Thin passthrough to Collection.drop().

      Parameters

      • collectionName: string
      • Optionaloptions: DropCollectionOptions

      Returns Promise<boolean>

    • Opens a change stream on collectionName, delivering real-time notifications of insert/update/replace/delete operations. Thin passthrough to the native driver's Collection.watch().

      Like withTransaction, this requires a replica set / sharded cluster deployment - a standalone mongod does not support change streams (see README "Known limitations").

      Returns the native ChangeStream, which is itself an async iterable and event emitter (on('change', ...), on('error', ...), close()).

      const stream = store.watch<User>('users', [{ $match: { operationType: 'insert' } }]);
      stream.on('change', (change) => {
      // change.operationType, change.fullDocument, ...
      });
      // later: await stream.close();

      Type Parameters

      • T extends Document = Document

      Parameters

      • collectionName: string
      • Optionalpipeline: Document[]
      • Optionaloptions: ChangeStreamOptions

      Returns ChangeStream<T, ChangeStreamDocument<T>>

    • Starts a new client session (for multi-document transactions).

      Parameters

      • Optionaloptions: ClientSessionOptions

      Returns ClientSession

    • Runs fn inside a MongoDB multi-document transaction. Requires a replica set / mongos deployment - standalone servers do not support transactions (see README "Known limitations").

      Type Parameters

      • T

      Parameters

      • fn: (session: ClientSession) => Promise<T>
      • Optionaloptions: TransactionOptions

      Returns Promise<T>