API 参考
    正在准备搜索索引...

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

    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']] } }
    })
    索引

    构造函数

    • Creates a new Db instance

      参数

      • client: MongoClient

        The MongoClient for the database.

      • databaseName: string

        The name of the database this instance represents.

      • 可选options: DbOptions

        Optional settings for Db construction

      返回 Db

    属性

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

    访问器

    • get bsonOptions(): BSONSerializeOptions

      返回 BSONSerializeOptions

    • get databaseName(): string

      返回 string

    • get namespace(): string

      返回 string

    • get options(): undefined | DbOptions

      返回 undefined | DbOptions

    • get readConcern(): undefined | ReadConcern

      返回 undefined | ReadConcern

    • get readPreference(): ReadPreference

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

      返回 ReadPreference

    • get secondaryOk(): boolean

      Check if a secondary can be used (because the read preference is not set to primary)

      返回 boolean

    • get writeConcern(): undefined | WriteConcern

      返回 undefined | WriteConcern

    方法

    • Add a user to the database

      参数

      • username: string

        The username for the new user

      • 可选passwordOrOptions: string | AddUserOptions

        An optional password for the new user, or the options for the command

      • 可选options: AddUserOptions

        Optional settings for the command

      返回 Promise<Document>

    • Return the Admin db instance

      返回 Admin

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

      类型参数

      参数

      • 可选pipeline: Document[]

        An array of aggregation stages to be executed

      • 可选options: AggregateOptions

        Optional settings for the command

      返回 AggregationCursor<T>

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

      类型参数

      参数

      返回 Collection<TSchema>

      return the new Collection instance

    • Fetch all collections for the current db.

      参数

      返回 Promise<Collection<Document>[]>

    • Execute a command

      参数

      返回 Promise<Document>

      This command does not inherit options from the MongoClient.

    • Creates an index on the db and collection.

      参数

      • name: string

        Name of the collection to create the index on.

      • indexSpec: IndexSpecification

        Specify the field to index, or an index specification

      • 可选options: CreateIndexesOptions

        Optional settings for the command

      返回 Promise<string>

    • Drop a collection from the database, removing it permanently. New accesses will create a new collection.

      参数

      • name: string

        Name of collection to drop

      • 可选options: DropCollectionOptions

        Optional settings for the command

      返回 Promise<boolean>

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

      参数

      返回 Promise<boolean>

    • Retrieves this collections index info.

      参数

      返回 Promise<Document>

    • Retrieve the current profiling Level for MongoDB

      参数

      返回 Promise<string>

    • Remove a user from a database

      参数

      返回 Promise<boolean>

    • Rename a collection.

      类型参数

      参数

      • fromCollection: string

        Name of current collection to rename

      • toCollection: string

        New name of of the collection

      • 可选options: RenameOptions

        Optional settings for the command

      返回 Promise<Collection<TSchema>>

      This operation does not inherit options from the MongoClient.

    • Set the current profiling level of MongoDB

      参数

      返回 Promise<ProfilingLevel>

    • Get all the db statistics.

      参数

      返回 Promise<Document>

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

      类型参数

      参数

      • 可选pipeline: Document[]

        An array of pipeline stages through which to pass change stream documents. This allows for filtering (using $match) and manipulating the change stream documents.

      • 可选options: ChangeStreamOptions

        Optional settings for the command

      返回 ChangeStream<TSchema, TChange>

      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