Options
All
  • Public
  • Public/Protected
  • All
Menu

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

Properties

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

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

Accessors

  • get databaseName(): string
  • Returns string

  • Returns DbOptions

  • get slaveOk(): boolean
  • slaveOk specified

    deprecated

    Use secondaryOk instead

    Returns boolean

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

    Returns boolean

  • Returns ReadConcern

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

    Returns ReadPreference

  • Returns WriteConcern

  • get namespace(): string
  • Returns string

  • Returns Logger

Methods

  • Return the Admin db instance

    Returns Admin

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

    Type Parameters

    Parameters

    • name: string

      the collection name we wish to access.

    • Optional options: CollectionOptions

    Returns <internal>.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>

  • deprecated

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

    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

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

    Returns Promise<boolean>

  • deprecated

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

    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

  • 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

    Returns Promise<string>

  • deprecated

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

    Parameters

    Returns Promise<string>

  • 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

  • Add a user to the database

    Parameters

    • username: string

      The username for the new user

    Returns Promise<<internal>.Document>

  • deprecated

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

    Parameters

    • username: string
    • password: string

    Returns Promise<<internal>.Document>

  • deprecated

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

    Parameters

    Returns Promise<<internal>.Document>

  • deprecated

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

    Parameters

    Returns Promise<<internal>.Document>

  • 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

  • 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

  • Remove a user from a database

    Parameters

    • username: string

      The username to remove

    Returns Promise<boolean>

  • deprecated

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

    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

  • Retrieve the current profiling Level for MongoDB

    Returns Promise<string>

  • deprecated

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

    Parameters

    Returns Promise<string>

  • 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

  • Retrieves this collections index info.

    Parameters

    • name: string

      The name of the collection.

    Returns Promise<<internal>.Document>

  • deprecated

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

    Parameters

    Returns Promise<<internal>.Document>

  • 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

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

  • Return the db logger

    Returns Logger

Generated using TypeDoc