Options
All
  • Public
  • Public/Protected
  • All
Menu

Class Db

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

example
const { MongoClient } = require('mongodb');
// Connection url
const url = 'mongodb://localhost:27017';
// Database Name
const dbName = 'test';
// Connect using MongoClient
MongoClient.connect(url, function(err, client) {
// Select the database by name
const testDb = client.db(dbName);
client.close();
});

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

  • 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

  • renameCollection<TSchema>(fromCollection: string, toCollection: string): Promise<Collection<TSchema>>
  • renameCollection<TSchema>(fromCollection: string, toCollection: string, callback: Callback<Collection<TSchema>>): void
  • renameCollection<TSchema>(fromCollection: string, toCollection: string, options: RenameOptions): Promise<Collection<TSchema>>
  • 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 usecases:

    • 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