Skip to content

@142vip.nest typeorm.类.Collection

mmdapl edited this page Oct 8, 2026 · 1 revision

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

类: 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:1467

The Collection class is an internal class that embodies a MongoDB collection allowing for insert/find/update/delete and other command operation on that MongoDB collection.

COLLECTION Cannot directly be instantiated

示例

import { MongoClient } from 'mongodb'

interface Pet {
  name: string
  kind: 'dog' | 'cat' | 'fish'
}

const client = new MongoClient('mongodb://localhost:27017')
const pets = client.db().collection<Pet>('pets')

const petCursor = pets.find()

for await (const pet of petCursor) {
  console.log(`${pet.name} is a ${pet.kind}!`)
}

类型参数

TSchema

TSchema extends Document = Document

构造函数

构造函数

new Collection<TSchema>(): Collection<TSchema>

返回

Collection<TSchema>

访问器

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:1490

返回

BSONSerializeOptions


collectionName

Getter 签名

get collectionName(): 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:1475

The name of this collection

返回

string


dbName

Getter 签名

get dbName(): 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:1471

The name of the database this collection belongs to

返回

string


hint

Getter 签名

get hint(): Hint | 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:1497

The current index hint for the collection

返回

Hint | undefined

Setter 签名

set hint(v): void

定义于: 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:1498

参数
v

Hint | undefined

返回

void


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:1479

The namespace of this collection, in the format ${this.dbName}.${this.collectionName}

返回

string


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:1484

The current readConcern of the collection. If not explicitly defined for this collection, will be inherited from the parent DB

返回

ReadConcern | undefined


readPreference

Getter 签名

get readPreference(): ReadPreference | 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:1489

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

返回

ReadPreference | undefined


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:1495

The current writeConcern of the collection. If not explicitly defined for this collection, will be inherited from the parent DB

返回

WriteConcern | undefined

方法

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:1810

Execute an aggregation framework pipeline against the collection, needs MongoDB \>= 2.2

类型参数

T

T extends Document = Document

参数

pipeline?

Document[]

An array of aggregation pipelines to execute

options?

AggregateOptions

Optional settings for the command

返回

AggregationCursor<T>


bulkWrite()

bulkWrite(operations, options?): Promise<BulkWriteResult>

定义于: 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:1536

Perform a bulkWrite operation without a fluent API

Legal operation types are

  • insertOne
  • replaceOne
  • updateOne
  • updateMany
  • deleteOne
  • deleteMany

If documents passed in do not contain the _id field, one will be added to each of the documents missing it by the driver, mutating the document. This behavior can be overridden by setting the forceServerObjectId flag.

参数

operations

AnyBulkWriteOperation<TSchema>[]

Bulk operations to perform

options?

BulkWriteOptions

Optional settings for the command

返回

Promise<BulkWriteResult>

抛出

MongoDriverError if operations is not an array


count()

count(filter?, options?): Promise<number>

定义于: 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:1880

An estimated count of matching documents in the db to a filter.

NOTE: This method has been deprecated, since it does not provide an accurate count of the documents in a collection. To obtain an accurate count of documents in the collection, use countDocuments. To obtain an estimated count of all documents in the collection, use estimatedDocumentCount.

参数

filter?

Filter<TSchema>

The filter for the count.

options?

CountOptions

Optional settings for the command

返回

Promise<number>

已被弃用

use countDocuments or estimatedDocumentCount instead


countDocuments()

countDocuments(filter?, options?): Promise<number>

定义于: 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:1755

Gets the number of documents matching the filter. For a fast count of the total documents in a collection see estimatedDocumentCount. Note: When migrating from count to countDocuments the following query operators must be replaced:

Operator Replacement
$where $expr
$near $geoWithin with $center
$nearSphere $geoWithin with $centerSphere

参数

filter?

Document

The filter for the count

options?

CountDocumentsOptions

Optional settings for the command

返回

Promise<number>

参阅


createIndex()

createIndex(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:1651

Creates an index on the db and collection collection.

参数

indexSpec

IndexSpecification

The field name or index specification to create an index for

options?

CreateIndexesOptions

Optional settings for the command

返回

Promise<string>

示例

const collection = client.db('foo').collection('bar')

await collection.createIndex({ a: 1, b: -1 })

// Alternate syntax for { c: 1, d: -1 } that ensures order of indexes
await collection.createIndex([[c, 1], [d, -1]])

// Equivalent to { e: 1 }
await collection.createIndex('e')

// Equivalent to { f: 1, g: 1 }
await collection.createIndex(['f', 'g'])

// Equivalent to { h: 1, i: -1 }
await collection.createIndex([{ h: 1 }, { i: -1 }])

// Equivalent to { j: 1, k: -1, l: 2d }
await collection.createIndex(['j', ['k', -1], { l: '2d' }])

createIndexes()

createIndexes(indexSpecs, 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:1683

Creates multiple indexes in the collection, this method is only supported for MongoDB 2.6 or higher. Earlier version of MongoDB will throw a command not supported error.

Note: Unlike createIndex, this function takes in raw index specifications. Index specifications are defined here.

参数

indexSpecs

IndexDescription[]

An array of index specifications to be created

options?

CreateIndexesOptions

Optional settings for the command

返回

Promise<string[]>

示例

const collection = client.db('foo').collection('bar');
await collection.createIndexes([
  // Simple index on field fizz
  {
    key: { fizz: 1 },
  }
  // wildcard index
  {
    key: { '$**': 1 }
  },
  // named index on darmok and jalad
  {
    key: { darmok: 1, jalad: -1 }
    name: 'tanagra'
  }
]);

deleteMany()

deleteMany(filter?, options?): Promise<DeleteResult>

定义于: 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:1574

Delete multiple documents from a collection

参数

filter?

Filter<TSchema>

The filter used to select the documents to remove

options?

DeleteOptions

Optional settings for the command

返回

Promise<DeleteResult>


deleteOne()

deleteOne(filter?, options?): Promise<DeleteResult>

定义于: 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:1567

Delete a document from a collection

参数

filter?

Filter<TSchema>

The filter used to select the document to remove

options?

DeleteOptions

Optional settings for the command

返回

Promise<DeleteResult>


distinct()

调用签名

distinct<Key>(key): Promise<Flatten<WithId<TSchema>[Key]>[]>

定义于: 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:1763

The distinct command returns a list of distinct values for the given key across a collection.

类型参数
Key

Key extends string | number | symbol

参数
key

Key

Field of the document to find distinct values for

返回

Promise<Flatten<WithId<TSchema>[Key]>[]>

调用签名

distinct<Key>(key, filter): Promise<Flatten<WithId<TSchema>[Key]>[]>

定义于: 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:1764

The distinct command returns a list of distinct values for the given key across a collection.

类型参数
Key

Key extends string | number | symbol

参数
key

Key

Field of the document to find distinct values for

filter

Filter<TSchema>

The filter for filtering the set of documents to which we apply the distinct filter.

返回

Promise<Flatten<WithId<TSchema>[Key]>[]>

调用签名

distinct<Key>(key, filter, options): Promise<Flatten<WithId<TSchema>[Key]>[]>

定义于: 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:1765

The distinct command returns a list of distinct values for the given key across a collection.

类型参数
Key

Key extends string | number | symbol

参数
key

Key

Field of the document to find distinct values for

filter

Filter<TSchema>

The filter for filtering the set of documents to which we apply the distinct filter.

options

CommandOperationOptions

Optional settings for the command

返回

Promise<Flatten<WithId<TSchema>[Key]>[]>

调用签名

distinct(key): Promise<any[]>

定义于: 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:1766

The distinct command returns a list of distinct values for the given key across a collection.

参数
key

string

Field of the document to find distinct values for

返回

Promise<any[]>

调用签名

distinct(key, filter): Promise<any[]>

定义于: 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:1767

The distinct command returns a list of distinct values for the given key across a collection.

参数
key

string

Field of the document to find distinct values for

filter

Filter<TSchema>

The filter for filtering the set of documents to which we apply the distinct filter.

返回

Promise<any[]>

调用签名

distinct(key, filter, options): Promise<any[]>

定义于: 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:1768

The distinct command returns a list of distinct values for the given key across a collection.

参数
key

string

Field of the document to find distinct values for

filter

Filter<TSchema>

The filter for filtering the set of documents to which we apply the distinct filter.

options

CommandOperationOptions

Optional settings for the command

返回

Promise<any[]>


drop()

drop(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:1590

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

参数

options?

DropCollectionOptions

Optional settings for the command

返回

Promise<boolean>


dropIndex()

dropIndex(indexName, 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:1690

Drops an index from this collection.

参数

indexName

string

Name of the index to drop.

options?

CommandOperationOptions

Optional settings for the command

返回

Promise<Document>


dropIndexes()

dropIndexes(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:1696

Drops all indexes from this collection.

参数

options?

CommandOperationOptions

Optional settings for the command

返回

Promise<Document>


estimatedDocumentCount()

estimatedDocumentCount(options?): Promise<number>

定义于: 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:1729

Gets an estimate of the count of documents in a collection using collection metadata. This will always run a count command on all server versions.

due to an oversight in versions 5.0.0-5.0.8 of MongoDB, the count command, which estimatedDocumentCount uses in its implementation, was not included in v1 of the Stable API, and so users of the Stable API with estimatedDocumentCount are recommended to upgrade their server version to 5.0.9+ or set apiStrict: false to avoid encountering errors.

参数

options?

EstimatedDocumentCountOptions

Optional settings for the command

返回

Promise<number>

参阅

Behavior


find()

调用签名

find(): FindCursor<WithId<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:1608

Creates a cursor for a filter that can be used to iterate over results from MongoDB

返回

FindCursor<WithId<TSchema>>

调用签名

find(filter, options?): FindCursor<WithId<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:1609

Creates a cursor for a filter that can be used to iterate over results from MongoDB

参数
filter

Filter<TSchema>

The filter predicate. If unspecified, then all documents in the collection will match the predicate

options?

FindOptions<Document>

返回

FindCursor<WithId<TSchema>>

调用签名

find<T>(filter, options?): FindCursor<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:1610

Creates a cursor for a filter that can be used to iterate over results from MongoDB

类型参数
T

T extends Document

参数
filter

Filter<TSchema>

The filter predicate. If unspecified, then all documents in the collection will match the predicate

options?

FindOptions<Document>

返回

FindCursor<T>


findOne()

调用签名

findOne(): Promise<WithId<TSchema> | null>

定义于: 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:1597

Fetches the first document that matches the filter

返回

Promise<WithId<TSchema> | null>

调用签名

findOne(filter): Promise<WithId<TSchema> | null>

定义于: 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:1598

Fetches the first document that matches the filter

参数
filter

Filter<TSchema>

Query for find Operation

返回

Promise<WithId<TSchema> | null>

调用签名

findOne(filter, options): Promise<WithId<TSchema> | null>

定义于: 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:1599

Fetches the first document that matches the filter

参数
filter

Filter<TSchema>

Query for find Operation

options

FindOptions

Optional settings for the command

返回

Promise<WithId<TSchema> | null>

调用签名

findOne<T>(): Promise<T | null>

定义于: 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:1600

Fetches the first document that matches the filter

类型参数
T

T = TSchema

返回

Promise<T | null>

调用签名

findOne<T>(filter): Promise<T | null>

定义于: 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:1601

Fetches the first document that matches the filter

类型参数
T

T = TSchema

参数
filter

Filter<TSchema>

Query for find Operation

返回

Promise<T | null>

调用签名

findOne<T>(filter, options?): Promise<T | null>

定义于: 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:1602

Fetches the first document that matches the filter

类型参数
T

T = TSchema

参数
filter

Filter<TSchema>

Query for find Operation

options?

FindOptions<Document>

Optional settings for the command

返回

Promise<T | null>


findOneAndDelete()

findOneAndDelete(filter, options?): Promise<ModifyResult<TSchema> | null>

定义于: 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:1787

Find a document and delete it in one atomic operation. Requires a write lock for the duration of the operation.

参数

filter

Filter<TSchema>

The filter used to select the document to remove

options?

FindOneAndDeleteOptions

Optional settings for the command

返回

Promise<ModifyResult<TSchema> | null>


findOneAndReplace()

findOneAndReplace(filter, replacement, options?): Promise<ModifyResult<TSchema> | null>

定义于: 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:1795

Find a document and replace it in one atomic operation. Requires a write lock for the duration of the operation.

参数

filter

Filter<TSchema>

The filter used to select the document to replace

replacement

WithoutId<TSchema>

The Document that replaces the matching document

options?

FindOneAndReplaceOptions

Optional settings for the command

返回

Promise<ModifyResult<TSchema> | null>


findOneAndUpdate()

findOneAndUpdate(filter, update, options?): Promise<ModifyResult<TSchema> | null>

定义于: 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:1803

Find a document and update it in one atomic operation. Requires a write lock for the duration of the operation.

参数

filter

Filter<TSchema>

The filter used to select the document to update

update

UpdateFilter<TSchema>

Update operations to be performed on the document

options?

FindOneAndUpdateOptions

Optional settings for the command

返回

Promise<ModifyResult<TSchema> | null>


indexes()

indexes(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:1774

Retrieve all the indexes on the collection.

参数

options?

IndexInformationOptions

Optional settings for the command

返回

Promise<Document[]>


indexExists()

indexExists(indexes, 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:1709

Checks if one or more indexes exist on the collection, fails on first non-existing index

参数

indexes

string | string[]

One or more index names to check.

options?

IndexInformationOptions

Optional settings for the command

返回

Promise<boolean>


indexInformation()

indexInformation(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:1715

Retrieves this collections index info.

参数

options?

IndexInformationOptions

Optional settings for the command

返回

Promise<Document>


initializeOrderedBulkOp()

initializeOrderedBulkOp(options?): OrderedBulkOperation

定义于: 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:1867

Initiate an In order bulk write operation. Operations will be serially executed in the order they are added, creating a new operation for each switch in types.

参数

options?

BulkWriteOptions

返回

OrderedBulkOperation

抛出

MongoNotConnectedError

备注

NOTE: MongoClient must be connected prior to calling this method due to a known limitation in this legacy implementation. However, collection.bulkWrite() provides an equivalent API that does not require prior connecting.


initializeUnorderedBulkOp()

initializeUnorderedBulkOp(options?): UnorderedBulkOperation

定义于: 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:1858

Initiate an Out of order batch write operation. All operations will be buffered into insert/update/remove commands executed out of order.

参数

options?

BulkWriteOptions

返回

UnorderedBulkOperation

抛出

MongoNotConnectedError

备注

NOTE: MongoClient must be connected prior to calling this method due to a known limitation in this legacy implementation. However, collection.bulkWrite() provides an equivalent API that does not require prior connecting.


insertMany()

insertMany(docs, options?): Promise<InsertManyResult<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:1516

Inserts an array of documents into MongoDB. If documents passed in do not contain the _id field, one will be added to each of the documents missing it by the driver, mutating the document. This behavior can be overridden by setting the forceServerObjectId flag.

参数

docs

OptionalUnlessRequiredId<TSchema>[]

The documents to insert

options?

BulkWriteOptions

Optional settings for the command

返回

Promise<InsertManyResult<TSchema>>


insertOne()

insertOne(doc, options?): Promise<InsertOneResult<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:1507

Inserts a single document into MongoDB. If documents passed in do not contain the _id field, one will be added to each of the documents missing it by the driver, mutating the document. This behavior can be overridden by setting the forceServerObjectId flag.

参数

doc

OptionalUnlessRequiredId<TSchema>

The document to insert

options?

InsertOneOptions

Optional settings for the command

返回

Promise<InsertOneResult<TSchema>>


isCapped()

isCapped(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:1622

Returns if the collection is a capped collection

参数

options?

OperationOptions

Optional settings for the command

返回

Promise<boolean>


listIndexes()

listIndexes(options?): ListIndexesCursor

定义于: 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:1702

Get the list of all indexes information for the collection.

参数

options?

ListIndexesOptions

Optional settings for the command

返回

ListIndexesCursor


options()

options(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:1616

Returns the options of the collection.

参数

options?

OperationOptions

Optional settings for the command

返回

Promise<Document>


rename()

rename(newName, 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:1584

Rename the collection.

参数

newName

string

New name of of the collection.

options?

RenameOptions

Optional settings for the command

返回

Promise<Collection<Document>>

备注

This operation does not inherit options from the Db or MongoClient.


replaceOne()

replaceOne(filter, replacement, options?): Promise<Document | UpdateResult>

定义于: 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:1552

Replace a document in a collection with another document

参数

filter

Filter<TSchema>

The filter used to select the document to replace

replacement

WithoutId<TSchema>

The Document that replaces the matching document

options?

ReplaceOptions

Optional settings for the command

返回

Promise<Document | UpdateResult>


stats()

stats(options?): Promise<CollStats>

定义于: 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:1780

Get all the collection statistics.

参数

options?

CollStatsOptions

Optional settings for the command

返回

Promise<CollStats>


updateMany()

updateMany(filter, update, options?): Promise<UpdateResult>

定义于: 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:1560

Update multiple documents in a collection

参数

filter

Filter<TSchema>

The filter used to select the documents to update

update

UpdateFilter<TSchema>

The update operations to be applied to the documents

options?

UpdateOptions

Optional settings for the command

返回

Promise<UpdateResult>


updateOne()

updateOne(filter, update, options?): Promise<UpdateResult>

定义于: 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:1544

Update a single document in a collection

参数

filter

Filter<TSchema>

The filter used to select the document to update

update

UpdateFilter<TSchema> | Partial<TSchema>

The update operations to be applied to the document

options?

UpdateOptions

Optional settings for the command

返回

Promise<UpdateResult>


watch()

watch<TLocal, TChange>(pipeline?, options?): ChangeStream<TLocal, 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:1849

Create a new Change Stream, watching for new changes (insertions, updates, replacements, deletions, and invalidations) in this collection.

类型参数

TLocal

TLocal extends Document = TSchema

Type of the data being detected by the change stream

TChange

TChange extends Document = ChangeStreamDocument<TLocal>

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<TLocal, TChange>

备注

watch() accepts two generic arguments for distinct use cases:

  • The first is to override the schema that may be defined for this specific collection
  • 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

Examples

By just providing the first argument I can type the change to be ChangeStreamDocument<{ _id: number }>

collection.watch<{ _id: number }>()
  .on('change', change => console.log(change._id.toFixed(4)))

Passing a second argument provides a way to reflect the type changes caused by an advanced pipeline. Here, we are using a pipeline to have MongoDB filter for insert changes only and add a comment. No need start from scratch on the ChangeStreamInsertDocument type! By using an intersection we can save time and ensure defaults remain the same type!

collection
  .watch<Schema, ChangeStreamInsertDocument<Schema> & { comment: string }>([
    { $addFields: { comment: 'big changes' } },
    { $match: { operationType: 'insert' } }
  ])
  .on('change', (change) => {
    change.comment.startsWith('big')
    change.operationType === 'insert'
    // No need to narrow in code because the generics did that for us!
    expectType<Schema>(change.fullDocument)
  })

Clone this wiki locally