Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 | 396x 396x 396x 396x 396x 396x 396x 36913x 36913x 36913x 402x 36913x 396x 48599x 48599x 48599x 48599x 48599x 48599x 7394x 48599x 7355x 48599x 384x 48599x 10206x 48599x 633x 48599x 2352x 48599x 48599x 48599x 55471x 53639x 53639x 53639x 53639x 53639x 53639x 1602x 1602x 336x 1266x 53303x 41338x 396x 11228x 11228x 11228x 396x 15307x 15307x 15307x 229x 15078x 15078x 15078x 396x 21835x 21835x 21835x 21835x 21835x 21835x 21835x 687x 396x | import type { Document } from '../bson'; import type { Collection } from '../collection'; import { MongoCompatibilityError, MongoInvalidArgumentError } from '../error'; import { ReadPreference } from '../read_preference'; import type { Server } from '../sdam/server'; import type { ClientSession } from '../sessions'; import { formatSort, type Sort, type SortForCmd } from '../sort'; import { type TimeoutContext } from '../timeout'; import { decorateWithCollation, hasAtomicOperators, maxWireVersion } from '../utils'; import { type WriteConcern, type WriteConcernSettings } from '../write_concern'; import { CommandOperation, type CommandOperationOptions } from './command'; import { Aspect, defineAspects } from './operation'; /** @public */ export const ReturnDocument = Object.freeze({ BEFORE: 'before', AFTER: 'after' } as const); /** @public */ export type ReturnDocument = (typeof ReturnDocument)[keyof typeof ReturnDocument]; /** @public */ export interface FindOneAndDeleteOptions extends CommandOperationOptions { /** An optional hint for query optimization. See the {@link https://www.mongodb.com/docs/manual/reference/command/update/#update-command-hint|update command} reference for more information.*/ hint?: Document; /** Limits the fields to return for all matching documents. */ projection?: Document; /** Determines which document the operation modifies if the query selects multiple documents. */ sort?: Sort; /** Map of parameter names and values that can be accessed using $$var (requires MongoDB 5.0). */ let?: Document; /** * Return the ModifyResult instead of the modified document. Defaults to false */ includeResultMetadata?: boolean; } /** @public */ export interface FindOneAndReplaceOptions extends CommandOperationOptions { /** Allow driver to bypass schema validation. */ bypassDocumentValidation?: boolean; /** An optional hint for query optimization. See the {@link https://www.mongodb.com/docs/manual/reference/command/update/#update-command-hint|update command} reference for more information.*/ hint?: Document; /** Limits the fields to return for all matching documents. */ projection?: Document; /** When set to 'after', returns the updated document rather than the original. The default is 'before'. */ returnDocument?: ReturnDocument; /** Determines which document the operation modifies if the query selects multiple documents. */ sort?: Sort; /** Upsert the document if it does not exist. */ upsert?: boolean; /** Map of parameter names and values that can be accessed using $$var (requires MongoDB 5.0). */ let?: Document; /** * Return the ModifyResult instead of the modified document. Defaults to false */ includeResultMetadata?: boolean; } /** @public */ export interface FindOneAndUpdateOptions extends CommandOperationOptions { /** Optional list of array filters referenced in filtered positional operators */ arrayFilters?: Document[]; /** Allow driver to bypass schema validation. */ bypassDocumentValidation?: boolean; /** An optional hint for query optimization. See the {@link https://www.mongodb.com/docs/manual/reference/command/update/#update-command-hint|update command} reference for more information.*/ hint?: Document; /** Limits the fields to return for all matching documents. */ projection?: Document; /** When set to 'after', returns the updated document rather than the original. The default is 'before'. */ returnDocument?: ReturnDocument; /** Determines which document the operation modifies if the query selects multiple documents. */ sort?: Sort; /** Upsert the document if it does not exist. */ upsert?: boolean; /** Map of parameter names and values that can be accessed using $$var (requires MongoDB 5.0). */ let?: Document; /** * Return the ModifyResult instead of the modified document. Defaults to false */ includeResultMetadata?: boolean; } /** @internal */ interface FindAndModifyCmdBase { remove: boolean; new: boolean; upsert: boolean; update?: Document; sort?: SortForCmd; fields?: Document; bypassDocumentValidation?: boolean; arrayFilters?: Document[]; maxTimeMS?: number; let?: Document; writeConcern?: WriteConcern | WriteConcernSettings; /** * Comment to apply to the operation. * * In server versions pre-4.4, 'comment' must be string. A server * error will be thrown if any other type is provided. * * In server versions 4.4 and above, 'comment' can be any valid BSON type. */ comment?: unknown; } function configureFindAndModifyCmdBaseUpdateOpts( cmdBase: FindAndModifyCmdBase, options: FindOneAndReplaceOptions | FindOneAndUpdateOptions ): FindAndModifyCmdBase { cmdBase.new = options.returnDocument === ReturnDocument.AFTER; cmdBase.upsert = options.upsert === true; if (options.bypassDocumentValidation === true) { cmdBase.bypassDocumentValidation = options.bypassDocumentValidation; } return cmdBase; } /** @internal */ export class FindAndModifyOperation extends CommandOperation<Document> { override options: FindOneAndReplaceOptions | FindOneAndUpdateOptions | FindOneAndDeleteOptions; cmdBase: FindAndModifyCmdBase; collection: Collection; query: Document; doc?: Document; constructor( collection: Collection, query: Document, options: FindOneAndReplaceOptions | FindOneAndUpdateOptions | FindOneAndDeleteOptions ) { super(collection, options); this.options = options ?? {}; this.cmdBase = { remove: false, new: false, upsert: false }; options.includeResultMetadata ??= false; const sort = formatSort(options.sort); if (sort) { this.cmdBase.sort = sort; } if (options.projection) { this.cmdBase.fields = options.projection; } if (options.maxTimeMS) { this.cmdBase.maxTimeMS = options.maxTimeMS; } // Decorate the findAndModify command with the write Concern if (options.writeConcern) { this.cmdBase.writeConcern = options.writeConcern; } if (options.let) { this.cmdBase.let = options.let; } // we check for undefined specifically here to allow falsy values // eslint-disable-next-line no-restricted-syntax if (options.comment !== undefined) { this.cmdBase.comment = options.comment; } // force primary read preference this.readPreference = ReadPreference.primary; this.collection = collection; this.query = query; } override get commandName() { return 'findAndModify' as const; } override async execute( server: Server, session: ClientSession | undefined, timeoutContext: TimeoutContext ): Promise<Document> { const coll = this.collection; const query = this.query; const options = { ...this.options, ...this.bsonOptions }; // Create findAndModify command object const cmd: Document = { findAndModify: coll.collectionName, query: query, ...this.cmdBase }; decorateWithCollation(cmd, coll, options); if (options.hint) { // TODO: once this method becomes a CommandOperation we will have the server // in place to check. const unacknowledgedWrite = this.writeConcern?.w === 0; if (unacknowledgedWrite || maxWireVersion(server) < 8) { throw new MongoCompatibilityError( 'The current topology does not support a hint on findAndModify commands' ); } cmd.hint = options.hint; } // Execute the command const result = await super.executeCommand(server, session, cmd, timeoutContext); return options.includeResultMetadata ? result : (result.value ?? null); } } /** @internal */ export class FindOneAndDeleteOperation extends FindAndModifyOperation { constructor(collection: Collection, filter: Document, options: FindOneAndDeleteOptions) { // Basic validation Iif (filter == null || typeof filter !== 'object') { throw new MongoInvalidArgumentError('Argument "filter" must be an object'); } super(collection, filter, options); this.cmdBase.remove = true; } } /** @internal */ export class FindOneAndReplaceOperation extends FindAndModifyOperation { constructor( collection: Collection, filter: Document, replacement: Document, options: FindOneAndReplaceOptions ) { Iif (filter == null || typeof filter !== 'object') { throw new MongoInvalidArgumentError('Argument "filter" must be an object'); } Iif (replacement == null || typeof replacement !== 'object') { throw new MongoInvalidArgumentError('Argument "replacement" must be an object'); } if (hasAtomicOperators(replacement)) { throw new MongoInvalidArgumentError('Replacement document must not contain atomic operators'); } super(collection, filter, options); this.cmdBase.update = replacement; configureFindAndModifyCmdBaseUpdateOpts(this.cmdBase, options); } } /** @internal */ export class FindOneAndUpdateOperation extends FindAndModifyOperation { constructor( collection: Collection, filter: Document, update: Document, options: FindOneAndUpdateOptions ) { Iif (filter == null || typeof filter !== 'object') { throw new MongoInvalidArgumentError('Argument "filter" must be an object'); } Iif (update == null || typeof update !== 'object') { throw new MongoInvalidArgumentError('Argument "update" must be an object'); } Iif (!hasAtomicOperators(update)) { throw new MongoInvalidArgumentError('Update document requires atomic operators'); } super(collection, filter, options); this.cmdBase.update = update; configureFindAndModifyCmdBaseUpdateOpts(this.cmdBase, options); if (options.arrayFilters) { this.cmdBase.arrayFilters = options.arrayFilters; } } } defineAspects(FindAndModifyOperation, [ Aspect.WRITE_OPERATION, Aspect.RETRYABLE, Aspect.EXPLAINABLE ]); |