The Db class is a class that represents a MongoDB Database.

Example

import { MongoClient } from 'mongodb';

interface Pet {
name: string;
kind: 'dog' | 'cat' | 'fish';
}

const client = new MongoClient('mongodb://localhost:27017');
const db = client.db();

// Create a collection that validates our union
await db.createCollection<Pet>('pets', {
validator: { $expr: { $in: ['$kind', ['dog', 'cat', 'fish']] } }
})

Hierarchy

  • Db

Constructors

  • Creates a new Db instance

    Parameters

    • client: MongoClient

      The MongoClient for the database.

    • databaseName: string

      The name of the database this instance represents.

    • Optional options: DbOptions

      Optional settings for Db construction

      Optional

    Returns Db

Properties

SYSTEM_COMMAND_COLLECTION: string
SYSTEM_INDEX_COLLECTION: string
SYSTEM_JS_COLLECTION: string
SYSTEM_NAMESPACE_COLLECTION: string
SYSTEM_PROFILE_COLLECTION: string
SYSTEM_USER_COLLECTION: string

Accessors

  • get databaseName(): string
  • Returns string

  • get namespace(): string
  • Returns string

  • get options(): DbOptions
  • Returns DbOptions

  • get readConcern(): ReadConcern
  • Returns ReadConcern

  • get readPreference(): ReadPreference
  • The current readPreference of the Db. If not explicitly defined for this Db, will be inherited from the parent MongoClient

    Returns ReadPreference

  • get secondaryOk(): boolean
  • Check if a secondary can be used (because the read preference is not set to primary)

    Returns boolean

Methods

  • Return the Admin db instance

    Returns Admin

  • Execute a command

    Parameters

    Returns Promise<mongoose.mongo.BSON.Document>

    Remarks

    This command does not inherit options from the MongoClient.

    The driver will ensure the following fields are attached to the command sent to the server:

    • lsid - sourced from an implicit session or options.session
    • $readPreference - defaults to primary or can be configured by options.readPreference
    • $db - sourced from the name of this database

    If the client has a serverApi setting:

    • apiVersion
    • apiStrict
    • apiDeprecationErrors

    When in a transaction:

    • readConcern - sourced from readConcern set on the TransactionOptions
    • writeConcern - sourced from writeConcern set on the TransactionOptions

    Attaching any of the above fields to the command will have no effect as the driver will overwrite the value.

  • Creates an index on the db and collection.

    Parameters

    • name: string

      Name of the collection to create the index on.

    • indexSpec: IndexSpecification

      Specify the field to index, or an index specification

    • Optional options: CreateIndexesOptions

      Optional settings for the command

      Optional

    Returns Promise<string>

  • Drop a collection from the database, removing it permanently. New accesses will create a new collection.

    Parameters

    • name: string

      Name of collection to drop

    • Optional options: DropCollectionOptions

      Optional settings for the command

      Optional

    Returns Promise<boolean>

  • Drop a database, removing it permanently from the server.

    Parameters

    Returns Promise<boolean>

  • Retrieves this collections index info.

    Parameters

    • name: string

      The name of the collection.

    • Optional options: IndexInformationOptions

      Optional settings for the command

      Optional

    Returns Promise<mongoose.mongo.BSON.Document>

  • Retrieve the current profiling Level for MongoDB

    Parameters

    Returns Promise<string>

  • Remove a user from a database

    Parameters

    • username: string

      The username to remove

    • Optional options: CommandOperationOptions

      Optional settings for the command

      Optional

    Returns Promise<boolean>

  • Rename a collection.

    Type Parameters

    Parameters

    • fromCollection: string

      Name of current collection to rename

    • toCollection: string

      New name of of the collection

    • Optional options: RenameOptions

      Optional settings for the command

      Optional

    Returns Promise<mongoose.mongo.Collection<TSchema>>

    Remarks

    This operation does not inherit options from the MongoClient.

  • A low level cursor API providing basic driver functionality:

    • ClientSession management
    • ReadPreference for server selection
    • Running getMores automatically when a local batch is exhausted

    Parameters

    Returns RunCommandCursor

  • Create a new Change Stream, watching for new changes (insertions, updates, replacements, deletions, and invalidations) in this database. Will ignore all changes to system collections.

    Type Parameters

    Parameters

    • Optional pipeline: mongoose.mongo.BSON.Document[]

      An array of pipeline stages through which to pass change stream documents. This allows for filtering (using $match) and manipulating the change stream documents.

      Optional
    • Optional options: ChangeStreamOptions

      Optional settings for the command

      Optional

    Returns ChangeStream<TSchema, TChange>

    Remarks

    watch() accepts two generic arguments for distinct use cases:

    • The first is to provide the schema that may be defined for all the collections within this database
    • The second is to override the shape of the change stream document entirely, if it is not provided the type will default to ChangeStreamDocument of the first argument

Generated using TypeDoc