Options
All
  • Public
  • Public/Protected
  • All
Menu

Class Db

The Db class is a class that represents a MongoDB Database.Db类是表示MongoDB数据库的类。

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.数据库的MongoClient。

    • databaseName: string

      The name of the database this instance represents.此实例表示的数据库的名称。

    • Optional options: DbOptions

      Optional settings for Db constructionDb构造的可选设置

    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数据库的当前读取首选项。如果没有为此数据库明确定义,则将从父MongoClient继承

    Returns返回ReadPreference

slaveOk

  • get slaveOk(): boolean

writeConcern

Methods方法

addUser

admin

  • Return the Admin db instance返回管理数据库实例

    Returns返回Admin

aggregate

  • Execute an aggregation framework pipeline against the database, needs MongoDB >= 3.6针对数据库执行聚合框架管道,需要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.返回对MongoDB集合的引用。如果它不存在,将隐式创建它。

    Type parameters类型参数

    Parameters参数

    • name: string

      the collection name we wish to access.我们希望访问的集合名称。

    • options: CollectionOptions = {}

    Returns返回Collection<TSchema>

    return the new Collection instance返回新的集合实例

collections

command

createCollection

createIndex

dropCollection

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

  • Parameters参数

    • name: string
    • callback: Callback<boolean>

    Returns返回void

  • Parameters参数

    Returns返回Promise<boolean>

  • Parameters参数

    Returns返回void

dropDatabase

getLogger

  • Return the db logger返回数据库记录器

    Returns返回Logger

indexInformation

listCollections

profilingLevel

removeUser

  • Remove a user from a database从数据库中删除用户

    Parameters参数

    • username: string

      The username to remove要删除的用户名

    Returns返回Promise<boolean>

  • Parameters参数

    • username: string
    • callback: Callback<boolean>

    Returns返回void

  • Parameters参数

    Returns返回Promise<boolean>

  • Parameters参数

    Returns返回void

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
  • Rename a collection.重命名集合。

    remarks

    This operation does not inherit options from the MongoClient.此操作不会从MongoClient继承选项。

    Type parameters类型参数

    Parameters参数

    • fromCollection: string

      Name of current collection to rename要重命名的当前集合的名称

    • toCollection: string

      New name of of the collection集合的新名称

    Returns返回Promise<Collection<TSchema>>

  • Type parameters类型参数

    Parameters参数

    Returns返回void

  • Type parameters类型参数

    Parameters参数

    • fromCollection: string
    • toCollection: string
    • options: RenameOptions

    Returns返回Promise<Collection<TSchema>>

  • Type parameters类型参数

    Parameters参数

    Returns返回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参数

    • pipeline: Document[] = []

      An array of aggregation pipeline stages through which to pass change stream documents. 用于传递更改流文档的聚合管道阶段数组。This allows for filtering (using $match) and manipulating the change stream documents.这允许过滤(使用$match)和操作更改流文档。

    • options: ChangeStreamOptions = {}

      Optional settings for the command命令的可选设置

    Returns返回ChangeStream<TSchema>

Generated using TypeDoc