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

constructor

  • 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

s

Static SYSTEM_COMMAND_COLLECTION

SYSTEM_COMMAND_COLLECTION: string = ...

Static SYSTEM_INDEX_COLLECTION

SYSTEM_INDEX_COLLECTION: string = ...

Static SYSTEM_JS_COLLECTION

SYSTEM_JS_COLLECTION: string = ...

Static SYSTEM_NAMESPACE_COLLECTION

SYSTEM_NAMESPACE_COLLECTION: string = ...

Static SYSTEM_PROFILE_COLLECTION

SYSTEM_PROFILE_COLLECTION: string = ...

Static SYSTEM_USER_COLLECTION

SYSTEM_USER_COLLECTION: string = ...

Accessors

bsonOptions

databaseName

  • get databaseName(): string

logger

namespace

  • get namespace(): string

options

readConcern

readPreference

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

    Returns ReadPreference

slaveOk

  • get slaveOk(): boolean

writeConcern

Methods

addUser

admin

aggregate

  • Execute an aggregation framework pipeline against the database, needs MongoDB >= 3.6

    Type parameters

    Parameters

    • pipeline: Document[] = []

      An array of aggregation stages to be executed

    • Optional options: AggregateOptions

      Optional settings for the command

    Returns AggregationCursor<T>

collection

  • 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

collections

command

createCollection

createIndex

dropCollection

dropDatabase

getLogger

indexInformation

listCollections

profilingLevel

removeUser

renameCollection

  • 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

setProfilingLevel

stats

unref

  • unref(): void
  • Unref all sockets

    deprecated

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

    Returns void

watch

  • 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

    Returns ChangeStream<TSchema>

Generated using TypeDoc