Skip to content

@142vip.nest typeorm.类.Db

mmdapl edited this page Oct 8, 2026 · 1 revision

API 参考 / @142vip/nest-typeorm / Db

类: Db

定义于: node_modules/.pnpm/typeorm@0.3.30_ioredis@5.6.0_mongodb@6.19.0_mssql@11.0.1_mysql2@3.15.1_pg@8.11.3_ts-node@10.9_6zvdq3z7xgsf663jy3j6oymho4/node_modules/typeorm/driver/mongodb/typings.d.ts:2345

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

构造函数

构造函数

new Db(client, databaseName, options?): Db

定义于: node_modules/.pnpm/typeorm@0.3.30_ioredis@5.6.0_mongodb@6.19.0_mssql@11.0.1_mysql2@3.15.1_pg@8.11.3_ts-node@10.9_6zvdq3z7xgsf663jy3j6oymho4/node_modules/typeorm/driver/mongodb/typings.d.ts:2359

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

static SYSTEM_COMMAND_COLLECTION: string

定义于: node_modules/.pnpm/typeorm@0.3.30_ioredis@5.6.0_mongodb@6.19.0_mssql@11.0.1_mysql2@3.15.1_pg@8.11.3_ts-node@10.9_6zvdq3z7xgsf663jy3j6oymho4/node_modules/typeorm/driver/mongodb/typings.d.ts:2350


SYSTEM_INDEX_COLLECTION

static SYSTEM_INDEX_COLLECTION: string

定义于: node_modules/.pnpm/typeorm@0.3.30_ioredis@5.6.0_mongodb@6.19.0_mssql@11.0.1_mysql2@3.15.1_pg@8.11.3_ts-node@10.9_6zvdq3z7xgsf663jy3j6oymho4/node_modules/typeorm/driver/mongodb/typings.d.ts:2347


SYSTEM_JS_COLLECTION

static SYSTEM_JS_COLLECTION: string

定义于: node_modules/.pnpm/typeorm@0.3.30_ioredis@5.6.0_mongodb@6.19.0_mssql@11.0.1_mysql2@3.15.1_pg@8.11.3_ts-node@10.9_6zvdq3z7xgsf663jy3j6oymho4/node_modules/typeorm/driver/mongodb/typings.d.ts:2351


SYSTEM_NAMESPACE_COLLECTION

static SYSTEM_NAMESPACE_COLLECTION: string

定义于: node_modules/.pnpm/typeorm@0.3.30_ioredis@5.6.0_mongodb@6.19.0_mssql@11.0.1_mysql2@3.15.1_pg@8.11.3_ts-node@10.9_6zvdq3z7xgsf663jy3j6oymho4/node_modules/typeorm/driver/mongodb/typings.d.ts:2346


SYSTEM_PROFILE_COLLECTION

static SYSTEM_PROFILE_COLLECTION: string

定义于: node_modules/.pnpm/typeorm@0.3.30_ioredis@5.6.0_mongodb@6.19.0_mssql@11.0.1_mysql2@3.15.1_pg@8.11.3_ts-node@10.9_6zvdq3z7xgsf663jy3j6oymho4/node_modules/typeorm/driver/mongodb/typings.d.ts:2348


SYSTEM_USER_COLLECTION

static SYSTEM_USER_COLLECTION: string

定义于: node_modules/.pnpm/typeorm@0.3.30_ioredis@5.6.0_mongodb@6.19.0_mssql@11.0.1_mysql2@3.15.1_pg@8.11.3_ts-node@10.9_6zvdq3z7xgsf663jy3j6oymho4/node_modules/typeorm/driver/mongodb/typings.d.ts:2349

访问器

bsonOptions

Getter 签名

get bsonOptions(): BSONSerializeOptions

定义于: node_modules/.pnpm/typeorm@0.3.30_ioredis@5.6.0_mongodb@6.19.0_mssql@11.0.1_mysql2@3.15.1_pg@8.11.3_ts-node@10.9_6zvdq3z7xgsf663jy3j6oymho4/node_modules/typeorm/driver/mongodb/typings.d.ts:2372

返回

BSONSerializeOptions


databaseName

Getter 签名

get databaseName(): string

定义于: node_modules/.pnpm/typeorm@0.3.30_ioredis@5.6.0_mongodb@6.19.0_mssql@11.0.1_mysql2@3.15.1_pg@8.11.3_ts-node@10.9_6zvdq3z7xgsf663jy3j6oymho4/node_modules/typeorm/driver/mongodb/typings.d.ts:2360

返回

string


namespace

Getter 签名

get namespace(): string

定义于: node_modules/.pnpm/typeorm@0.3.30_ioredis@5.6.0_mongodb@6.19.0_mssql@11.0.1_mysql2@3.15.1_pg@8.11.3_ts-node@10.9_6zvdq3z7xgsf663jy3j6oymho4/node_modules/typeorm/driver/mongodb/typings.d.ts:2374

返回

string


options

Getter 签名

get options(): DbOptions | undefined

定义于: node_modules/.pnpm/typeorm@0.3.30_ioredis@5.6.0_mongodb@6.19.0_mssql@11.0.1_mysql2@3.15.1_pg@8.11.3_ts-node@10.9_6zvdq3z7xgsf663jy3j6oymho4/node_modules/typeorm/driver/mongodb/typings.d.ts:2361

返回

DbOptions | undefined


readConcern

Getter 签名

get readConcern(): ReadConcern | undefined

定义于: node_modules/.pnpm/typeorm@0.3.30_ioredis@5.6.0_mongodb@6.19.0_mssql@11.0.1_mysql2@3.15.1_pg@8.11.3_ts-node@10.9_6zvdq3z7xgsf663jy3j6oymho4/node_modules/typeorm/driver/mongodb/typings.d.ts:2366

返回

ReadConcern | undefined


readPreference

Getter 签名

get readPreference(): ReadPreference

定义于: node_modules/.pnpm/typeorm@0.3.30_ioredis@5.6.0_mongodb@6.19.0_mssql@11.0.1_mysql2@3.15.1_pg@8.11.3_ts-node@10.9_6zvdq3z7xgsf663jy3j6oymho4/node_modules/typeorm/driver/mongodb/typings.d.ts:2371

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

返回

ReadPreference


secondaryOk

Getter 签名

get secondaryOk(): boolean

定义于: node_modules/.pnpm/typeorm@0.3.30_ioredis@5.6.0_mongodb@6.19.0_mssql@11.0.1_mysql2@3.15.1_pg@8.11.3_ts-node@10.9_6zvdq3z7xgsf663jy3j6oymho4/node_modules/typeorm/driver/mongodb/typings.d.ts:2365

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

返回

boolean


writeConcern

Getter 签名

get writeConcern(): WriteConcern | undefined

定义于: node_modules/.pnpm/typeorm@0.3.30_ioredis@5.6.0_mongodb@6.19.0_mssql@11.0.1_mysql2@3.15.1_pg@8.11.3_ts-node@10.9_6zvdq3z7xgsf663jy3j6oymho4/node_modules/typeorm/driver/mongodb/typings.d.ts:2373

返回

WriteConcern | undefined

方法

addUser()

addUser(username, passwordOrOptions?, options?): Promise<Document>

定义于: node_modules/.pnpm/typeorm@0.3.30_ioredis@5.6.0_mongodb@6.19.0_mssql@11.0.1_mysql2@3.15.1_pg@8.11.3_ts-node@10.9_6zvdq3z7xgsf663jy3j6oymho4/node_modules/typeorm/driver/mongodb/typings.d.ts:2473

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>


admin()

admin(): Admin

定义于: node_modules/.pnpm/typeorm@0.3.30_ioredis@5.6.0_mongodb@6.19.0_mssql@11.0.1_mysql2@3.15.1_pg@8.11.3_ts-node@10.9_6zvdq3z7xgsf663jy3j6oymho4/node_modules/typeorm/driver/mongodb/typings.d.ts:2401

Return the Admin db instance

返回

Admin


aggregate()

aggregate<T>(pipeline?, options?): AggregationCursor<T>

定义于: node_modules/.pnpm/typeorm@0.3.30_ioredis@5.6.0_mongodb@6.19.0_mssql@11.0.1_mysql2@3.15.1_pg@8.11.3_ts-node@10.9_6zvdq3z7xgsf663jy3j6oymho4/node_modules/typeorm/driver/mongodb/typings.d.ts:2399

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

类型参数

T

T extends Document = Document

参数

pipeline?

Document[]

An array of aggregation stages to be executed

options?

AggregateOptions

Optional settings for the command

返回

AggregationCursor<T>


collection()

collection<TSchema>(name, options?): Collection<TSchema>

定义于: node_modules/.pnpm/typeorm@0.3.30_ioredis@5.6.0_mongodb@6.19.0_mssql@11.0.1_mysql2@3.15.1_pg@8.11.3_ts-node@10.9_6zvdq3z7xgsf663jy3j6oymho4/node_modules/typeorm/driver/mongodb/typings.d.ts:2408

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

类型参数

TSchema

TSchema extends Document = Document

参数

name

string

the collection name we wish to access.

options?

CollectionOptions

返回

Collection<TSchema>

return the new Collection instance


collections()

collections(options?): Promise<Collection<Document>[]>

定义于: node_modules/.pnpm/typeorm@0.3.30_ioredis@5.6.0_mongodb@6.19.0_mssql@11.0.1_mysql2@3.15.1_pg@8.11.3_ts-node@10.9_6zvdq3z7xgsf663jy3j6oymho4/node_modules/typeorm/driver/mongodb/typings.d.ts:2457

Fetch all collections for the current db.

参数

options?

ListCollectionsOptions

Optional settings for the command

返回

Promise<Collection<Document>[]>


command()

command(command, options?): Promise<Document>

定义于: node_modules/.pnpm/typeorm@0.3.30_ioredis@5.6.0_mongodb@6.19.0_mssql@11.0.1_mysql2@3.15.1_pg@8.11.3_ts-node@10.9_6zvdq3z7xgsf663jy3j6oymho4/node_modules/typeorm/driver/mongodb/typings.d.ts:2392

Execute a command

参数

command

Document

The command to run

options?

CommandOperationOptions

Optional settings for the command

返回

Promise<Document>

备注

This command does not inherit options from the MongoClient.


createCollection()

createCollection<TSchema>(name, options?): Promise<Collection<TSchema>>

定义于: node_modules/.pnpm/typeorm@0.3.30_ioredis@5.6.0_mongodb@6.19.0_mssql@11.0.1_mysql2@3.15.1_pg@8.11.3_ts-node@10.9_6zvdq3z7xgsf663jy3j6oymho4/node_modules/typeorm/driver/mongodb/typings.d.ts:2382

Create a new collection on a server with the specified options. Use this to create capped collections. More information about command options available at https://www.mongodb.com/docs/manual/reference/command/create/

类型参数

TSchema

TSchema extends Document = Document

参数

name

string

The name of the collection to create

options?

CreateCollectionOptions

Optional settings for the command

返回

Promise<Collection<TSchema>>


createIndex()

createIndex(name, indexSpec, options?): Promise<string>

定义于: node_modules/.pnpm/typeorm@0.3.30_ioredis@5.6.0_mongodb@6.19.0_mssql@11.0.1_mysql2@3.15.1_pg@8.11.3_ts-node@10.9_6zvdq3z7xgsf663jy3j6oymho4/node_modules/typeorm/driver/mongodb/typings.d.ts:2465

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>


dropCollection()

dropCollection(name, options?): Promise<boolean>

定义于: node_modules/.pnpm/typeorm@0.3.30_ioredis@5.6.0_mongodb@6.19.0_mssql@11.0.1_mysql2@3.15.1_pg@8.11.3_ts-node@10.9_6zvdq3z7xgsf663jy3j6oymho4/node_modules/typeorm/driver/mongodb/typings.d.ts:2445

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>


dropDatabase()

dropDatabase(options?): Promise<boolean>

定义于: node_modules/.pnpm/typeorm@0.3.30_ioredis@5.6.0_mongodb@6.19.0_mssql@11.0.1_mysql2@3.15.1_pg@8.11.3_ts-node@10.9_6zvdq3z7xgsf663jy3j6oymho4/node_modules/typeorm/driver/mongodb/typings.d.ts:2451

Drop a database, removing it permanently from the server.

参数

options?

CommandOperationOptions

Optional settings for the command

返回

Promise<boolean>


indexInformation()

indexInformation(name, options?): Promise<Document>

定义于: node_modules/.pnpm/typeorm@0.3.30_ioredis@5.6.0_mongodb@6.19.0_mssql@11.0.1_mysql2@3.15.1_pg@8.11.3_ts-node@10.9_6zvdq3z7xgsf663jy3j6oymho4/node_modules/typeorm/driver/mongodb/typings.d.ts:2500

Retrieves this collections index info.

参数

name

string

The name of the collection.

options?

IndexInformationOptions

Optional settings for the command

返回

Promise<Document>


listCollections()

调用签名

listCollections(filter, options): ListCollectionsCursor<Pick<CollectionInfo, "type" | "name">>

定义于: node_modules/.pnpm/typeorm@0.3.30_ioredis@5.6.0_mongodb@6.19.0_mssql@11.0.1_mysql2@3.15.1_pg@8.11.3_ts-node@10.9_6zvdq3z7xgsf663jy3j6oymho4/node_modules/typeorm/driver/mongodb/typings.d.ts:2421

List all collections of this database with optional filter

参数
filter

Document

Query to filter collections by

options

ListCollectionsOptions & object

Optional settings for the command

返回

ListCollectionsCursor<Pick<CollectionInfo, "type" | "name">>

调用签名

listCollections(filter, options): ListCollectionsCursor<CollectionInfo>

定义于: node_modules/.pnpm/typeorm@0.3.30_ioredis@5.6.0_mongodb@6.19.0_mssql@11.0.1_mysql2@3.15.1_pg@8.11.3_ts-node@10.9_6zvdq3z7xgsf663jy3j6oymho4/node_modules/typeorm/driver/mongodb/typings.d.ts:2424

List all collections of this database with optional filter

参数
filter

Document

Query to filter collections by

options

ListCollectionsOptions & object

Optional settings for the command

返回

ListCollectionsCursor<CollectionInfo>

调用签名

listCollections<T>(filter?, options?): ListCollectionsCursor<T>

定义于: node_modules/.pnpm/typeorm@0.3.30_ioredis@5.6.0_mongodb@6.19.0_mssql@11.0.1_mysql2@3.15.1_pg@8.11.3_ts-node@10.9_6zvdq3z7xgsf663jy3j6oymho4/node_modules/typeorm/driver/mongodb/typings.d.ts:2427

List all collections of this database with optional filter

类型参数
T

T extends CollectionInfo | Pick<CollectionInfo, "type" | "name"> = CollectionInfo | Pick<CollectionInfo, "type" | "name">

参数
filter?

Document

Query to filter collections by

options?

ListCollectionsOptions

Optional settings for the command

返回

ListCollectionsCursor<T>


profilingLevel()

profilingLevel(options?): Promise<string>

定义于: node_modules/.pnpm/typeorm@0.3.30_ioredis@5.6.0_mongodb@6.19.0_mssql@11.0.1_mysql2@3.15.1_pg@8.11.3_ts-node@10.9_6zvdq3z7xgsf663jy3j6oymho4/node_modules/typeorm/driver/mongodb/typings.d.ts:2493

Retrieve the current profiling Level for MongoDB

参数

options?

CommandOperationOptions

Optional settings for the command

返回

Promise<string>


removeUser()

removeUser(username, options?): Promise<boolean>

定义于: node_modules/.pnpm/typeorm@0.3.30_ioredis@5.6.0_mongodb@6.19.0_mssql@11.0.1_mysql2@3.15.1_pg@8.11.3_ts-node@10.9_6zvdq3z7xgsf663jy3j6oymho4/node_modules/typeorm/driver/mongodb/typings.d.ts:2480

Remove a user from a database

参数

username

string

The username to remove

options?

CommandOperationOptions

Optional settings for the command

返回

Promise<boolean>


renameCollection()

renameCollection<TSchema>(fromCollection, toCollection, options?): Promise<Collection<TSchema>>

定义于: node_modules/.pnpm/typeorm@0.3.30_ioredis@5.6.0_mongodb@6.19.0_mssql@11.0.1_mysql2@3.15.1_pg@8.11.3_ts-node@10.9_6zvdq3z7xgsf663jy3j6oymho4/node_modules/typeorm/driver/mongodb/typings.d.ts:2438

Rename a collection.

类型参数

TSchema

TSchema extends Document = Document

参数

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.


setProfilingLevel()

setProfilingLevel(level, options?): Promise<ProfilingLevel>

定义于: node_modules/.pnpm/typeorm@0.3.30_ioredis@5.6.0_mongodb@6.19.0_mssql@11.0.1_mysql2@3.15.1_pg@8.11.3_ts-node@10.9_6zvdq3z7xgsf663jy3j6oymho4/node_modules/typeorm/driver/mongodb/typings.d.ts:2487

Set the current profiling level of MongoDB

参数

level

ProfilingLevel

The new profiling level (off, slow_only, all).

options?

CommandOperationOptions

Optional settings for the command

返回

Promise<ProfilingLevel>


stats()

stats(options?): Promise<Document>

定义于: node_modules/.pnpm/typeorm@0.3.30_ioredis@5.6.0_mongodb@6.19.0_mssql@11.0.1_mysql2@3.15.1_pg@8.11.3_ts-node@10.9_6zvdq3z7xgsf663jy3j6oymho4/node_modules/typeorm/driver/mongodb/typings.d.ts:2414

Get all the db statistics.

参数

options?

DbStatsOptions

Optional settings for the command

返回

Promise<Document>


watch()

watch<TSchema, TChange>(pipeline?, options?): ChangeStream<TSchema, TChange>

定义于: node_modules/.pnpm/typeorm@0.3.30_ioredis@5.6.0_mongodb@6.19.0_mssql@11.0.1_mysql2@3.15.1_pg@8.11.3_ts-node@10.9_6zvdq3z7xgsf663jy3j6oymho4/node_modules/typeorm/driver/mongodb/typings.d.ts:2516

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.

类型参数

TSchema

TSchema extends Document = Document

Type of the data being detected by the change stream

TChange

TChange extends Document = ChangeStreamDocument<TSchema>

Type of the whole change stream document emitted

参数

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

Clone this wiki locally