Refactoring day1
This commit is contained in:
145
node_modules/mongodb/lib/collection.js
generated
vendored
145
node_modules/mongodb/lib/collection.js
generated
vendored
@@ -5,6 +5,7 @@ const deprecateOptions = require('./utils').deprecateOptions;
|
||||
const checkCollectionName = require('./utils').checkCollectionName;
|
||||
const ObjectID = require('./core').BSON.ObjectID;
|
||||
const MongoError = require('./core').MongoError;
|
||||
const toError = require('./utils').toError;
|
||||
const normalizeHintField = require('./utils').normalizeHintField;
|
||||
const decorateCommand = require('./utils').decorateCommand;
|
||||
const decorateWithCollation = require('./utils').decorateWithCollation;
|
||||
@@ -15,6 +16,7 @@ const unordered = require('./bulk/unordered');
|
||||
const ordered = require('./bulk/ordered');
|
||||
const ChangeStream = require('./change_stream');
|
||||
const executeLegacyOperation = require('./utils').executeLegacyOperation;
|
||||
const resolveReadPreference = require('./utils').resolveReadPreference;
|
||||
const WriteConcern = require('./write_concern');
|
||||
const ReadConcern = require('./read_concern');
|
||||
const MongoDBNamespace = require('./utils').MongoDBNamespace;
|
||||
@@ -22,6 +24,7 @@ const AggregationCursor = require('./aggregation_cursor');
|
||||
const CommandCursor = require('./command_cursor');
|
||||
|
||||
// Operations
|
||||
const checkForAtomicOperators = require('./operations/collection_ops').checkForAtomicOperators;
|
||||
const ensureIndex = require('./operations/collection_ops').ensureIndex;
|
||||
const group = require('./operations/collection_ops').group;
|
||||
const parallelCollectionScan = require('./operations/collection_ops').parallelCollectionScan;
|
||||
@@ -32,6 +35,7 @@ const updateDocuments = require('./operations/common_functions').updateDocuments
|
||||
const AggregateOperation = require('./operations/aggregate');
|
||||
const BulkWriteOperation = require('./operations/bulk_write');
|
||||
const CountDocumentsOperation = require('./operations/count_documents');
|
||||
const CreateIndexOperation = require('./operations/create_index');
|
||||
const CreateIndexesOperation = require('./operations/create_indexes');
|
||||
const DeleteManyOperation = require('./operations/delete_many');
|
||||
const DeleteOneOperation = require('./operations/delete_one');
|
||||
@@ -274,7 +278,7 @@ Object.defineProperty(Collection.prototype, 'hint', {
|
||||
}
|
||||
});
|
||||
|
||||
const DEPRECATED_FIND_OPTIONS = ['maxScan', 'fields', 'snapshot', 'oplogReplay'];
|
||||
const DEPRECATED_FIND_OPTIONS = ['maxScan', 'fields', 'snapshot'];
|
||||
|
||||
/**
|
||||
* Creates a cursor for a query that can be used to iterate over results from MongoDB
|
||||
@@ -309,7 +313,6 @@ const DEPRECATED_FIND_OPTIONS = ['maxScan', 'fields', 'snapshot', 'oplogReplay']
|
||||
* @param {number} [options.maxAwaitTimeMS] The maximum amount of time for the server to wait on new documents to satisfy a tailable cursor query. Requires `tailable` and `awaitData` to be true
|
||||
* @param {boolean} [options.noCursorTimeout] The server normally times out idle cursors after an inactivity period (10 minutes) to prevent excess memory use. Set this option to prevent that.
|
||||
* @param {object} [options.collation] Specify collation (MongoDB 3.4 or higher) settings for update operation (see 3.4 documentation for available fields).
|
||||
* @param {boolean} [options.allowDiskUse] Enables writing to temporary files on the server.
|
||||
* @param {ClientSession} [options.session] optional session to use for this operation
|
||||
* @throws {MongoError}
|
||||
* @return {Cursor}
|
||||
@@ -396,7 +399,7 @@ Collection.prototype.find = deprecateOptions(
|
||||
newOptions.slaveOk = options.slaveOk != null ? options.slaveOk : this.s.db.slaveOk;
|
||||
|
||||
// Add read preference if needed
|
||||
newOptions.readPreference = ReadPreference.resolve(this, newOptions);
|
||||
newOptions.readPreference = resolveReadPreference(this, newOptions);
|
||||
|
||||
// Set slave ok to true if read preference different from primary
|
||||
if (
|
||||
@@ -419,10 +422,6 @@ Collection.prototype.find = deprecateOptions(
|
||||
query: selector
|
||||
};
|
||||
|
||||
if (typeof options.allowDiskUse === 'boolean') {
|
||||
findCommand.allowDiskUse = options.allowDiskUse;
|
||||
}
|
||||
|
||||
// Ensure we use the right await data option
|
||||
if (typeof newOptions.awaitdata === 'boolean') {
|
||||
newOptions.awaitData = newOptions.awaitdata;
|
||||
@@ -745,6 +744,14 @@ Collection.prototype.insert = deprecate(function(docs, options, callback) {
|
||||
*/
|
||||
Collection.prototype.updateOne = function(filter, update, options, callback) {
|
||||
if (typeof options === 'function') (callback = options), (options = {});
|
||||
options = options || {};
|
||||
|
||||
const err = checkForAtomicOperators(update);
|
||||
if (err) {
|
||||
if (typeof callback === 'function') return callback(err);
|
||||
return this.s.promiseLibrary.reject(err);
|
||||
}
|
||||
|
||||
options = Object.assign({}, options);
|
||||
|
||||
// Add ignoreUndefined
|
||||
@@ -753,11 +760,9 @@ Collection.prototype.updateOne = function(filter, update, options, callback) {
|
||||
options.ignoreUndefined = this.s.options.ignoreUndefined;
|
||||
}
|
||||
|
||||
return executeOperation(
|
||||
this.s.topology,
|
||||
new UpdateOneOperation(this, filter, update, options),
|
||||
callback
|
||||
);
|
||||
const updateOneOperation = new UpdateOneOperation(this, filter, update, options);
|
||||
|
||||
return executeOperation(this.s.topology, updateOneOperation, callback);
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -790,11 +795,9 @@ Collection.prototype.replaceOne = function(filter, doc, options, callback) {
|
||||
options.ignoreUndefined = this.s.options.ignoreUndefined;
|
||||
}
|
||||
|
||||
return executeOperation(
|
||||
this.s.topology,
|
||||
new ReplaceOneOperation(this, filter, doc, options),
|
||||
callback
|
||||
);
|
||||
const replaceOneOperation = new ReplaceOneOperation(this, filter, doc, options);
|
||||
|
||||
return executeOperation(this.s.topology, replaceOneOperation, callback);
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -820,6 +823,14 @@ Collection.prototype.replaceOne = function(filter, doc, options, callback) {
|
||||
*/
|
||||
Collection.prototype.updateMany = function(filter, update, options, callback) {
|
||||
if (typeof options === 'function') (callback = options), (options = {});
|
||||
options = options || {};
|
||||
|
||||
const err = checkForAtomicOperators(update);
|
||||
if (err) {
|
||||
if (typeof callback === 'function') return callback(err);
|
||||
return this.s.promiseLibrary.reject(err);
|
||||
}
|
||||
|
||||
options = Object.assign({}, options);
|
||||
|
||||
// Add ignoreUndefined
|
||||
@@ -828,11 +839,9 @@ Collection.prototype.updateMany = function(filter, update, options, callback) {
|
||||
options.ignoreUndefined = this.s.options.ignoreUndefined;
|
||||
}
|
||||
|
||||
return executeOperation(
|
||||
this.s.topology,
|
||||
new UpdateManyOperation(this, filter, update, options),
|
||||
callback
|
||||
);
|
||||
const updateManyOperation = new UpdateManyOperation(this, filter, update, options);
|
||||
|
||||
return executeOperation(this.s.topology, updateManyOperation, callback);
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -904,7 +913,6 @@ Collection.prototype.update = deprecate(function(selector, update, options, call
|
||||
* @param {boolean} [options.serializeFunctions=false] Serialize functions on any object.
|
||||
* @param {boolean} [options.ignoreUndefined=false] Specify if the BSON serializer should ignore undefined fields.
|
||||
* @param {ClientSession} [options.session] optional session to use for this operation
|
||||
* @param {string|object} [options.hint] optional index hint for optimizing the filter query
|
||||
* @param {Collection~deleteWriteOpCallback} [callback] The command result callback
|
||||
* @return {Promise} returns Promise if no callback passed
|
||||
*/
|
||||
@@ -938,7 +946,6 @@ Collection.prototype.removeOne = Collection.prototype.deleteOne;
|
||||
* @param {boolean} [options.serializeFunctions=false] Serialize functions on any object.
|
||||
* @param {boolean} [options.ignoreUndefined=false] Specify if the BSON serializer should ignore undefined fields.
|
||||
* @param {ClientSession} [options.session] optional session to use for this operation
|
||||
* @param {string|object} [options.hint] optional index hint for optimizing the filter query
|
||||
* @param {Collection~deleteWriteOpCallback} [callback] The command result callback
|
||||
* @return {Promise} returns Promise if no callback passed
|
||||
*/
|
||||
@@ -1199,7 +1206,6 @@ Collection.prototype.isCapped = function(options, callback) {
|
||||
* @param {object} [options.partialFilterExpression] Creates a partial index based on the given filter object (MongoDB 3.2 or higher)
|
||||
* @param {object} [options.collation] Specify collation (MongoDB 3.4 or higher) settings for update operation (see 3.4 documentation for available fields).
|
||||
* @param {ClientSession} [options.session] optional session to use for this operation
|
||||
* @param {(number|string)} [options.commitQuorum] (MongoDB 4.4. or higher) Specifies how many data-bearing members of a replica set, including the primary, must complete the index builds successfully before the primary marks the indexes as ready. This option accepts the same values for the "w" field in a write concern plus "votingMembers", which indicates all voting data-bearing nodes.
|
||||
* @param {Collection~resultCallback} [callback] The command result callback
|
||||
* @return {Promise} returns Promise if no callback passed
|
||||
* @example
|
||||
@@ -1226,14 +1232,14 @@ Collection.prototype.createIndex = function(fieldOrSpec, options, callback) {
|
||||
if (typeof options === 'function') (callback = options), (options = {});
|
||||
options = options || {};
|
||||
|
||||
const createIndexesOperation = new CreateIndexesOperation(
|
||||
this,
|
||||
const createIndexOperation = new CreateIndexOperation(
|
||||
this.s.db,
|
||||
this.collectionName,
|
||||
fieldOrSpec,
|
||||
options
|
||||
);
|
||||
|
||||
return executeOperation(this.s.topology, createIndexesOperation, callback);
|
||||
return executeOperation(this.s.topology, createIndexOperation, callback);
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -1254,7 +1260,6 @@ Collection.prototype.createIndex = function(fieldOrSpec, options, callback) {
|
||||
* @param {Collection~IndexDefinition[]} indexSpecs An array of index specifications to be created
|
||||
* @param {Object} [options] Optional settings
|
||||
* @param {ClientSession} [options.session] optional session to use for this operation
|
||||
* @param {(number|string)} [options.commitQuorum] (MongoDB 4.4. or higher) Specifies how many data-bearing members of a replica set, including the primary, must complete the index builds successfully before the primary marks the indexes as ready. This option accepts the same values for the "w" field in a write concern plus "votingMembers", which indicates all voting data-bearing nodes.
|
||||
* @param {Collection~resultCallback} [callback] The command result callback
|
||||
* @return {Promise} returns Promise if no callback passed
|
||||
* @example
|
||||
@@ -1279,15 +1284,9 @@ Collection.prototype.createIndexes = function(indexSpecs, options, callback) {
|
||||
if (typeof options === 'function') (callback = options), (options = {});
|
||||
|
||||
options = options ? Object.assign({}, options) : {};
|
||||
|
||||
if (typeof options.maxTimeMS !== 'number') delete options.maxTimeMS;
|
||||
|
||||
const createIndexesOperation = new CreateIndexesOperation(
|
||||
this,
|
||||
this.collectionName,
|
||||
indexSpecs,
|
||||
options
|
||||
);
|
||||
const createIndexesOperation = new CreateIndexesOperation(this, indexSpecs, options);
|
||||
|
||||
return executeOperation(this.s.topology, createIndexesOperation, callback);
|
||||
};
|
||||
@@ -1354,20 +1353,19 @@ Collection.prototype.dropAllIndexes = deprecate(
|
||||
* Reindex all indexes on the collection
|
||||
* Warning: reIndex is a blocking operation (indexes are rebuilt in the foreground) and will be slow for large collections.
|
||||
* @method
|
||||
* @deprecated use db.command instead
|
||||
* @param {Object} [options] Optional settings
|
||||
* @param {ClientSession} [options.session] optional session to use for this operation
|
||||
* @param {Collection~resultCallback} [callback] The command result callback
|
||||
* @return {Promise} returns Promise if no callback passed
|
||||
*/
|
||||
Collection.prototype.reIndex = deprecate(function(options, callback) {
|
||||
Collection.prototype.reIndex = function(options, callback) {
|
||||
if (typeof options === 'function') (callback = options), (options = {});
|
||||
options = options || {};
|
||||
|
||||
const reIndexOperation = new ReIndexOperation(this, options);
|
||||
|
||||
return executeOperation(this.s.topology, reIndexOperation, callback);
|
||||
}, 'collection.reIndex is deprecated. Use db.command instead.');
|
||||
};
|
||||
|
||||
/**
|
||||
* Get the list of all indexes information for the collection.
|
||||
@@ -1667,11 +1665,13 @@ Collection.prototype.findOneAndDelete = function(filter, options, callback) {
|
||||
if (typeof options === 'function') (callback = options), (options = {});
|
||||
options = options || {};
|
||||
|
||||
return executeOperation(
|
||||
this.s.topology,
|
||||
new FindOneAndDeleteOperation(this, filter, options),
|
||||
callback
|
||||
);
|
||||
// Basic validation
|
||||
if (filter == null || typeof filter !== 'object')
|
||||
throw toError('filter parameter must be an object');
|
||||
|
||||
const findOneAndDeleteOperation = new FindOneAndDeleteOperation(this, filter, options);
|
||||
|
||||
return executeOperation(this.s.topology, findOneAndDeleteOperation, callback);
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -1683,7 +1683,6 @@ Collection.prototype.findOneAndDelete = function(filter, options, callback) {
|
||||
* @param {object} [options] Optional settings.
|
||||
* @param {boolean} [options.bypassDocumentValidation=false] Allow driver to bypass schema validation in MongoDB 3.2 or higher.
|
||||
* @param {object} [options.collation] Specify collation (MongoDB 3.4 or higher) settings for update operation (see 3.4 documentation for available fields).
|
||||
* @param {string|object} [options.hint] An optional index to use for this operation
|
||||
* @param {number} [options.maxTimeMS] The maximum amount of time to allow the query to run.
|
||||
* @param {object} [options.projection] Limits the fields to return for all matching documents.
|
||||
* @param {object} [options.sort] Determines which document the operation modifies if the query selects multiple documents.
|
||||
@@ -1700,11 +1699,27 @@ Collection.prototype.findOneAndReplace = function(filter, replacement, options,
|
||||
if (typeof options === 'function') (callback = options), (options = {});
|
||||
options = options || {};
|
||||
|
||||
return executeOperation(
|
||||
this.s.topology,
|
||||
new FindOneAndReplaceOperation(this, filter, replacement, options),
|
||||
callback
|
||||
// Basic validation
|
||||
if (filter == null || typeof filter !== 'object')
|
||||
throw toError('filter parameter must be an object');
|
||||
if (replacement == null || typeof replacement !== 'object')
|
||||
throw toError('replacement parameter must be an object');
|
||||
|
||||
// Check that there are no atomic operators
|
||||
const keys = Object.keys(replacement);
|
||||
|
||||
if (keys[0] && keys[0][0] === '$') {
|
||||
throw toError('The replacement document must not contain atomic operators.');
|
||||
}
|
||||
|
||||
const findOneAndReplaceOperation = new FindOneAndReplaceOperation(
|
||||
this,
|
||||
filter,
|
||||
replacement,
|
||||
options
|
||||
);
|
||||
|
||||
return executeOperation(this.s.topology, findOneAndReplaceOperation, callback);
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -1717,7 +1732,6 @@ Collection.prototype.findOneAndReplace = function(filter, replacement, options,
|
||||
* @param {Array} [options.arrayFilters] optional list of array filters referenced in filtered positional operators
|
||||
* @param {boolean} [options.bypassDocumentValidation=false] Allow driver to bypass schema validation in MongoDB 3.2 or higher.
|
||||
* @param {object} [options.collation] Specify collation (MongoDB 3.4 or higher) settings for update operation (see 3.4 documentation for available fields).
|
||||
* @param {string|object} [options.hint] An optional index to use for this operation
|
||||
* @param {number} [options.maxTimeMS] The maximum amount of time to allow the query to run.
|
||||
* @param {object} [options.projection] Limits the fields to return for all matching documents.
|
||||
* @param {object} [options.sort] Determines which document the operation modifies if the query selects multiple documents.
|
||||
@@ -1726,7 +1740,7 @@ Collection.prototype.findOneAndReplace = function(filter, replacement, options,
|
||||
* @param {boolean} [options.checkKeys=false] If true, will throw if bson documents start with `$` or include a `.` in any key value
|
||||
* @param {boolean} [options.serializeFunctions=false] Serialize functions on any object.
|
||||
* @param {boolean} [options.ignoreUndefined=false] Specify if the BSON serializer should ignore undefined fields.
|
||||
* @param {ClientSession} [options.session] An ptional session to use for this operation
|
||||
* @param {ClientSession} [options.session] optional session to use for this operation
|
||||
* @param {Collection~findAndModifyCallback} [callback] The collection result callback
|
||||
* @return {Promise<Collection~findAndModifyWriteOpResultObject>} returns Promise if no callback passed
|
||||
*/
|
||||
@@ -1734,11 +1748,21 @@ Collection.prototype.findOneAndUpdate = function(filter, update, options, callba
|
||||
if (typeof options === 'function') (callback = options), (options = {});
|
||||
options = options || {};
|
||||
|
||||
return executeOperation(
|
||||
this.s.topology,
|
||||
new FindOneAndUpdateOperation(this, filter, update, options),
|
||||
callback
|
||||
);
|
||||
// Basic validation
|
||||
if (filter == null || typeof filter !== 'object')
|
||||
throw toError('filter parameter must be an object');
|
||||
if (update == null || typeof update !== 'object')
|
||||
throw toError('update parameter must be an object');
|
||||
|
||||
const err = checkForAtomicOperators(update);
|
||||
if (err) {
|
||||
if (typeof callback === 'function') return callback(err);
|
||||
return this.s.promiseLibrary.reject(err);
|
||||
}
|
||||
|
||||
const findOneAndUpdateOperation = new FindOneAndUpdateOperation(this, filter, update, options);
|
||||
|
||||
return executeOperation(this.s.topology, findOneAndUpdateOperation, callback);
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -1840,7 +1864,7 @@ Collection.prototype.findAndRemove = deprecate(function(query, sort, options, ca
|
||||
* @param {boolean} [options.promoteLongs=true] Promotes Long values to number if they fit inside the 53 bits resolution.
|
||||
* @param {boolean} [options.promoteValues=true] Promotes BSON values to native types where possible, set to false to only receive wrapper types.
|
||||
* @param {boolean} [options.promoteBuffers=false] Promotes Binary BSON values to native Node Buffers.
|
||||
* @param {object} [options.collation] Specify collation settings for operation. See {@link https://docs.mongodb.com/manual/reference/command/aggregate|aggregation documentation}.
|
||||
* @param {object} [options.collation] Specify collation (MongoDB 3.4 or higher) settings for update operation (see 3.4 documentation for available fields).
|
||||
* @param {string} [options.comment] Add a comment to an aggregation command
|
||||
* @param {string|object} [options.hint] Add an index selection hint to an aggregation command
|
||||
* @param {ClientSession} [options.session] optional session to use for this operation
|
||||
@@ -1954,7 +1978,7 @@ Collection.prototype.parallelCollectionScan = deprecate(function(options, callba
|
||||
|
||||
options = Object.assign({}, options);
|
||||
// Ensure we have the right read preference inheritance
|
||||
options.readPreference = ReadPreference.resolve(this, options);
|
||||
options.readPreference = resolveReadPreference(this, options);
|
||||
|
||||
// Add a promiseLibrary
|
||||
options.promiseLibrary = this.s.promiseLibrary;
|
||||
@@ -1985,9 +2009,8 @@ Collection.prototype.parallelCollectionScan = deprecate(function(options, callba
|
||||
* @param {ClientSession} [options.session] optional session to use for this operation
|
||||
* @param {Collection~resultCallback} [callback] The command result callback
|
||||
* @return {Promise} returns Promise if no callback passed
|
||||
* @deprecated See {@link https://docs.mongodb.com/manual/geospatial-queries/|geospatial queries docs} for current geospatial support
|
||||
*/
|
||||
Collection.prototype.geoHaystackSearch = deprecate(function(x, y, options, callback) {
|
||||
Collection.prototype.geoHaystackSearch = function(x, y, options, callback) {
|
||||
const args = Array.prototype.slice.call(arguments, 2);
|
||||
callback = typeof args[args.length - 1] === 'function' ? args.pop() : undefined;
|
||||
options = args.length ? args.shift() || {} : {};
|
||||
@@ -1995,7 +2018,7 @@ Collection.prototype.geoHaystackSearch = deprecate(function(x, y, options, callb
|
||||
const geoHaystackSearchOperation = new GeoHaystackSearchOperation(this, x, y, options);
|
||||
|
||||
return executeOperation(this.s.topology, geoHaystackSearchOperation, callback);
|
||||
}, 'geoHaystackSearch is deprecated, and will be removed in a future version.');
|
||||
};
|
||||
|
||||
/**
|
||||
* Run a group command across a collection
|
||||
|
||||
Reference in New Issue
Block a user