import { BSONSerializeOptions, Document } from '../bson'; import type { Collection } from '../collection'; import { AnyError, MongoServerError } from '../error'; import type { Filter, OneOrMore, OptionalId, UpdateFilter, WithoutId } from '../mongo_types'; import type { CollationOptions, CommandOperationOptions } from '../operations/command'; import { DeleteStatement } from '../operations/delete'; import { Hint } from '../operations/operation'; import { UpdateStatement } from '../operations/update'; import type { Topology } from '../sdam/topology'; import { Callback, MongoDBNamespace } from '../utils'; import { WriteConcern } from '../write_concern'; /** @internal */ declare const kServerError: unique symbol; /** @public */ export declare const BatchType: Readonly<{ readonly INSERT: 1; readonly UPDATE: 2; readonly DELETE: 3; }>; /** @public */ export declare type BatchType = typeof BatchType[keyof typeof BatchType]; /** @public */ export interface InsertOneModel { /** The document to insert. */ document: OptionalId; } /** @public */ export interface DeleteOneModel { /** The filter to limit the deleted documents. */ filter: Filter; /** Specifies a collation. */ collation?: CollationOptions; /** The index to use. If specified, then the query system will only consider plans using the hinted index. */ hint?: Hint; } /** @public */ export interface DeleteManyModel { /** The filter to limit the deleted documents. */ filter: Filter; /** Specifies a collation. */ collation?: CollationOptions; /** The index to use. If specified, then the query system will only consider plans using the hinted index. */ hint?: Hint; } /** @public */ export interface ReplaceOneModel { /** The filter to limit the replaced document. */ filter: Filter; /** The document with which to replace the matched document. */ replacement: WithoutId; /** Specifies a collation. */ collation?: CollationOptions; /** The index to use. If specified, then the query system will only consider plans using the hinted index. */ hint?: Hint; /** When true, creates a new document if no document matches the query. */ upsert?: boolean; } /** @public */ export interface UpdateOneModel { /** The filter to limit the updated documents. */ filter: Filter; /** A document or pipeline containing update operators. */ update: UpdateFilter | UpdateFilter[]; /** A set of filters specifying to which array elements an update should apply. */ arrayFilters?: Document[]; /** Specifies a collation. */ collation?: CollationOptions; /** The index to use. If specified, then the query system will only consider plans using the hinted index. */ hint?: Hint; /** When true, creates a new document if no document matches the query. */ upsert?: boolean; } /** @public */ export interface UpdateManyModel { /** The filter to limit the updated documents. */ filter: Filter; /** A document or pipeline containing update operators. */ update: UpdateFilter | UpdateFilter[]; /** A set of filters specifying to which array elements an update should apply. */ arrayFilters?: Document[]; /** Specifies a collation. */ collation?: CollationOptions; /** The index to use. If specified, then the query system will only consider plans using the hinted index. */ hint?: Hint; /** When true, creates a new document if no document matches the query. */ upsert?: boolean; } /** @public */ export declare type AnyBulkWriteOperation = { insertOne: InsertOneModel; } | { replaceOne: ReplaceOneModel; } | { updateOne: UpdateOneModel; } | { updateMany: UpdateManyModel; } | { deleteOne: DeleteOneModel; } | { deleteMany: DeleteManyModel; }; /** @public */ export interface BulkResult { ok: number; writeErrors: WriteError[]; writeConcernErrors: WriteConcernError[]; insertedIds: Document[]; nInserted: number; nUpserted: number; nMatched: number; nModified: number; nRemoved: number; upserted: Document[]; opTime?: Document; } /** * Keeps the state of a unordered batch so we can rewrite the results * correctly after command execution * * @public */ export declare class Batch { originalZeroIndex: number; currentIndex: number; originalIndexes: number[]; batchType: BatchType; operations: T[]; size: number; sizeBytes: number; constructor(batchType: BatchType, originalZeroIndex: number); } /** * @public * The result of a bulk write. */ export declare class BulkWriteResult { result: BulkResult; /** * Create a new BulkWriteResult instance * @internal */ constructor(bulkResult: BulkResult); /** Number of documents inserted. */ get insertedCount(): number; /** Number of documents matched for update. */ get matchedCount(): number; /** Number of documents modified. */ get modifiedCount(): number; /** Number of documents deleted. */ get deletedCount(): number; /** Number of documents upserted. */ get upsertedCount(): number; /** Upserted document generated Id's, hash key is the index of the originating operation */ get upsertedIds(): { [key: number]: any; }; /** Inserted document generated Id's, hash key is the index of the originating operation */ get insertedIds(): { [key: number]: any; }; /** Evaluates to true if the bulk operation correctly executes */ get ok(): number; /** The number of inserted documents */ get nInserted(): number; /** Number of upserted documents */ get nUpserted(): number; /** Number of matched documents */ get nMatched(): number; /** Number of documents updated physically on disk */ get nModified(): number; /** Number of removed documents */ get nRemoved(): number; /** Returns an array of all inserted ids */ getInsertedIds(): Document[]; /** Returns an array of all upserted ids */ getUpsertedIds(): Document[]; /** Returns the upserted id at the given index */ getUpsertedIdAt(index: number): Document | undefined; /** Returns raw internal result */ getRawResponse(): Document; /** Returns true if the bulk operation contains a write error */ hasWriteErrors(): boolean; /** Returns the number of write errors off the bulk operation */ getWriteErrorCount(): number; /** Returns a specific write error object */ getWriteErrorAt(index: number): WriteError | undefined; /** Retrieve all write errors */ getWriteErrors(): WriteError[]; /** Retrieve lastOp if available */ getLastOp(): Document | undefined; /** Retrieve the write concern error if one exists */ getWriteConcernError(): WriteConcernError | undefined; toJSON(): BulkResult; toString(): string; isOk(): boolean; } /** @public */ export interface WriteConcernErrorData { code: number; errmsg: string; errInfo?: Document; } /** * An error representing a failure by the server to apply the requested write concern to the bulk operation. * @public * @category Error */ export declare class WriteConcernError { /** @internal */ [kServerError]: WriteConcernErrorData; constructor(error: WriteConcernErrorData); /** Write concern error code. */ get code(): number | undefined; /** Write concern error message. */ get errmsg(): string | undefined; /** Write concern error info. */ get errInfo(): Document | undefined; /** @deprecated The `err` prop that contained a MongoServerError has been deprecated. */ get err(): WriteConcernErrorData; toJSON(): WriteConcernErrorData; toString(): string; } /** @public */ export interface BulkWriteOperationError { index: number; code: number; errmsg: string; errInfo: Document; op: Document | UpdateStatement | DeleteStatement; } /** * An error that occurred during a BulkWrite on the server. * @public * @category Error */ export declare class WriteError { err: BulkWriteOperationError; constructor(err: BulkWriteOperationError); /** WriteError code. */ get code(): number; /** WriteError original bulk operation index. */ get index(): number; /** WriteError message. */ get errmsg(): string | undefined; /** WriteError details. */ get errInfo(): Document | undefined; /** Returns the underlying operation that caused the error */ getOperation(): Document; toJSON(): { code: number; index: number; errmsg?: string; op: Document; }; toString(): string; } /** Merges results into shared data structure */ export declare function mergeBatchResults(batch: Batch, bulkResult: BulkResult, err?: AnyError, result?: Document): void; /** * An error indicating an unsuccessful Bulk Write * @public * @category Error */ export declare class MongoBulkWriteError extends MongoServerError { result: BulkWriteResult; writeErrors: OneOrMore; err?: WriteConcernError; /** Creates a new MongoBulkWriteError */ constructor(error: { message: string; code: number; writeErrors?: WriteError[]; } | WriteConcernError | AnyError, result: BulkWriteResult); get name(): string; /** Number of documents inserted. */ get insertedCount(): number; /** Number of documents matched for update. */ get matchedCount(): number; /** Number of documents modified. */ get modifiedCount(): number; /** Number of documents deleted. */ get deletedCount(): number; /** Number of documents upserted. */ get upsertedCount(): number; /** Inserted document generated Id's, hash key is the index of the originating operation */ get insertedIds(): { [key: number]: any; }; /** Upserted document generated Id's, hash key is the index of the originating operation */ get upsertedIds(): { [key: number]: any; }; } /** * A builder object that is returned from {@link BulkOperationBase#find}. * Is used to build a write operation that involves a query filter. * * @public */ export declare class FindOperators { bulkOperation: BulkOperationBase; /** * Creates a new FindOperators object. * @internal */ constructor(bulkOperation: BulkOperationBase); /** Add a multiple update operation to the bulk operation */ update(updateDocument: Document): BulkOperationBase; /** Add a single update operation to the bulk operation */ updateOne(updateDocument: Document): BulkOperationBase; /** Add a replace one operation to the bulk operation */ replaceOne(replacement: Document): BulkOperationBase; /** Add a delete one operation to the bulk operation */ deleteOne(): BulkOperationBase; /** Add a delete many operation to the bulk operation */ delete(): BulkOperationBase; /** Upsert modifier for update bulk operation, noting that this operation is an upsert. */ upsert(): this; /** Specifies the collation for the query condition. */ collation(collation: CollationOptions): this; /** Specifies arrayFilters for UpdateOne or UpdateMany bulk operations. */ arrayFilters(arrayFilters: Document[]): this; } /** @internal */ export interface BulkOperationPrivate { bulkResult: BulkResult; currentBatch?: Batch; currentIndex: number; currentBatchSize: number; currentBatchSizeBytes: number; currentInsertBatch?: Batch; currentUpdateBatch?: Batch; currentRemoveBatch?: Batch; batches: Batch[]; writeConcern?: WriteConcern; maxBsonObjectSize: number; maxBatchSizeBytes: number; maxWriteBatchSize: number; maxKeySize: number; namespace: MongoDBNamespace; topology: Topology; options: BulkWriteOptions; bsonOptions: BSONSerializeOptions; currentOp?: Document; executed: boolean; collection: Collection; err?: AnyError; checkKeys: boolean; bypassDocumentValidation?: boolean; } /** @public */ export interface BulkWriteOptions extends CommandOperationOptions { /** Allow driver to bypass schema validation in MongoDB 3.2 or higher. */ bypassDocumentValidation?: boolean; /** If true, when an insert fails, don't execute the remaining writes. If false, continue with remaining inserts when one fails. */ ordered?: boolean; /** @deprecated use `ordered` instead */ keepGoing?: boolean; /** Force server to assign _id values instead of driver. */ forceServerObjectId?: boolean; /** Map of parameter names and values that can be accessed using $$var (requires MongoDB 5.0). */ let?: Document; } /** @public */ export declare abstract class BulkOperationBase { isOrdered: boolean; /** @internal */ s: BulkOperationPrivate; operationId?: number; /** * Create a new OrderedBulkOperation or UnorderedBulkOperation instance * @internal */ constructor(collection: Collection, options: BulkWriteOptions, isOrdered: boolean); /** * Add a single insert document to the bulk operation * * @example * ```js * const bulkOp = collection.initializeOrderedBulkOp(); * * // Adds three inserts to the bulkOp. * bulkOp * .insert({ a: 1 }) * .insert({ b: 2 }) * .insert({ c: 3 }); * await bulkOp.execute(); * ``` */ insert(document: Document): BulkOperationBase; /** * Builds a find operation for an update/updateOne/delete/deleteOne/replaceOne. * Returns a builder object used to complete the definition of the operation. * * @example * ```js * const bulkOp = collection.initializeOrderedBulkOp(); * * // Add an updateOne to the bulkOp * bulkOp.find({ a: 1 }).updateOne({ $set: { b: 2 } }); * * // Add an updateMany to the bulkOp * bulkOp.find({ c: 3 }).update({ $set: { d: 4 } }); * * // Add an upsert * bulkOp.find({ e: 5 }).upsert().updateOne({ $set: { f: 6 } }); * * // Add a deletion * bulkOp.find({ g: 7 }).deleteOne(); * * // Add a multi deletion * bulkOp.find({ h: 8 }).delete(); * * // Add a replaceOne * bulkOp.find({ i: 9 }).replaceOne({writeConcern: { j: 10 }}); * * // Update using a pipeline (requires Mongodb 4.2 or higher) * bulk.find({ k: 11, y: { $exists: true }, z: { $exists: true } }).updateOne([ * { $set: { total: { $sum: [ '$y', '$z' ] } } } * ]); * * // All of the ops will now be executed * await bulkOp.execute(); * ``` */ find(selector: Document): FindOperators; /** Specifies a raw operation to perform in the bulk write. */ raw(op: AnyBulkWriteOperation): this; get bsonOptions(): BSONSerializeOptions; get writeConcern(): WriteConcern | undefined; get batches(): Batch[]; execute(options?: BulkWriteOptions): Promise; execute(callback: Callback): void; execute(options: BulkWriteOptions | undefined, callback: Callback): void; execute(options?: BulkWriteOptions | Callback, callback?: Callback): Promise | void; /** * Handles the write error before executing commands * @internal */ handleWriteError(callback: Callback, writeResult: BulkWriteResult): boolean; abstract addToOperationsList(batchType: BatchType, document: Document | UpdateStatement | DeleteStatement): this; } export {}; //# sourceMappingURL=common.d.ts.map