Options
All
  • Public
  • Public/Protected
  • All
Menu

Class Db

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

Index

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

    Returns Db

Properties

SYSTEM_COMMAND_COLLECTION: string = CONSTANTS.SYSTEM_COMMAND_COLLECTION
SYSTEM_INDEX_COLLECTION: string = CONSTANTS.SYSTEM_INDEX_COLLECTION
SYSTEM_JS_COLLECTION: string = CONSTANTS.SYSTEM_JS_COLLECTION
SYSTEM_NAMESPACE_COLLECTION: string = CONSTANTS.SYSTEM_NAMESPACE_COLLECTION
SYSTEM_PROFILE_COLLECTION: string = CONSTANTS.SYSTEM_PROFILE_COLLECTION
SYSTEM_USER_COLLECTION: string = CONSTANTS.SYSTEM_USER_COLLECTION

Accessors

  • get databaseName(): string
  • deprecated

    The Legacy Logger is deprecated and will be removed in the next major version.

    Returns Logger

  • get namespace(): string
  • 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

  • get slaveOk(): boolean
  • slaveOk specified

    deprecated

    Use secondaryOk instead

    Returns boolean

Methods

  • Returns a reference to a MongoDB Collection. If it does not exist it will be created implicitly.

    Type parameters

    Parameters

    Returns Collection<TSchema>

    return the new Collection instance

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

    Parameters

    • name: string

      Name of collection to drop

    Returns Promise<boolean>

  • Parameters

    Returns Promise<boolean>

  • deprecated

    Callbacks are deprecated and will be removed in the next major version. See mongodb-legacy for migration assistance

    Parameters

    Returns void

  • deprecated

    Callbacks are deprecated and will be removed in the next major version. See mongodb-legacy for migration assistance

    Parameters

    Returns void

  • Return the db logger

    deprecated

    The Legacy Logger is deprecated and will be removed in the next major version.

    Returns Logger

  • renameCollection<TSchema>(fromCollection: string, toCollection: string): Promise<Collection<TSchema>>
  • renameCollection<TSchema>(fromCollection: string, toCollection: string, options: RenameOptions): Promise<Collection<TSchema>>
  • renameCollection<TSchema>(fromCollection: string, toCollection: string, callback: Callback<Collection<TSchema>>): void
  • renameCollection<TSchema>(fromCollection: string, toCollection: string, options: RenameOptions, callback: Callback<Collection<TSchema>>): void
  • unref(): void
  • Unref all sockets

    deprecated

    This function is deprecated and will be removed in the next major version.

    Returns void

  • 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.

    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

    Type parameters

    Parameters

    Returns ChangeStream<TSchema, TChange>

Generated using TypeDoc