This commit is contained in:
2020-08-20 11:44:32 +02:00
parent 4715fc1814
commit 6aceefeb2f
2891 changed files with 11239 additions and 347539 deletions

View File

@@ -5,7 +5,6 @@ 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;
@@ -16,7 +15,6 @@ 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;
@@ -24,7 +22,6 @@ 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;
@@ -35,7 +32,6 @@ 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');
@@ -278,7 +274,7 @@ Object.defineProperty(Collection.prototype, 'hint', {
}
});
const DEPRECATED_FIND_OPTIONS = ['maxScan', 'fields', 'snapshot'];
const DEPRECATED_FIND_OPTIONS = ['maxScan', 'fields', 'snapshot', 'oplogReplay'];
/**
* Creates a cursor for a query that can be used to iterate over results from MongoDB
@@ -313,6 +309,7 @@ const DEPRECATED_FIND_OPTIONS = ['maxScan', 'fields', 'snapshot'];
* @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}
@@ -399,7 +396,7 @@ Collection.prototype.find = deprecateOptions(
newOptions.slaveOk = options.slaveOk != null ? options.slaveOk : this.s.db.slaveOk;
// Add read preference if needed
newOptions.readPreference = resolveReadPreference(this, newOptions);
newOptions.readPreference = ReadPreference.resolve(this, newOptions);
// Set slave ok to true if read preference different from primary
if (
@@ -422,6 +419,10 @@ 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;
@@ -744,14 +745,6 @@ 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
@@ -760,9 +753,11 @@ Collection.prototype.updateOne = function(filter, update, options, callback) {
options.ignoreUndefined = this.s.options.ignoreUndefined;
}
const updateOneOperation = new UpdateOneOperation(this, filter, update, options);
return executeOperation(this.s.topology, updateOneOperation, callback);
return executeOperation(
this.s.topology,
new UpdateOneOperation(this, filter, update, options),
callback
);
};
/**
@@ -795,9 +790,11 @@ Collection.prototype.replaceOne = function(filter, doc, options, callback) {
options.ignoreUndefined = this.s.options.ignoreUndefined;
}
const replaceOneOperation = new ReplaceOneOperation(this, filter, doc, options);
return executeOperation(this.s.topology, replaceOneOperation, callback);
return executeOperation(
this.s.topology,
new ReplaceOneOperation(this, filter, doc, options),
callback
);
};
/**
@@ -823,14 +820,6 @@ 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
@@ -839,9 +828,11 @@ Collection.prototype.updateMany = function(filter, update, options, callback) {
options.ignoreUndefined = this.s.options.ignoreUndefined;
}
const updateManyOperation = new UpdateManyOperation(this, filter, update, options);
return executeOperation(this.s.topology, updateManyOperation, callback);
return executeOperation(
this.s.topology,
new UpdateManyOperation(this, filter, update, options),
callback
);
};
/**
@@ -913,6 +904,7 @@ 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
*/
@@ -946,6 +938,7 @@ 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
*/
@@ -1206,6 +1199,7 @@ 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
@@ -1232,14 +1226,14 @@ Collection.prototype.createIndex = function(fieldOrSpec, options, callback) {
if (typeof options === 'function') (callback = options), (options = {});
options = options || {};
const createIndexOperation = new CreateIndexOperation(
this.s.db,
const createIndexesOperation = new CreateIndexesOperation(
this,
this.collectionName,
fieldOrSpec,
options
);
return executeOperation(this.s.topology, createIndexOperation, callback);
return executeOperation(this.s.topology, createIndexesOperation, callback);
};
/**
@@ -1260,6 +1254,7 @@ 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
@@ -1284,9 +1279,15 @@ 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, indexSpecs, options);
const createIndexesOperation = new CreateIndexesOperation(
this,
this.collectionName,
indexSpecs,
options
);
return executeOperation(this.s.topology, createIndexesOperation, callback);
};
@@ -1353,19 +1354,20 @@ 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 = function(options, callback) {
Collection.prototype.reIndex = deprecate(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.
@@ -1665,13 +1667,11 @@ Collection.prototype.findOneAndDelete = function(filter, options, callback) {
if (typeof options === 'function') (callback = options), (options = {});
options = options || {};
// 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);
return executeOperation(
this.s.topology,
new FindOneAndDeleteOperation(this, filter, options),
callback
);
};
/**
@@ -1683,6 +1683,7 @@ 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.
@@ -1699,27 +1700,11 @@ Collection.prototype.findOneAndReplace = function(filter, replacement, options,
if (typeof options === 'function') (callback = options), (options = {});
options = options || {};
// 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,
new FindOneAndReplaceOperation(this, filter, replacement, options),
callback
);
return executeOperation(this.s.topology, findOneAndReplaceOperation, callback);
};
/**
@@ -1732,6 +1717,7 @@ 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.
@@ -1740,7 +1726,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] optional session to use for this operation
* @param {ClientSession} [options.session] An ptional session to use for this operation
* @param {Collection~findAndModifyCallback} [callback] The collection result callback
* @return {Promise<Collection~findAndModifyWriteOpResultObject>} returns Promise if no callback passed
*/
@@ -1748,21 +1734,11 @@ Collection.prototype.findOneAndUpdate = function(filter, update, options, callba
if (typeof options === 'function') (callback = options), (options = {});
options = options || {};
// 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);
return executeOperation(
this.s.topology,
new FindOneAndUpdateOperation(this, filter, update, options),
callback
);
};
/**
@@ -1864,7 +1840,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 (MongoDB 3.4 or higher) settings for update operation (see 3.4 documentation for available fields).
* @param {object} [options.collation] Specify collation settings for operation. See {@link https://docs.mongodb.com/manual/reference/command/aggregate|aggregation documentation}.
* @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
@@ -1978,7 +1954,7 @@ Collection.prototype.parallelCollectionScan = deprecate(function(options, callba
options = Object.assign({}, options);
// Ensure we have the right read preference inheritance
options.readPreference = resolveReadPreference(this, options);
options.readPreference = ReadPreference.resolve(this, options);
// Add a promiseLibrary
options.promiseLibrary = this.s.promiseLibrary;
@@ -2009,8 +1985,9 @@ 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 = function(x, y, options, callback) {
Collection.prototype.geoHaystackSearch = deprecate(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() || {} : {};
@@ -2018,7 +1995,7 @@ Collection.prototype.geoHaystackSearch = function(x, y, options, callback) {
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