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

internal

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

collection

  • collection<TSchema>(name: string): Collection<TSchema>
  • 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.

    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

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