|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const OperationBase = require('./operation').OperationBase; |
| 4 | +const AggregationCursor = require('../aggregation_cursor'); |
| 5 | +const applyWriteConcern = require('../utils').applyWriteConcern; |
| 6 | +const decorateWithCollation = require('../utils').decorateWithCollation; |
| 7 | +const decorateWithReadConcern = require('../utils').decorateWithReadConcern; |
| 8 | +const handleCallback = require('../utils').handleCallback; |
| 9 | +const MongoError = require('mongodb-core').MongoError; |
| 10 | +const resolveReadPreference = require('../utils').resolveReadPreference; |
| 11 | +const toError = require('../utils').toError; |
| 12 | + |
| 13 | +const DB_AGGREGATE_COLLECTION = 1; |
| 14 | + |
| 15 | +class AggregateOperation extends OperationBase { |
| 16 | + constructor(db, collection, pipeline, options) { |
| 17 | + super(options); |
| 18 | + |
| 19 | + this.db = db; |
| 20 | + this.collection = collection; |
| 21 | + this.pipeline = pipeline; |
| 22 | + } |
| 23 | + |
| 24 | + execute(callback) { |
| 25 | + const db = this.db; |
| 26 | + const coll = this.collection; |
| 27 | + let pipeline = this.pipeline; |
| 28 | + let options = this.options; |
| 29 | + |
| 30 | + const isDbAggregate = typeof coll === 'string'; |
| 31 | + const target = isDbAggregate ? db : coll; |
| 32 | + const topology = target.s.topology; |
| 33 | + let hasOutStage = false; |
| 34 | + |
| 35 | + if (typeof options.out === 'string') { |
| 36 | + pipeline = pipeline.concat({ $out: options.out }); |
| 37 | + hasOutStage = true; |
| 38 | + } else if (pipeline.length > 0 && pipeline[pipeline.length - 1]['$out']) { |
| 39 | + hasOutStage = true; |
| 40 | + } |
| 41 | + |
| 42 | + let command; |
| 43 | + let namespace; |
| 44 | + let optionSources; |
| 45 | + |
| 46 | + if (isDbAggregate) { |
| 47 | + command = { aggregate: DB_AGGREGATE_COLLECTION, pipeline: pipeline }; |
| 48 | + namespace = `${db.s.databaseName}.${DB_AGGREGATE_COLLECTION}`; |
| 49 | + |
| 50 | + optionSources = { db }; |
| 51 | + } else { |
| 52 | + command = { aggregate: coll.s.name, pipeline: pipeline }; |
| 53 | + namespace = coll.s.namespace; |
| 54 | + |
| 55 | + optionSources = { db: coll.s.db, collection: coll }; |
| 56 | + } |
| 57 | + |
| 58 | + const takesWriteConcern = topology.capabilities().commandsTakeWriteConcern; |
| 59 | + |
| 60 | + if (!hasOutStage) { |
| 61 | + decorateWithReadConcern(command, target, options); |
| 62 | + } |
| 63 | + |
| 64 | + if (pipeline.length > 0 && pipeline[pipeline.length - 1]['$out'] && takesWriteConcern) { |
| 65 | + applyWriteConcern(command, optionSources, options); |
| 66 | + } |
| 67 | + |
| 68 | + try { |
| 69 | + decorateWithCollation(command, target, options); |
| 70 | + } catch (err) { |
| 71 | + if (typeof callback === 'function') return callback(err, null); |
| 72 | + throw err; |
| 73 | + } |
| 74 | + |
| 75 | + if (options.bypassDocumentValidation === true) { |
| 76 | + command.bypassDocumentValidation = options.bypassDocumentValidation; |
| 77 | + } |
| 78 | + |
| 79 | + if (typeof options.allowDiskUse === 'boolean') command.allowDiskUse = options.allowDiskUse; |
| 80 | + if (typeof options.maxTimeMS === 'number') command.maxTimeMS = options.maxTimeMS; |
| 81 | + |
| 82 | + if (options.hint) command.hint = options.hint; |
| 83 | + |
| 84 | + options = Object.assign({}, options); |
| 85 | + |
| 86 | + // Ensure we have the right read preference inheritance |
| 87 | + options.readPreference = resolveReadPreference(options, optionSources); |
| 88 | + |
| 89 | + if (options.explain) { |
| 90 | + if (command.readConcern || command.writeConcern) { |
| 91 | + throw toError( |
| 92 | + '"explain" cannot be used on an aggregate call with readConcern/writeConcern' |
| 93 | + ); |
| 94 | + } |
| 95 | + command.explain = options.explain; |
| 96 | + } |
| 97 | + |
| 98 | + if (typeof options.comment === 'string') command.comment = options.comment; |
| 99 | + |
| 100 | + // Validate that cursor options is valid |
| 101 | + if (options.cursor != null && typeof options.cursor !== 'object') { |
| 102 | + throw toError('cursor options must be an object'); |
| 103 | + } |
| 104 | + |
| 105 | + options.cursor = options.cursor || {}; |
| 106 | + if (options.batchSize && !hasOutStage) options.cursor.batchSize = options.batchSize; |
| 107 | + command.cursor = options.cursor; |
| 108 | + |
| 109 | + // promiseLibrary |
| 110 | + options.promiseLibrary = target.s.promiseLibrary; |
| 111 | + |
| 112 | + // Set the AggregationCursor constructor |
| 113 | + options.cursorFactory = AggregationCursor; |
| 114 | + |
| 115 | + if (typeof callback !== 'function') { |
| 116 | + if (!topology.capabilities()) { |
| 117 | + throw new MongoError('cannot connect to server'); |
| 118 | + } |
| 119 | + |
| 120 | + return topology.cursor(namespace, command, options); |
| 121 | + } |
| 122 | + |
| 123 | + return handleCallback(callback, null, topology.cursor(namespace, command, options)); |
| 124 | + } |
| 125 | +} |
| 126 | + |
| 127 | +module.exports = AggregateOperation; |
0 commit comments