Options
All
  • Public
  • Public/Protected
  • All
Menu

A connection to a MongoDB database.

Hierarchy

  • MongoDatabase

Index

Constructors

constructor

Properties

Protected client

client: MongoClient

Protected Readonly options

options: Partial<IMongoDatabaseOptions>

Methods

Protected checkOptionsOrThrow

  • checkOptionsOrThrow(): void
  • Check if options are valid and throw an error if not.

    Returns void

connect

  • connect(): Promise<void>

count

  • count<T>(collectionName: string, filter?: Filter<T>, options?: CountDocumentsOptions): Promise<number>
  • Does a count on a MongoDB collection.

    Type parameters

    • T: Document

    Parameters

    • collectionName: string

      The collection's name.

    • Optional filter: Filter<T>
    • Optional options: CountDocumentsOptions

    Returns Promise<number>

    The promise with the number of documents.

createIndex

  • createIndex(collectionName: string, indexSpec: IndexSpecification, options?: CreateIndexesOptions): Promise<string>
  • Create an index on a collection.

    Parameters

    • collectionName: string

      The collection's name.

    • indexSpec: IndexSpecification

      The field or spec.

    • Optional options: CreateIndexesOptions

    Returns Promise<string>

    The promise with the result.

deleteMany

  • deleteMany<T>(collectionName: string, filter: Filter<T>, options?: DeleteOptions): Promise<DeleteResult>
  • Delete documents from a MongoDB collection.

    Type parameters

    • T: Document

    Parameters

    • collectionName: string

      The collection's name.

    • filter: Filter<T>

      The filter.

    • Optional options: DeleteOptions

    Returns Promise<DeleteResult>

    The promise with the result.

deleteOne

  • deleteOne<T>(collectionName: string, filter: Filter<T>, options?: DeleteOptions): Promise<DeleteResult>
  • Delete a document from a MongoDB collection.

    Type parameters

    • T: Document

    Parameters

    • collectionName: string

      The collection's name.

    • filter: Filter<T>

      The filter.

    • Optional options: DeleteOptions

    Returns Promise<DeleteResult>

    The promise with the result.

disconnect

  • disconnect(): Promise<void>

find

  • find<T>(collectionName: string, filter: Filter<T>, options?: FindOptions<T>): Promise<WithId<T>[]>
  • Does a find on a MongoDB collection.

    Type parameters

    Parameters

    • collectionName: string

      The collection's name.

    • filter: Filter<T>

      The filter.

    • Optional options: FindOptions<T>

    Returns Promise<WithId<T>[]>

    The promise with the result.

findOne

  • findOne<T>(collectionName: string, filter: Filter<T>, options?: FindOptions<T extends IMongoSchema ? IMongoSchema : T>): Promise<null | T>
  • Does a findOne on a MongoDB collection.

    Type parameters

    Parameters

    • collectionName: string

      The collection's name.

    • filter: Filter<T>

      The filter.

    • Optional options: FindOptions<T extends IMongoSchema ? IMongoSchema : T>

    Returns Promise<null | T>

    The promise with the result or (null) if not found.

getClient

  • getClient(): null | MongoClient
  • Get the MongoDB client.

    Returns null | MongoClient

    The the MongoDB client.

insertMany

  • insertMany<T>(collectionName: string, docs: T[], options?: BulkWriteOptions): Promise<InsertManyResult<T>>
  • Insert many documents into a MongoDB collection.

    Type parameters

    Parameters

    • collectionName: string

      The collection's name.

    • docs: T[]

      The documents to insert.

    • Optional options: BulkWriteOptions

    Returns Promise<InsertManyResult<T>>

    The promise with the result.

insertOne

  • insertOne<T>(collectionName: string, doc: T, options?: BulkWriteOptions): Promise<InsertOneResult<T>>
  • Insert one document into a MongoDB collection.

    Type parameters

    Parameters

    • collectionName: string

      The collection's name.

    • doc: T

      The document to insert.

    • Optional options: BulkWriteOptions

    Returns Promise<InsertOneResult<T>>

    The promise with the result.

updateMany

  • updateMany<T>(collectionName: string, filter: Filter<T>, update: UpdateFilter<T>, options?: UpdateOptions): Promise<Document | UpdateResult<Document>>
  • Update documents in a MongoDB collection.

    Type parameters

    • T: Document

    Parameters

    • collectionName: string

      The collection's name.

    • filter: Filter<T>

      The filter for the documents.

    • update: UpdateFilter<T>

      The update query for the documents.

    • Optional options: UpdateOptions

    Returns Promise<Document | UpdateResult<Document>>

    The promise with the result.

updateOne

  • updateOne<T>(collectionName: string, filter: Filter<T>, update: UpdateFilter<T>, options?: UpdateOptions): Promise<Document | UpdateResult<Document>>
  • Update one document in a MongoDB collection.

    Type parameters

    • T: Document

    Parameters

    • collectionName: string

      The collection's name.

    • filter: Filter<T>

      The filter for the document.

    • update: UpdateFilter<T>

      The update query for the document.

    • Optional options: UpdateOptions

    Returns Promise<Document | UpdateResult<Document>>

    The promise with the result.

withClient

  • Opens a new client connection if it doesn't exist and executes an action on it.

    Type parameters

    • TResult: unknown = any

    Parameters

    Returns Promise<TResult>

    The promise with the result.

Generated using TypeDoc