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

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

  • Execute a command

    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.

    Parameters

    Returns Promise<Document>

  • 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

    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

    Returns Promise<boolean>

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

    Parameters

    Returns Promise<boolean>

  • 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

    Returns Promise<boolean>

  • renameCollection<TSchema>(fromCollection: string, toCollection: string, options?: RenameOptions): Promise<Collection<TSchema>>
  • Rename a collection.

    remarks

    This operation does not inherit options from the MongoClient.

    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

    Returns Promise<Collection<TSchema>>

  • 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