12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985 |
- 'use strict';
-
- const deprecate = require('util').deprecate;
- const deprecateOptions = require('./utils').deprecateOptions;
- const checkCollectionName = require('./utils').checkCollectionName;
- const ObjectID = require('mongodb-core').BSON.ObjectID;
- const MongoError = require('mongodb-core').MongoError;
- const toError = require('./utils').toError;
- const normalizeHintField = require('./utils').normalizeHintField;
- const handleCallback = require('./utils').handleCallback;
- const decorateCommand = require('./utils').decorateCommand;
- const decorateWithCollation = require('./utils').decorateWithCollation;
- const decorateWithReadConcern = require('./utils').decorateWithReadConcern;
- const formattedOrderClause = require('./utils').formattedOrderClause;
- const ReadPreference = require('mongodb-core').ReadPreference;
- const CommandCursor = require('./command_cursor');
- const unordered = require('./bulk/unordered');
- const ordered = require('./bulk/ordered');
- const ChangeStream = require('./change_stream');
- const executeOperation = require('./utils').executeOperation;
- const resolveReadPreference = require('./utils').resolveReadPreference;
-
- // Operations
- const aggregate = require('./operations/aggregate').aggregate;
- const bulkWrite = require('./operations/collection_ops').bulkWrite;
- const checkForAtomicOperators = require('./operations/collection_ops').checkForAtomicOperators;
- const count = require('./operations/collection_ops').count;
- const countDocuments = require('./operations/collection_ops').countDocuments;
- const createIndex = require('./operations/collection_ops').createIndex;
- const createIndexes = require('./operations/collection_ops').createIndexes;
- const deleteMany = require('./operations/collection_ops').deleteMany;
- const deleteOne = require('./operations/collection_ops').deleteOne;
- const distinct = require('./operations/collection_ops').distinct;
- const dropIndex = require('./operations/collection_ops').dropIndex;
- const dropIndexes = require('./operations/collection_ops').dropIndexes;
- const ensureIndex = require('./operations/collection_ops').ensureIndex;
- const findAndModify = require('./operations/collection_ops').findAndModify;
- const findAndRemove = require('./operations/collection_ops').findAndRemove;
- const findOne = require('./operations/collection_ops').findOne;
- const findOneAndDelete = require('./operations/collection_ops').findOneAndDelete;
- const findOneAndReplace = require('./operations/collection_ops').findOneAndReplace;
- const findOneAndUpdate = require('./operations/collection_ops').findOneAndUpdate;
- const geoHaystackSearch = require('./operations/collection_ops').geoHaystackSearch;
- const group = require('./operations/collection_ops').group;
- const indexes = require('./operations/collection_ops').indexes;
- const indexExists = require('./operations/collection_ops').indexExists;
- const indexInformation = require('./operations/collection_ops').indexInformation;
- const insertMany = require('./operations/collection_ops').insertMany;
- const insertOne = require('./operations/collection_ops').insertOne;
- const isCapped = require('./operations/collection_ops').isCapped;
- const mapReduce = require('./operations/collection_ops').mapReduce;
- const optionsOp = require('./operations/collection_ops').optionsOp;
- const parallelCollectionScan = require('./operations/collection_ops').parallelCollectionScan;
- const reIndex = require('./operations/collection_ops').reIndex;
- const removeDocuments = require('./operations/collection_ops').removeDocuments;
- const rename = require('./operations/collection_ops').rename;
- const replaceOne = require('./operations/collection_ops').replaceOne;
- const save = require('./operations/collection_ops').save;
- const stats = require('./operations/collection_ops').stats;
- const updateDocuments = require('./operations/collection_ops').updateDocuments;
- const updateMany = require('./operations/collection_ops').updateMany;
- const updateOne = require('./operations/collection_ops').updateOne;
-
- /**
- * @fileOverview The **Collection** class is an internal class that embodies a MongoDB collection
- * allowing for insert/update/remove/find and other command operation on that MongoDB collection.
- *
- * **COLLECTION Cannot directly be instantiated**
- * @example
- * const MongoClient = require('mongodb').MongoClient;
- * const test = require('assert');
- * // Connection url
- * const url = 'mongodb://localhost:27017';
- * // Database Name
- * const dbName = 'test';
- * // Connect using MongoClient
- * MongoClient.connect(url, function(err, client) {
- * // Create a collection we want to drop later
- * const col = client.db(dbName).collection('createIndexExample1');
- * // Show that duplicate records got dropped
- * col.find({}).toArray(function(err, items) {
- * test.equal(null, err);
- * test.equal(4, items.length);
- * client.close();
- * });
- * });
- */
-
- const mergeKeys = ['ignoreUndefined'];
-
- /**
- * Create a new Collection instance (INTERNAL TYPE, do not instantiate directly)
- * @class
- * @property {string} collectionName Get the collection name.
- * @property {string} namespace Get the full collection namespace.
- * @property {object} writeConcern The current write concern values.
- * @property {object} readConcern The current read concern values.
- * @property {object} hint Get current index hint for collection.
- * @return {Collection} a Collection instance.
- */
- function Collection(db, topology, dbName, name, pkFactory, options) {
- checkCollectionName(name);
-
- // Unpack variables
- const internalHint = null;
- const slaveOk = options == null || options.slaveOk == null ? db.slaveOk : options.slaveOk;
- const serializeFunctions =
- options == null || options.serializeFunctions == null
- ? db.s.options.serializeFunctions
- : options.serializeFunctions;
- const raw = options == null || options.raw == null ? db.s.options.raw : options.raw;
- const promoteLongs =
- options == null || options.promoteLongs == null
- ? db.s.options.promoteLongs
- : options.promoteLongs;
- const promoteValues =
- options == null || options.promoteValues == null
- ? db.s.options.promoteValues
- : options.promoteValues;
- const promoteBuffers =
- options == null || options.promoteBuffers == null
- ? db.s.options.promoteBuffers
- : options.promoteBuffers;
- let readPreference = null;
- const collectionHint = null;
- const namespace = `${dbName}.${name}`;
-
- // Get the promiseLibrary
- const promiseLibrary = options.promiseLibrary || Promise;
-
- // Assign the right collection level readPreference
- if (options && options.readPreference) {
- readPreference = options.readPreference;
- } else if (db.options.readPreference) {
- readPreference = db.options.readPreference;
- }
-
- // Set custom primary key factory if provided
- pkFactory = pkFactory == null ? ObjectID : pkFactory;
-
- // Internal state
- this.s = {
- // Set custom primary key factory if provided
- pkFactory: pkFactory,
- // Db
- db: db,
- // Topology
- topology: topology,
- // dbName
- dbName: dbName,
- // Options
- options: options,
- // Namespace
- namespace: namespace,
- // Read preference
- readPreference: readPreference,
- // SlaveOK
- slaveOk: slaveOk,
- // Serialize functions
- serializeFunctions: serializeFunctions,
- // Raw
- raw: raw,
- // promoteLongs
- promoteLongs: promoteLongs,
- // promoteValues
- promoteValues: promoteValues,
- // promoteBuffers
- promoteBuffers: promoteBuffers,
- // internalHint
- internalHint: internalHint,
- // collectionHint
- collectionHint: collectionHint,
- // Name
- name: name,
- // Promise library
- promiseLibrary: promiseLibrary,
- // Read Concern
- readConcern: options.readConcern,
- // Write Concern
- writeConcern: options.writeConcern
- };
- }
-
- Object.defineProperty(Collection.prototype, 'dbName', {
- enumerable: true,
- get: function() {
- return this.s.dbName;
- }
- });
-
- Object.defineProperty(Collection.prototype, 'collectionName', {
- enumerable: true,
- get: function() {
- return this.s.name;
- }
- });
-
- Object.defineProperty(Collection.prototype, 'namespace', {
- enumerable: true,
- get: function() {
- return this.s.namespace;
- }
- });
-
- Object.defineProperty(Collection.prototype, 'readConcern', {
- enumerable: true,
- get: function() {
- return this.s.readConcern || { level: 'local' };
- }
- });
-
- Object.defineProperty(Collection.prototype, 'writeConcern', {
- enumerable: true,
- get: function() {
- let ops = {};
- if (this.s.writeConcern) {
- return this.s.writeConcern;
- }
-
- if (this.s.options.w != null) ops.w = this.s.options.w;
- if (this.s.options.j != null) ops.j = this.s.options.j;
- if (this.s.options.fsync != null) ops.fsync = this.s.options.fsync;
- if (this.s.options.wtimeout != null) ops.wtimeout = this.s.options.wtimeout;
- return ops;
- }
- });
-
- /**
- * @ignore
- */
- Object.defineProperty(Collection.prototype, 'hint', {
- enumerable: true,
- get: function() {
- return this.s.collectionHint;
- },
- set: function(v) {
- this.s.collectionHint = normalizeHintField(v);
- }
- });
-
- const DEPRECATED_FIND_OPTIONS = ['maxScan', 'fields', 'snapshot'];
-
- /**
- * Creates a cursor for a query that can be used to iterate over results from MongoDB
- * @method
- * @param {object} [query={}] The cursor query object.
- * @param {object} [options] Optional settings.
- * @param {number} [options.limit=0] Sets the limit of documents returned in the query.
- * @param {(array|object)} [options.sort] Set to sort the documents coming back from the query. Array of indexes, [['a', 1]] etc.
- * @param {object} [options.projection] The fields to return in the query. Object of fields to include or exclude (not both), {'a':1}
- * @param {object} [options.fields] **Deprecated** Use `options.projection` instead
- * @param {number} [options.skip=0] Set to skip N documents ahead in your query (useful for pagination).
- * @param {Object} [options.hint] Tell the query to use specific indexes in the query. Object of indexes to use, {'_id':1}
- * @param {boolean} [options.explain=false] Explain the query instead of returning the data.
- * @param {boolean} [options.snapshot=false] DEPRECATED: Snapshot query.
- * @param {boolean} [options.timeout=false] Specify if the cursor can timeout.
- * @param {boolean} [options.tailable=false] Specify if the cursor is tailable.
- * @param {number} [options.batchSize=0] Set the batchSize for the getMoreCommand when iterating over the query results.
- * @param {boolean} [options.returnKey=false] Only return the index key.
- * @param {number} [options.maxScan] DEPRECATED: Limit the number of items to scan.
- * @param {number} [options.min] Set index bounds.
- * @param {number} [options.max] Set index bounds.
- * @param {boolean} [options.showDiskLoc=false] Show disk location of results.
- * @param {string} [options.comment] You can put a $comment field on a query to make looking in the profiler logs simpler.
- * @param {boolean} [options.raw=false] Return document results as raw BSON buffers.
- * @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 {(ReadPreference|string)} [options.readPreference] The preferred read preference (ReadPreference.PRIMARY, ReadPreference.PRIMARY_PREFERRED, ReadPreference.SECONDARY, ReadPreference.SECONDARY_PREFERRED, ReadPreference.NEAREST).
- * @param {boolean} [options.partial=false] Specify if the cursor should return partial results when querying against a sharded system
- * @param {number} [options.maxTimeMS] Number of milliseconds to wait before aborting the query.
- * @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
- * @throws {MongoError}
- * @return {Cursor}
- */
- Collection.prototype.find = deprecateOptions(
- {
- name: 'collection.find',
- deprecatedOptions: DEPRECATED_FIND_OPTIONS,
- optionsIndex: 1
- },
- function(query, options, callback) {
- if (typeof callback === 'object') {
- // TODO(MAJOR): throw in the future
- console.warn('Third parameter to `find()` must be a callback or undefined');
- }
-
- let selector = query;
- // figuring out arguments
- if (typeof callback !== 'function') {
- if (typeof options === 'function') {
- callback = options;
- options = undefined;
- } else if (options == null) {
- callback = typeof selector === 'function' ? selector : undefined;
- selector = typeof selector === 'object' ? selector : undefined;
- }
- }
-
- // Ensure selector is not null
- selector = selector == null ? {} : selector;
- // Validate correctness off the selector
- const object = selector;
- if (Buffer.isBuffer(object)) {
- const object_size = object[0] | (object[1] << 8) | (object[2] << 16) | (object[3] << 24);
- if (object_size !== object.length) {
- const error = new Error(
- 'query selector raw message size does not match message header size [' +
- object.length +
- '] != [' +
- object_size +
- ']'
- );
- error.name = 'MongoError';
- throw error;
- }
- }
-
- // Check special case where we are using an objectId
- if (selector != null && selector._bsontype === 'ObjectID') {
- selector = { _id: selector };
- }
-
- if (!options) options = {};
-
- let projection = options.projection || options.fields;
-
- if (projection && !Buffer.isBuffer(projection) && Array.isArray(projection)) {
- projection = projection.length
- ? projection.reduce((result, field) => {
- result[field] = 1;
- return result;
- }, {})
- : { _id: 1 };
- }
-
- // Make a shallow copy of options
- let newOptions = Object.assign({}, options);
-
- // Make a shallow copy of the collection options
- for (let key in this.s.options) {
- if (mergeKeys.indexOf(key) !== -1) {
- newOptions[key] = this.s.options[key];
- }
- }
-
- // Unpack options
- newOptions.skip = options.skip ? options.skip : 0;
- newOptions.limit = options.limit ? options.limit : 0;
- newOptions.raw = typeof options.raw === 'boolean' ? options.raw : this.s.raw;
- newOptions.hint =
- options.hint != null ? normalizeHintField(options.hint) : this.s.collectionHint;
- newOptions.timeout = typeof options.timeout === 'undefined' ? undefined : options.timeout;
- // // If we have overridden slaveOk otherwise use the default db setting
- newOptions.slaveOk = options.slaveOk != null ? options.slaveOk : this.s.db.slaveOk;
-
- // Add read preference if needed
- newOptions.readPreference = resolveReadPreference(newOptions, {
- db: this.s.db,
- collection: this
- });
-
- // Set slave ok to true if read preference different from primary
- if (
- newOptions.readPreference != null &&
- (newOptions.readPreference !== 'primary' || newOptions.readPreference.mode !== 'primary')
- ) {
- newOptions.slaveOk = true;
- }
-
- // Ensure the query is an object
- if (selector != null && typeof selector !== 'object') {
- throw MongoError.create({ message: 'query selector must be an object', driver: true });
- }
-
- // Build the find command
- const findCommand = {
- find: this.s.namespace,
- limit: newOptions.limit,
- skip: newOptions.skip,
- query: selector
- };
-
- // Ensure we use the right await data option
- if (typeof newOptions.awaitdata === 'boolean') {
- newOptions.awaitData = newOptions.awaitdata;
- }
-
- // Translate to new command option noCursorTimeout
- if (typeof newOptions.timeout === 'boolean') newOptions.noCursorTimeout = newOptions.timeout;
-
- decorateCommand(findCommand, newOptions, ['session', 'collation']);
-
- if (projection) findCommand.fields = projection;
-
- // Add db object to the new options
- newOptions.db = this.s.db;
-
- // Add the promise library
- newOptions.promiseLibrary = this.s.promiseLibrary;
-
- // Set raw if available at collection level
- if (newOptions.raw == null && typeof this.s.raw === 'boolean') newOptions.raw = this.s.raw;
- // Set promoteLongs if available at collection level
- if (newOptions.promoteLongs == null && typeof this.s.promoteLongs === 'boolean')
- newOptions.promoteLongs = this.s.promoteLongs;
- if (newOptions.promoteValues == null && typeof this.s.promoteValues === 'boolean')
- newOptions.promoteValues = this.s.promoteValues;
- if (newOptions.promoteBuffers == null && typeof this.s.promoteBuffers === 'boolean')
- newOptions.promoteBuffers = this.s.promoteBuffers;
-
- // Sort options
- if (findCommand.sort) {
- findCommand.sort = formattedOrderClause(findCommand.sort);
- }
-
- // Set the readConcern
- decorateWithReadConcern(findCommand, this, options);
-
- // Decorate find command with collation options
- try {
- decorateWithCollation(findCommand, this, options);
- } catch (err) {
- if (typeof callback === 'function') return callback(err, null);
- throw err;
- }
-
- const cursor = this.s.topology.cursor(this.s.namespace, findCommand, newOptions);
-
- return typeof callback === 'function' ? handleCallback(callback, null, cursor) : cursor;
- }
- );
-
- /**
- * Inserts a single document into MongoDB. If documents passed in do not contain the **_id** field,
- * one will be added to each of the documents missing it by the driver, mutating the document. This behavior
- * can be overridden by setting the **forceServerObjectId** flag.
- *
- * @method
- * @param {object} doc Document to insert.
- * @param {object} [options] Optional settings.
- * @param {(number|string)} [options.w] The write concern.
- * @param {number} [options.wtimeout] The write concern timeout.
- * @param {boolean} [options.j=false] Specify a journal write concern.
- * @param {boolean} [options.serializeFunctions=false] Serialize functions on any object.
- * @param {boolean} [options.forceServerObjectId=false] Force server to assign _id values instead of driver.
- * @param {boolean} [options.bypassDocumentValidation=false] Allow driver to bypass schema validation in MongoDB 3.2 or higher.
- * @param {ClientSession} [options.session] optional session to use for this operation
- * @param {Collection~insertOneWriteOpCallback} [callback] The command result callback
- * @return {Promise} returns Promise if no callback passed
- */
- Collection.prototype.insertOne = function(doc, options, callback) {
- if (typeof options === 'function') (callback = options), (options = {});
- options = options || {};
-
- // Add ignoreUndefined
- if (this.s.options.ignoreUndefined) {
- options = Object.assign({}, options);
- options.ignoreUndefined = this.s.options.ignoreUndefined;
- }
-
- return executeOperation(this.s.topology, insertOne, [this, doc, options, callback]);
- };
-
- /**
- * Inserts an array of documents into MongoDB. If documents passed in do not contain the **_id** field,
- * one will be added to each of the documents missing it by the driver, mutating the document. This behavior
- * can be overridden by setting the **forceServerObjectId** flag.
- *
- * @method
- * @param {object[]} docs Documents to insert.
- * @param {object} [options] Optional settings.
- * @param {(number|string)} [options.w] The write concern.
- * @param {number} [options.wtimeout] The write concern timeout.
- * @param {boolean} [options.j=false] Specify a journal write concern.
- * @param {boolean} [options.serializeFunctions=false] Serialize functions on any object.
- * @param {boolean} [options.forceServerObjectId=false] Force server to assign _id values instead of driver.
- * @param {boolean} [options.bypassDocumentValidation=false] Allow driver to bypass schema validation in MongoDB 3.2 or higher.
- * @param {boolean} [options.ordered=true] If true, when an insert fails, don't execute the remaining writes. If false, continue with remaining inserts when one fails.
- * @param {ClientSession} [options.session] optional session to use for this operation
- * @param {Collection~insertWriteOpCallback} [callback] The command result callback
- * @return {Promise} returns Promise if no callback passed
- */
- Collection.prototype.insertMany = function(docs, options, callback) {
- if (typeof options === 'function') (callback = options), (options = {});
- options = options ? Object.assign({}, options) : { ordered: true };
-
- return executeOperation(this.s.topology, insertMany, [this, docs, options, callback]);
- };
-
- /**
- * @typedef {Object} Collection~BulkWriteOpResult
- * @property {number} insertedCount Number of documents inserted.
- * @property {number} matchedCount Number of documents matched for update.
- * @property {number} modifiedCount Number of documents modified.
- * @property {number} deletedCount Number of documents deleted.
- * @property {number} upsertedCount Number of documents upserted.
- * @property {object} insertedIds Inserted document generated Id's, hash key is the index of the originating operation
- * @property {object} upsertedIds Upserted document generated Id's, hash key is the index of the originating operation
- * @property {object} result The command result object.
- */
-
- /**
- * The callback format for inserts
- * @callback Collection~bulkWriteOpCallback
- * @param {BulkWriteError} error An error instance representing the error during the execution.
- * @param {Collection~BulkWriteOpResult} result The result object if the command was executed successfully.
- */
-
- /**
- * Perform a bulkWrite operation without a fluent API
- *
- * Legal operation types are
- *
- * { insertOne: { document: { a: 1 } } }
- *
- * { updateOne: { filter: {a:2}, update: {$set: {a:2}}, upsert:true } }
- *
- * { updateMany: { filter: {a:2}, update: {$set: {a:2}}, upsert:true } }
- *
- * { deleteOne: { filter: {c:1} } }
- *
- * { deleteMany: { filter: {c:1} } }
- *
- * { replaceOne: { filter: {c:3}, replacement: {c:4}, upsert:true}}
- *
- * If documents passed in do not contain the **_id** field,
- * one will be added to each of the documents missing it by the driver, mutating the document. This behavior
- * can be overridden by setting the **forceServerObjectId** flag.
- *
- * @method
- * @param {object[]} operations Bulk operations to perform.
- * @param {object} [options] Optional settings.
- * @param {(number|string)} [options.w] The write concern.
- * @param {number} [options.wtimeout] The write concern timeout.
- * @param {boolean} [options.j=false] Specify a journal write concern.
- * @param {boolean} [options.serializeFunctions=false] Serialize functions on any object.
- * @param {boolean} [options.ordered=true] Execute write operation in ordered or unordered fashion.
- * @param {boolean} [options.bypassDocumentValidation=false] Allow driver to bypass schema validation in MongoDB 3.2 or higher.
- * @param {ClientSession} [options.session] optional session to use for this operation
- * @param {Collection~bulkWriteOpCallback} [callback] The command result callback
- * @return {Promise} returns Promise if no callback passed
- */
- Collection.prototype.bulkWrite = function(operations, options, callback) {
- if (typeof options === 'function') (callback = options), (options = {});
- options = options || { ordered: true };
-
- if (!Array.isArray(operations)) {
- throw MongoError.create({ message: 'operations must be an array of documents', driver: true });
- }
-
- return executeOperation(this.s.topology, bulkWrite, [this, operations, options, callback]);
- };
-
- /**
- * @typedef {Object} Collection~WriteOpResult
- * @property {object[]} ops All the documents inserted using insertOne/insertMany/replaceOne. Documents contain the _id field if forceServerObjectId == false for insertOne/insertMany
- * @property {object} connection The connection object used for the operation.
- * @property {object} result The command result object.
- */
-
- /**
- * The callback format for inserts
- * @callback Collection~writeOpCallback
- * @param {MongoError} error An error instance representing the error during the execution.
- * @param {Collection~WriteOpResult} result The result object if the command was executed successfully.
- */
-
- /**
- * @typedef {Object} Collection~insertWriteOpResult
- * @property {Number} insertedCount The total amount of documents inserted.
- * @property {object[]} ops All the documents inserted using insertOne/insertMany/replaceOne. Documents contain the _id field if forceServerObjectId == false for insertOne/insertMany
- * @property {Object.<Number, ObjectId>} insertedIds Map of the index of the inserted document to the id of the inserted document.
- * @property {object} connection The connection object used for the operation.
- * @property {object} result The raw command result object returned from MongoDB (content might vary by server version).
- * @property {Number} result.ok Is 1 if the command executed correctly.
- * @property {Number} result.n The total count of documents inserted.
- */
-
- /**
- * @typedef {Object} Collection~insertOneWriteOpResult
- * @property {Number} insertedCount The total amount of documents inserted.
- * @property {object[]} ops All the documents inserted using insertOne/insertMany/replaceOne. Documents contain the _id field if forceServerObjectId == false for insertOne/insertMany
- * @property {ObjectId} insertedId The driver generated ObjectId for the insert operation.
- * @property {object} connection The connection object used for the operation.
- * @property {object} result The raw command result object returned from MongoDB (content might vary by server version).
- * @property {Number} result.ok Is 1 if the command executed correctly.
- * @property {Number} result.n The total count of documents inserted.
- */
-
- /**
- * The callback format for inserts
- * @callback Collection~insertWriteOpCallback
- * @param {MongoError} error An error instance representing the error during the execution.
- * @param {Collection~insertWriteOpResult} result The result object if the command was executed successfully.
- */
-
- /**
- * The callback format for inserts
- * @callback Collection~insertOneWriteOpCallback
- * @param {MongoError} error An error instance representing the error during the execution.
- * @param {Collection~insertOneWriteOpResult} result The result object if the command was executed successfully.
- */
-
- /**
- * Inserts a single document or a an array of documents into MongoDB. If documents passed in do not contain the **_id** field,
- * one will be added to each of the documents missing it by the driver, mutating the document. This behavior
- * can be overridden by setting the **forceServerObjectId** flag.
- *
- * @method
- * @param {(object|object[])} docs Documents to insert.
- * @param {object} [options] Optional settings.
- * @param {(number|string)} [options.w] The write concern.
- * @param {number} [options.wtimeout] The write concern timeout.
- * @param {boolean} [options.j=false] Specify a journal write concern.
- * @param {boolean} [options.serializeFunctions=false] Serialize functions on any object.
- * @param {boolean} [options.forceServerObjectId=false] Force server to assign _id values instead of driver.
- * @param {boolean} [options.bypassDocumentValidation=false] Allow driver to bypass schema validation in MongoDB 3.2 or higher.
- * @param {ClientSession} [options.session] optional session to use for this operation
- * @param {Collection~insertWriteOpCallback} [callback] The command result callback
- * @return {Promise} returns Promise if no callback passed
- * @deprecated Use insertOne, insertMany or bulkWrite
- */
- Collection.prototype.insert = deprecate(function(docs, options, callback) {
- if (typeof options === 'function') (callback = options), (options = {});
- options = options || { ordered: false };
- docs = !Array.isArray(docs) ? [docs] : docs;
-
- if (options.keepGoing === true) {
- options.ordered = false;
- }
-
- return this.insertMany(docs, options, callback);
- }, 'collection.insert is deprecated. Use insertOne, insertMany or bulkWrite instead.');
-
- /**
- * @typedef {Object} Collection~updateWriteOpResult
- * @property {Object} result The raw result returned from MongoDB. Will vary depending on server version.
- * @property {Number} result.ok Is 1 if the command executed correctly.
- * @property {Number} result.n The total count of documents scanned.
- * @property {Number} result.nModified The total count of documents modified.
- * @property {Object} connection The connection object used for the operation.
- * @property {Number} matchedCount The number of documents that matched the filter.
- * @property {Number} modifiedCount The number of documents that were modified.
- * @property {Number} upsertedCount The number of documents upserted.
- * @property {Object} upsertedId The upserted id.
- * @property {ObjectId} upsertedId._id The upserted _id returned from the server.
- * @property {Object} message
- * @property {Array} ops
- */
-
- /**
- * The callback format for inserts
- * @callback Collection~updateWriteOpCallback
- * @param {MongoError} error An error instance representing the error during the execution.
- * @param {Collection~updateWriteOpResult} result The result object if the command was executed successfully.
- */
-
- /**
- * Update a single document in a collection
- * @method
- * @param {object} filter The Filter used to select the document to update
- * @param {object} update The update operations to be applied to the document
- * @param {object} [options] Optional settings.
- * @param {boolean} [options.upsert=false] Update operation is an upsert.
- * @param {(number|string)} [options.w] The write concern.
- * @param {number} [options.wtimeout] The write concern timeout.
- * @param {boolean} [options.j=false] Specify a journal write concern.
- * @param {boolean} [options.bypassDocumentValidation=false] Allow driver to bypass schema validation in MongoDB 3.2 or higher.
- * @param {Array} [options.arrayFilters] optional list of array filters referenced in filtered positional operators
- * @param {ClientSession} [options.session] optional session to use for this operation
- * @param {Collection~updateWriteOpCallback} [callback] The command result callback
- * @return {Promise} returns Promise if no callback passed
- */
- 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
- if (this.s.options.ignoreUndefined) {
- options = Object.assign({}, options);
- options.ignoreUndefined = this.s.options.ignoreUndefined;
- }
-
- return executeOperation(this.s.topology, updateOne, [this, filter, update, options, callback]);
- };
-
- /**
- * Replace a document in a collection with another document
- * @method
- * @param {object} filter The Filter used to select the document to replace
- * @param {object} doc The Document that replaces the matching document
- * @param {object} [options] Optional settings.
- * @param {boolean} [options.upsert=false] Update operation is an upsert.
- * @param {(number|string)} [options.w] The write concern.
- * @param {number} [options.wtimeout] The write concern timeout.
- * @param {boolean} [options.j=false] Specify a journal write concern.
- * @param {boolean} [options.bypassDocumentValidation=false] Allow driver to bypass schema validation in MongoDB 3.2 or higher.
- * @param {ClientSession} [options.session] optional session to use for this operation
- * @param {Collection~updateWriteOpCallback} [callback] The command result callback
- * @return {Promise<Collection~updatewriteOpResultObject>} returns Promise if no callback passed
- */
- Collection.prototype.replaceOne = function(filter, doc, options, callback) {
- if (typeof options === 'function') (callback = options), (options = {});
- options = Object.assign({}, options);
-
- // Add ignoreUndefined
- if (this.s.options.ignoreUndefined) {
- options = Object.assign({}, options);
- options.ignoreUndefined = this.s.options.ignoreUndefined;
- }
-
- return executeOperation(this.s.topology, replaceOne, [this, filter, doc, options, callback]);
- };
-
- /**
- * Update multiple documents in a collection
- * @method
- * @param {object} filter The Filter used to select the documents to update
- * @param {object} update The update operations to be applied to the documents
- * @param {object} [options] Optional settings.
- * @param {boolean} [options.upsert=false] Update operation is an upsert.
- * @param {(number|string)} [options.w] The write concern.
- * @param {number} [options.wtimeout] The write concern timeout.
- * @param {boolean} [options.j=false] Specify a journal write concern.
- * @param {Array} [options.arrayFilters] optional list of array filters referenced in filtered positional operators
- * @param {ClientSession} [options.session] optional session to use for this operation
- * @param {Collection~updateWriteOpCallback} [callback] The command result callback
- * @return {Promise<Collection~updateWriteOpResultObject>} returns Promise if no callback passed
- */
- 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
- if (this.s.options.ignoreUndefined) {
- options = Object.assign({}, options);
- options.ignoreUndefined = this.s.options.ignoreUndefined;
- }
-
- return executeOperation(this.s.topology, updateMany, [this, filter, update, options, callback]);
- };
-
- /**
- * Updates documents.
- * @method
- * @param {object} selector The selector for the update operation.
- * @param {object} update The update operations to be applied to the documents
- * @param {object} [options] Optional settings.
- * @param {(number|string)} [options.w] The write concern.
- * @param {number} [options.wtimeout] The write concern timeout.
- * @param {boolean} [options.j=false] Specify a journal write concern.
- * @param {boolean} [options.upsert=false] Update operation is an upsert.
- * @param {boolean} [options.multi=false] Update one/all documents with operation.
- * @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 {Array} [options.arrayFilters] optional list of array filters referenced in filtered positional operators
- * @param {ClientSession} [options.session] optional session to use for this operation
- * @param {Collection~writeOpCallback} [callback] The command result callback
- * @throws {MongoError}
- * @return {Promise} returns Promise if no callback passed
- * @deprecated use updateOne, updateMany or bulkWrite
- */
- Collection.prototype.update = deprecate(function(selector, update, options, callback) {
- if (typeof options === 'function') (callback = options), (options = {});
- options = options || {};
-
- // Add ignoreUndefined
- if (this.s.options.ignoreUndefined) {
- options = Object.assign({}, options);
- options.ignoreUndefined = this.s.options.ignoreUndefined;
- }
-
- return executeOperation(this.s.topology, updateDocuments, [
- this,
- selector,
- update,
- options,
- callback
- ]);
- }, 'collection.update is deprecated. Use updateOne, updateMany, or bulkWrite instead.');
-
- /**
- * @typedef {Object} Collection~deleteWriteOpResult
- * @property {Object} result The raw result returned from MongoDB. Will vary depending on server version.
- * @property {Number} result.ok Is 1 if the command executed correctly.
- * @property {Number} result.n The total count of documents deleted.
- * @property {Object} connection The connection object used for the operation.
- * @property {Number} deletedCount The number of documents deleted.
- */
-
- /**
- * The callback format for inserts
- * @callback Collection~deleteWriteOpCallback
- * @param {MongoError} error An error instance representing the error during the execution.
- * @param {Collection~deleteWriteOpResult} result The result object if the command was executed successfully.
- */
-
- /**
- * Delete a document from a collection
- * @method
- * @param {object} filter The Filter used to select the document to remove
- * @param {object} [options] Optional settings.
- * @param {(number|string)} [options.w] The write concern.
- * @param {number} [options.wtimeout] The write concern timeout.
- * @param {boolean} [options.j=false] Specify a journal write concern.
- * @param {ClientSession} [options.session] optional session to use for this operation
- * @param {Collection~deleteWriteOpCallback} [callback] The command result callback
- * @return {Promise} returns Promise if no callback passed
- */
- Collection.prototype.deleteOne = function(filter, options, callback) {
- if (typeof options === 'function') (callback = options), (options = {});
- options = Object.assign({}, options);
-
- // Add ignoreUndefined
- if (this.s.options.ignoreUndefined) {
- options = Object.assign({}, options);
- options.ignoreUndefined = this.s.options.ignoreUndefined;
- }
-
- return executeOperation(this.s.topology, deleteOne, [this, filter, options, callback]);
- };
-
- Collection.prototype.removeOne = Collection.prototype.deleteOne;
-
- /**
- * Delete multiple documents from a collection
- * @method
- * @param {object} filter The Filter used to select the documents to remove
- * @param {object} [options] Optional settings.
- * @param {(number|string)} [options.w] The write concern.
- * @param {number} [options.wtimeout] The write concern timeout.
- * @param {boolean} [options.j=false] Specify a journal write concern.
- * @param {ClientSession} [options.session] optional session to use for this operation
- * @param {Collection~deleteWriteOpCallback} [callback] The command result callback
- * @return {Promise} returns Promise if no callback passed
- */
- Collection.prototype.deleteMany = function(filter, options, callback) {
- if (typeof options === 'function') (callback = options), (options = {});
- options = Object.assign({}, options);
-
- // Add ignoreUndefined
- if (this.s.options.ignoreUndefined) {
- options = Object.assign({}, options);
- options.ignoreUndefined = this.s.options.ignoreUndefined;
- }
-
- return executeOperation(this.s.topology, deleteMany, [this, filter, options, callback]);
- };
-
- Collection.prototype.removeMany = Collection.prototype.deleteMany;
-
- /**
- * Remove documents.
- * @method
- * @param {object} selector The selector for the update operation.
- * @param {object} [options] Optional settings.
- * @param {(number|string)} [options.w] The write concern.
- * @param {number} [options.wtimeout] The write concern timeout.
- * @param {boolean} [options.j=false] Specify a journal write concern.
- * @param {boolean} [options.single=false] Removes the first document found.
- * @param {ClientSession} [options.session] optional session to use for this operation
- * @param {Collection~writeOpCallback} [callback] The command result callback
- * @return {Promise} returns Promise if no callback passed
- * @deprecated use deleteOne, deleteMany or bulkWrite
- */
- Collection.prototype.remove = deprecate(function(selector, options, callback) {
- if (typeof options === 'function') (callback = options), (options = {});
- options = options || {};
-
- // Add ignoreUndefined
- if (this.s.options.ignoreUndefined) {
- options = Object.assign({}, options);
- options.ignoreUndefined = this.s.options.ignoreUndefined;
- }
-
- return executeOperation(this.s.topology, removeDocuments, [this, selector, options, callback]);
- }, 'collection.remove is deprecated. Use deleteOne, deleteMany, or bulkWrite instead.');
-
- /**
- * Save a document. Simple full document replacement function. Not recommended for efficiency, use atomic
- * operators and update instead for more efficient operations.
- * @method
- * @param {object} doc Document to save
- * @param {object} [options] Optional settings.
- * @param {(number|string)} [options.w] The write concern.
- * @param {number} [options.wtimeout] The write concern timeout.
- * @param {boolean} [options.j=false] Specify a journal write concern.
- * @param {ClientSession} [options.session] optional session to use for this operation
- * @param {Collection~writeOpCallback} [callback] The command result callback
- * @return {Promise} returns Promise if no callback passed
- * @deprecated use insertOne, insertMany, updateOne or updateMany
- */
- Collection.prototype.save = deprecate(function(doc, options, callback) {
- if (typeof options === 'function') (callback = options), (options = {});
- options = options || {};
-
- // Add ignoreUndefined
- if (this.s.options.ignoreUndefined) {
- options = Object.assign({}, options);
- options.ignoreUndefined = this.s.options.ignoreUndefined;
- }
-
- return executeOperation(this.s.topology, save, [this, doc, options, callback]);
- }, 'collection.save is deprecated. Use insertOne, insertMany, updateOne, or updateMany instead.');
-
- /**
- * The callback format for results
- * @callback Collection~resultCallback
- * @param {MongoError} error An error instance representing the error during the execution.
- * @param {object} result The result object if the command was executed successfully.
- */
-
- /**
- * The callback format for an aggregation call
- * @callback Collection~aggregationCallback
- * @param {MongoError} error An error instance representing the error during the execution.
- * @param {AggregationCursor} cursor The cursor if the aggregation command was executed successfully.
- */
-
- /**
- * Fetches the first document that matches the query
- * @method
- * @param {object} query Query for find Operation
- * @param {object} [options] Optional settings.
- * @param {number} [options.limit=0] Sets the limit of documents returned in the query.
- * @param {(array|object)} [options.sort] Set to sort the documents coming back from the query. Array of indexes, [['a', 1]] etc.
- * @param {object} [options.projection] The fields to return in the query. Object of fields to include or exclude (not both), {'a':1}
- * @param {object} [options.fields] **Deprecated** Use `options.projection` instead
- * @param {number} [options.skip=0] Set to skip N documents ahead in your query (useful for pagination).
- * @param {Object} [options.hint] Tell the query to use specific indexes in the query. Object of indexes to use, {'_id':1}
- * @param {boolean} [options.explain=false] Explain the query instead of returning the data.
- * @param {boolean} [options.snapshot=false] DEPRECATED: Snapshot query.
- * @param {boolean} [options.timeout=false] Specify if the cursor can timeout.
- * @param {boolean} [options.tailable=false] Specify if the cursor is tailable.
- * @param {number} [options.batchSize=0] Set the batchSize for the getMoreCommand when iterating over the query results.
- * @param {boolean} [options.returnKey=false] Only return the index key.
- * @param {number} [options.maxScan] DEPRECATED: Limit the number of items to scan.
- * @param {number} [options.min] Set index bounds.
- * @param {number} [options.max] Set index bounds.
- * @param {boolean} [options.showDiskLoc=false] Show disk location of results.
- * @param {string} [options.comment] You can put a $comment field on a query to make looking in the profiler logs simpler.
- * @param {boolean} [options.raw=false] Return document results as raw BSON buffers.
- * @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 {(ReadPreference|string)} [options.readPreference] The preferred read preference (ReadPreference.PRIMARY, ReadPreference.PRIMARY_PREFERRED, ReadPreference.SECONDARY, ReadPreference.SECONDARY_PREFERRED, ReadPreference.NEAREST).
- * @param {boolean} [options.partial=false] Specify if the cursor should return partial results when querying against a sharded system
- * @param {number} [options.maxTimeMS] Number of milliseconds to wait before aborting the query.
- * @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 {Collection~resultCallback} [callback] The command result callback
- * @return {Promise} returns Promise if no callback passed
- */
- Collection.prototype.findOne = deprecateOptions(
- {
- name: 'collection.find',
- deprecatedOptions: DEPRECATED_FIND_OPTIONS,
- optionsIndex: 1
- },
- function(query, options, callback) {
- if (typeof callback === 'object') {
- // TODO(MAJOR): throw in the future
- console.warn('Third parameter to `findOne()` must be a callback or undefined');
- }
-
- if (typeof query === 'function') (callback = query), (query = {}), (options = {});
- if (typeof options === 'function') (callback = options), (options = {});
- query = query || {};
- options = options || {};
-
- return executeOperation(this.s.topology, findOne, [this, query, options, callback]);
- }
- );
-
- /**
- * The callback format for the collection method, must be used if strict is specified
- * @callback Collection~collectionResultCallback
- * @param {MongoError} error An error instance representing the error during the execution.
- * @param {Collection} collection The collection instance.
- */
-
- /**
- * Rename the collection.
- *
- * @method
- * @param {string} newName New name of of the collection.
- * @param {object} [options] Optional settings.
- * @param {boolean} [options.dropTarget=false] Drop the target name collection if it previously exists.
- * @param {ClientSession} [options.session] optional session to use for this operation
- * @param {Collection~collectionResultCallback} [callback] The results callback
- * @return {Promise} returns Promise if no callback passed
- */
- Collection.prototype.rename = function(newName, options, callback) {
- if (typeof options === 'function') (callback = options), (options = {});
- options = Object.assign({}, options, { readPreference: ReadPreference.PRIMARY });
-
- return executeOperation(this.s.topology, rename, [this, newName, options, callback]);
- };
-
- /**
- * Drop the collection from the database, removing it permanently. New accesses will create a new collection.
- *
- * @method
- * @param {object} [options] Optional settings.
- * @param {ClientSession} [options.session] optional session to use for this operation
- * @param {Collection~resultCallback} [callback] The results callback
- * @return {Promise} returns Promise if no callback passed
- */
- Collection.prototype.drop = function(options, callback) {
- if (typeof options === 'function') (callback = options), (options = {});
- options = options || {};
-
- return executeOperation(this.s.topology, this.s.db.dropCollection.bind(this.s.db), [
- this.s.name,
- options,
- callback
- ]);
- };
-
- /**
- * Returns the options of the collection.
- *
- * @method
- * @param {Object} [options] Optional settings
- * @param {ClientSession} [options.session] optional session to use for this operation
- * @param {Collection~resultCallback} [callback] The results callback
- * @return {Promise} returns Promise if no callback passed
- */
- Collection.prototype.options = function(opts, callback) {
- if (typeof opts === 'function') (callback = opts), (opts = {});
- opts = opts || {};
-
- return executeOperation(this.s.topology, optionsOp, [this, opts, callback]);
- };
-
- /**
- * Returns if the collection is a capped collection
- *
- * @method
- * @param {Object} [options] Optional settings
- * @param {ClientSession} [options.session] optional session to use for this operation
- * @param {Collection~resultCallback} [callback] The results callback
- * @return {Promise} returns Promise if no callback passed
- */
- Collection.prototype.isCapped = function(options, callback) {
- if (typeof options === 'function') (callback = options), (options = {});
- options = options || {};
-
- return executeOperation(this.s.topology, isCapped, [this, options, callback]);
- };
-
- /**
- * Creates an index on the db and collection collection.
- * @method
- * @param {(string|object)} fieldOrSpec Defines the index.
- * @param {object} [options] Optional settings.
- * @param {(number|string)} [options.w] The write concern.
- * @param {number} [options.wtimeout] The write concern timeout.
- * @param {boolean} [options.j=false] Specify a journal write concern.
- * @param {boolean} [options.unique=false] Creates an unique index.
- * @param {boolean} [options.sparse=false] Creates a sparse index.
- * @param {boolean} [options.background=false] Creates the index in the background, yielding whenever possible.
- * @param {boolean} [options.dropDups=false] A unique index cannot be created on a key that has pre-existing duplicate values. If you would like to create the index anyway, keeping the first document the database indexes and deleting all subsequent documents that have duplicate value
- * @param {number} [options.min] For geospatial indexes set the lower bound for the co-ordinates.
- * @param {number} [options.max] For geospatial indexes set the high bound for the co-ordinates.
- * @param {number} [options.v] Specify the format version of the indexes.
- * @param {number} [options.expireAfterSeconds] Allows you to expire data on indexes applied to a data (MongoDB 2.2 or higher)
- * @param {string} [options.name] Override the autogenerated index name (useful if the resulting name is larger than 128 bytes)
- * @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 {Collection~resultCallback} [callback] The command result callback
- * @return {Promise} returns Promise if no callback passed
- */
- Collection.prototype.createIndex = function(fieldOrSpec, options, callback) {
- if (typeof options === 'function') (callback = options), (options = {});
- options = options || {};
-
- return executeOperation(this.s.topology, createIndex, [this, fieldOrSpec, options, callback]);
- };
-
- /**
- * Creates multiple indexes in the collection, this method is only supported for
- * MongoDB 2.6 or higher. Earlier version of MongoDB will throw a command not supported
- * error. Index specifications are defined at http://docs.mongodb.org/manual/reference/command/createIndexes/.
- * @method
- * @param {array} 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 {Collection~resultCallback} [callback] The command result callback
- * @return {Promise} returns Promise if no callback passed
- */
- 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;
-
- return executeOperation(this.s.topology, createIndexes, [this, indexSpecs, options, callback]);
- };
-
- /**
- * Drops an index from this collection.
- * @method
- * @param {string} indexName Name of the index to drop.
- * @param {object} [options] Optional settings.
- * @param {(number|string)} [options.w] The write concern.
- * @param {number} [options.wtimeout] The write concern timeout.
- * @param {boolean} [options.j=false] Specify a journal write concern.
- * @param {ClientSession} [options.session] optional session to use for this operation
- * @param {number} [options.maxTimeMS] Number of milliseconds to wait before aborting the query.
- * @param {Collection~resultCallback} [callback] The command result callback
- * @return {Promise} returns Promise if no callback passed
- */
- Collection.prototype.dropIndex = function(indexName, options, callback) {
- const args = Array.prototype.slice.call(arguments, 1);
- callback = typeof args[args.length - 1] === 'function' ? args.pop() : undefined;
-
- options = args.length ? args.shift() || {} : {};
- // Run only against primary
- options.readPreference = ReadPreference.PRIMARY;
-
- return executeOperation(this.s.topology, dropIndex, [this, indexName, options, callback]);
- };
-
- /**
- * Drops all indexes from this collection.
- * @method
- * @param {Object} [options] Optional settings
- * @param {ClientSession} [options.session] optional session to use for this operation
- * @param {number} [options.maxTimeMS] Number of milliseconds to wait before aborting the query.
- * @param {Collection~resultCallback} [callback] The command result callback
- * @return {Promise} returns Promise if no callback passed
- */
- Collection.prototype.dropIndexes = function(options, callback) {
- if (typeof options === 'function') (callback = options), (options = {});
- options = options ? Object.assign({}, options) : {};
-
- if (typeof options.maxTimeMS !== 'number') delete options.maxTimeMS;
-
- return executeOperation(this.s.topology, dropIndexes, [this, options, callback]);
- };
-
- /**
- * Drops all indexes from this collection.
- * @method
- * @deprecated use dropIndexes
- * @param {Collection~resultCallback} callback The command result callback
- * @return {Promise} returns Promise if no [callback] passed
- */
- Collection.prototype.dropAllIndexes = deprecate(
- Collection.prototype.dropIndexes,
- 'collection.dropAllIndexes is deprecated. Use dropIndexes instead.'
- );
-
- /**
- * 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
- * @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) {
- if (typeof options === 'function') (callback = options), (options = {});
- options = options || {};
-
- return executeOperation(this.s.topology, reIndex, [this, options, callback]);
- };
-
- /**
- * Get the list of all indexes information for the collection.
- *
- * @method
- * @param {object} [options] Optional settings.
- * @param {number} [options.batchSize] The batchSize for the returned command cursor or if pre 2.8 the systems batch collection
- * @param {(ReadPreference|string)} [options.readPreference] The preferred read preference (ReadPreference.PRIMARY, ReadPreference.PRIMARY_PREFERRED, ReadPreference.SECONDARY, ReadPreference.SECONDARY_PREFERRED, ReadPreference.NEAREST).
- * @param {ClientSession} [options.session] optional session to use for this operation
- * @return {CommandCursor}
- */
- Collection.prototype.listIndexes = function(options) {
- options = options || {};
- // Clone the options
- options = Object.assign({}, options);
- // Determine the read preference in the options.
- options.readPreference = resolveReadPreference(options, { db: this.s.db, collection: this });
- // Set the CommandCursor constructor
- options.cursorFactory = CommandCursor;
- // Set the promiseLibrary
- options.promiseLibrary = this.s.promiseLibrary;
-
- if (!this.s.topology.capabilities()) {
- throw new MongoError('cannot connect to server');
- }
-
- // Cursor options
- let cursor = options.batchSize ? { batchSize: options.batchSize } : {};
-
- // We have a list collections command
- if (this.s.topology.capabilities().hasListIndexesCommand) {
- // Build the command
- const command = { listIndexes: this.s.name, cursor: cursor };
- // Execute the cursor
- cursor = this.s.topology.cursor(`${this.s.dbName}.$cmd`, command, options);
- // Do we have a readPreference, apply it
- if (options.readPreference) cursor.setReadPreference(options.readPreference);
- // Return the cursor
- return cursor;
- }
-
- // Get the namespace
- const ns = `${this.s.dbName}.system.indexes`;
- // Get the query
- cursor = this.s.topology.cursor(ns, { find: ns, query: { ns: this.s.namespace } }, options);
- // Do we have a readPreference, apply it
- if (options.readPreference) cursor.setReadPreference(options.readPreference);
- // Set the passed in batch size if one was provided
- if (options.batchSize) cursor = cursor.batchSize(options.batchSize);
- // Return the cursor
- return cursor;
- };
-
- /**
- * Ensures that an index exists, if it does not it creates it
- * @method
- * @deprecated use createIndexes instead
- * @param {(string|object)} fieldOrSpec Defines the index.
- * @param {object} [options] Optional settings.
- * @param {(number|string)} [options.w] The write concern.
- * @param {number} [options.wtimeout] The write concern timeout.
- * @param {boolean} [options.j=false] Specify a journal write concern.
- * @param {boolean} [options.unique=false] Creates an unique index.
- * @param {boolean} [options.sparse=false] Creates a sparse index.
- * @param {boolean} [options.background=false] Creates the index in the background, yielding whenever possible.
- * @param {boolean} [options.dropDups=false] A unique index cannot be created on a key that has pre-existing duplicate values. If you would like to create the index anyway, keeping the first document the database indexes and deleting all subsequent documents that have duplicate value
- * @param {number} [options.min] For geospatial indexes set the lower bound for the co-ordinates.
- * @param {number} [options.max] For geospatial indexes set the high bound for the co-ordinates.
- * @param {number} [options.v] Specify the format version of the indexes.
- * @param {number} [options.expireAfterSeconds] Allows you to expire data on indexes applied to a data (MongoDB 2.2 or higher)
- * @param {number} [options.name] Override the autogenerated index name (useful if the resulting name is larger than 128 bytes)
- * @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 {Collection~resultCallback} [callback] The command result callback
- * @return {Promise} returns Promise if no callback passed
- */
- Collection.prototype.ensureIndex = deprecate(function(fieldOrSpec, options, callback) {
- if (typeof options === 'function') (callback = options), (options = {});
- options = options || {};
-
- return executeOperation(this.s.topology, ensureIndex, [this, fieldOrSpec, options, callback]);
- }, 'collection.ensureIndex is deprecated. Use createIndexes instead.');
-
- /**
- * Checks if one or more indexes exist on the collection, fails on first non-existing index
- * @method
- * @param {(string|array)} indexes One or more index names to check.
- * @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.indexExists = function(indexes, options, callback) {
- if (typeof options === 'function') (callback = options), (options = {});
- options = options || {};
-
- return executeOperation(this.s.topology, indexExists, [this, indexes, options, callback]);
- };
-
- /**
- * Retrieves this collections index info.
- * @method
- * @param {object} [options] Optional settings.
- * @param {boolean} [options.full=false] Returns the full raw index information.
- * @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.indexInformation = function(options, callback) {
- const args = Array.prototype.slice.call(arguments, 0);
- callback = typeof args[args.length - 1] === 'function' ? args.pop() : undefined;
- options = args.length ? args.shift() || {} : {};
-
- return executeOperation(this.s.topology, indexInformation, [this, options, callback]);
- };
-
- /**
- * The callback format for results
- * @callback Collection~countCallback
- * @param {MongoError} error An error instance representing the error during the execution.
- * @param {number} result The count of documents that matched the query.
- */
-
- /**
- * Count number of matching documents in the db to a query.
- * @method
- * @param {object} [query={}] The query for the count.
- * @param {object} [options] Optional settings.
- * @param {boolean} [options.limit] The limit of documents to count.
- * @param {boolean} [options.skip] The number of documents to skip for the count.
- * @param {string} [options.hint] An index name hint for the query.
- * @param {(ReadPreference|string)} [options.readPreference] The preferred read preference (ReadPreference.PRIMARY, ReadPreference.PRIMARY_PREFERRED, ReadPreference.SECONDARY, ReadPreference.SECONDARY_PREFERRED, ReadPreference.NEAREST).
- * @param {number} [options.maxTimeMS] Number of milliseconds to wait before aborting the query.
- * @param {ClientSession} [options.session] optional session to use for this operation
- * @param {Collection~countCallback} [callback] The command result callback
- * @return {Promise} returns Promise if no callback passed
- * @deprecated use {@link Collection#countDocuments countDocuments} or {@link Collection#estimatedDocumentCount estimatedDocumentCount} instead
- */
- Collection.prototype.count = deprecate(function(query, options, callback) {
- const args = Array.prototype.slice.call(arguments, 0);
- callback = typeof args[args.length - 1] === 'function' ? args.pop() : undefined;
- query = args.length ? args.shift() || {} : {};
- options = args.length ? args.shift() || {} : {};
-
- return executeOperation(this.s.topology, count, [this, query, options, callback]);
- }, 'collection.count is deprecated, and will be removed in a future version.' +
- ' Use collection.countDocuments or collection.estimatedDocumentCount instead');
-
- /**
- * Gets an estimate of the count of documents in a collection using collection metadata.
- *
- * @method
- * @param {object} [options] Optional settings.
- * @param {number} [options.maxTimeMS] The maximum amount of time to allow the operation to run.
- * @param {Collection~countCallback} [callback] The command result callback.
- * @return {Promise} returns Promise if no callback passed.
- */
- Collection.prototype.estimatedDocumentCount = function(options, callback) {
- if (typeof options === 'function') (callback = options), (options = {});
- options = options || {};
-
- return executeOperation(this.s.topology, count, [this, null, options, callback]);
- };
-
- /**
- * Gets the number of documents matching the filter.
- *
- * **Note**: When migrating from {@link Collection#count count} to {@link Collection#countDocuments countDocuments}
- * the following query operators must be replaced:
- *
- * | Operator | Replacement |
- * | -------- | ----------- |
- * | `$where` | [`$expr`][1] |
- * | `$near` | [`$geoWithin`][2] with [`$center`][3] |
- * | `$nearSphere` | [`$geoWithin`][2] with [`$centerSphere`][4] |
- *
- * [1]: https://docs.mongodb.com/manual/reference/operator/query/expr/
- * [2]: https://docs.mongodb.com/manual/reference/operator/query/geoWithin/
- * [3]: https://docs.mongodb.com/manual/reference/operator/query/center/#op._S_center
- * [4]: https://docs.mongodb.com/manual/reference/operator/query/centerSphere/#op._S_centerSphere
- *
- * @param {object} [query] the query for the count
- * @param {object} [options] Optional settings.
- * @param {object} [options.collation] Specifies a collation.
- * @param {string|object} [options.hint] The index to use.
- * @param {number} [options.limit] The maximum number of document to count.
- * @param {number} [options.maxTimeMS] The maximum amount of time to allow the operation to run.
- * @param {number} [options.skip] The number of documents to skip before counting.
- * @param {Collection~countCallback} [callback] The command result callback.
- * @return {Promise} returns Promise if no callback passed.
- * @see https://docs.mongodb.com/manual/reference/operator/query/expr/
- * @see https://docs.mongodb.com/manual/reference/operator/query/geoWithin/
- * @see https://docs.mongodb.com/manual/reference/operator/query/center/#op._S_center
- * @see https://docs.mongodb.com/manual/reference/operator/query/centerSphere/#op._S_centerSphere
- */
-
- Collection.prototype.countDocuments = function(query, options, callback) {
- const args = Array.prototype.slice.call(arguments, 0);
- callback = typeof args[args.length - 1] === 'function' ? args.pop() : undefined;
- query = args.length ? args.shift() || {} : {};
- options = args.length ? args.shift() || {} : {};
-
- return executeOperation(this.s.topology, countDocuments, [this, query, options, callback]);
- };
-
- /**
- * The distinct command returns a list of distinct values for the given key across a collection.
- * @method
- * @param {string} key Field of the document to find distinct values for.
- * @param {object} query The query for filtering the set of documents to which we apply the distinct filter.
- * @param {object} [options] Optional settings.
- * @param {(ReadPreference|string)} [options.readPreference] The preferred read preference (ReadPreference.PRIMARY, ReadPreference.PRIMARY_PREFERRED, ReadPreference.SECONDARY, ReadPreference.SECONDARY_PREFERRED, ReadPreference.NEAREST).
- * @param {number} [options.maxTimeMS] Number of milliseconds to wait before aborting the query.
- * @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.distinct = function(key, query, options, callback) {
- const args = Array.prototype.slice.call(arguments, 1);
- callback = typeof args[args.length - 1] === 'function' ? args.pop() : undefined;
- const queryOption = args.length ? args.shift() || {} : {};
- const optionsOption = args.length ? args.shift() || {} : {};
-
- return executeOperation(this.s.topology, distinct, [
- this,
- key,
- queryOption,
- optionsOption,
- callback
- ]);
- };
-
- /**
- * Retrieve all the indexes on the collection.
- * @method
- * @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.indexes = function(options, callback) {
- if (typeof options === 'function') (callback = options), (options = {});
- options = options || {};
-
- return executeOperation(this.s.topology, indexes, [this, options, callback]);
- };
-
- /**
- * Get all the collection statistics.
- *
- * @method
- * @param {object} [options] Optional settings.
- * @param {number} [options.scale] Divide the returned sizes by scale value.
- * @param {ClientSession} [options.session] optional session to use for this operation
- * @param {Collection~resultCallback} [callback] The collection result callback
- * @return {Promise} returns Promise if no callback passed
- */
- Collection.prototype.stats = function(options, callback) {
- const args = Array.prototype.slice.call(arguments, 0);
- callback = typeof args[args.length - 1] === 'function' ? args.pop() : undefined;
- options = args.length ? args.shift() || {} : {};
-
- return executeOperation(this.s.topology, stats, [this, options, callback]);
- };
-
- /**
- * @typedef {Object} Collection~findAndModifyWriteOpResult
- * @property {object} value Document returned from findAndModify command.
- * @property {object} lastErrorObject The raw lastErrorObject returned from the command.
- * @property {Number} ok Is 1 if the command executed correctly.
- */
-
- /**
- * The callback format for inserts
- * @callback Collection~findAndModifyCallback
- * @param {MongoError} error An error instance representing the error during the execution.
- * @param {Collection~findAndModifyWriteOpResult} result The result object if the command was executed successfully.
- */
-
- /**
- * Find a document and delete it in one atomic operation. Requires a write lock for the duration of the operation.
- *
- * @method
- * @param {object} filter The Filter used to select the document to remove
- * @param {object} [options] Optional settings.
- * @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.
- * @param {number} [options.maxTimeMS] The maximum amount of time to allow the query to run.
- * @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
- */
- 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');
-
- return executeOperation(this.s.topology, findOneAndDelete, [this, filter, options, callback]);
- };
-
- /**
- * Find a document and replace it in one atomic operation. Requires a write lock for the duration of the operation.
- *
- * @method
- * @param {object} filter The Filter used to select the document to replace
- * @param {object} replacement The Document that replaces the matching document
- * @param {object} [options] Optional settings.
- * @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.
- * @param {number} [options.maxTimeMS] The maximum amount of time to allow the query to run.
- * @param {boolean} [options.upsert=false] Upsert the document if it does not exist.
- * @param {boolean} [options.returnOriginal=true] When false, returns the updated document rather than the original. The default is true.
- * @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
- */
- Collection.prototype.findOneAndReplace = function(filter, replacement, 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');
- 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.');
- }
-
- return executeOperation(this.s.topology, findOneAndReplace, [
- this,
- filter,
- replacement,
- options,
- callback
- ]);
- };
-
- /**
- * Find a document and update it in one atomic operation. Requires a write lock for the duration of the operation.
- *
- * @method
- * @param {object} filter The Filter used to select the document to update
- * @param {object} update Update operations to be performed on the document
- * @param {object} [options] Optional settings.
- * @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.
- * @param {number} [options.maxTimeMS] The maximum amount of time to allow the query to run.
- * @param {boolean} [options.upsert=false] Upsert the document if it does not exist.
- * @param {boolean} [options.returnOriginal=true] When false, returns the updated document rather than the original. The default is true.
- * @param {ClientSession} [options.session] optional session to use for this operation
- * @param {Array} [options.arrayFilters] optional list of array filters referenced in filtered positional operators
- * @param {Collection~findAndModifyCallback} [callback] The collection result callback
- * @return {Promise<Collection~findAndModifyWriteOpResultObject>} returns Promise if no callback passed
- */
- Collection.prototype.findOneAndUpdate = function(filter, update, 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');
- 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);
- }
-
- return executeOperation(this.s.topology, findOneAndUpdate, [
- this,
- filter,
- update,
- options,
- callback
- ]);
- };
-
- /**
- * Find and update a document.
- * @method
- * @param {object} query Query object to locate the object to modify.
- * @param {array} sort If multiple docs match, choose the first one in the specified sort order as the object to manipulate.
- * @param {object} doc The fields/vals to be updated.
- * @param {object} [options] Optional settings.
- * @param {(number|string)} [options.w] The write concern.
- * @param {number} [options.wtimeout] The write concern timeout.
- * @param {boolean} [options.j=false] Specify a journal write concern.
- * @param {boolean} [options.remove=false] Set to true to remove the object before returning.
- * @param {boolean} [options.upsert=false] Perform an upsert operation.
- * @param {boolean} [options.new=false] Set to true if you want to return the modified object rather than the original. Ignored for remove.
- * @param {object} [options.projection] Object containing the field projection for the result returned from the operation.
- * @param {object} [options.fields] **Deprecated** Use `options.projection` instead
- * @param {ClientSession} [options.session] optional session to use for this operation
- * @param {Array} [options.arrayFilters] optional list of array filters referenced in filtered positional operators
- * @param {Collection~findAndModifyCallback} [callback] The command result callback
- * @return {Promise} returns Promise if no callback passed
- * @deprecated use findOneAndUpdate, findOneAndReplace or findOneAndDelete instead
- */
- Collection.prototype.findAndModify = deprecate(
- _findAndModify,
- 'collection.findAndModify is deprecated. Use findOneAndUpdate, findOneAndReplace or findOneAndDelete instead.'
- );
-
- /**
- * @ignore
- */
-
- Collection.prototype._findAndModify = _findAndModify;
-
- function _findAndModify(query, sort, doc, options, callback) {
- const args = Array.prototype.slice.call(arguments, 1);
- callback = typeof args[args.length - 1] === 'function' ? args.pop() : undefined;
- sort = args.length ? args.shift() || [] : [];
- doc = args.length ? args.shift() : null;
- options = args.length ? args.shift() || {} : {};
-
- // Clone options
- options = Object.assign({}, options);
- // Force read preference primary
- options.readPreference = ReadPreference.PRIMARY;
-
- return executeOperation(this.s.topology, findAndModify, [
- this,
- query,
- sort,
- doc,
- options,
- callback
- ]);
- }
-
- /**
- * Find and remove a document.
- * @method
- * @param {object} query Query object to locate the object to modify.
- * @param {array} sort If multiple docs match, choose the first one in the specified sort order as the object to manipulate.
- * @param {object} [options] Optional settings.
- * @param {(number|string)} [options.w] The write concern.
- * @param {number} [options.wtimeout] The write concern timeout.
- * @param {boolean} [options.j=false] Specify a journal write concern.
- * @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 use findOneAndDelete instead
- */
- Collection.prototype.findAndRemove = deprecate(function(query, sort, options, callback) {
- const args = Array.prototype.slice.call(arguments, 1);
- callback = typeof args[args.length - 1] === 'function' ? args.pop() : undefined;
- sort = args.length ? args.shift() || [] : [];
- options = args.length ? args.shift() || {} : {};
-
- return executeOperation(this.s.topology, findAndRemove, [this, query, sort, options, callback]);
- }, 'collection.findAndRemove is deprecated. Use findOneAndDelete instead.');
-
- /**
- * Execute an aggregation framework pipeline against the collection, needs MongoDB >= 2.2
- * @method
- * @param {object} [pipeline=[]] Array containing all the aggregation framework commands for the execution.
- * @param {object} [options] Optional settings.
- * @param {(ReadPreference|string)} [options.readPreference] The preferred read preference (ReadPreference.PRIMARY, ReadPreference.PRIMARY_PREFERRED, ReadPreference.SECONDARY, ReadPreference.SECONDARY_PREFERRED, ReadPreference.NEAREST).
- * @param {object} [options.cursor] Return the query as cursor, on 2.6 > it returns as a real cursor on pre 2.6 it returns as an emulated cursor.
- * @param {number} [options.cursor.batchSize] The batchSize for the cursor
- * @param {boolean} [options.explain=false] Explain returns the aggregation execution plan (requires mongodb 2.6 >).
- * @param {boolean} [options.allowDiskUse=false] allowDiskUse lets the server know if it can use disk to store temporary results for the aggregation (requires mongodb 2.6 >).
- * @param {number} [options.maxTimeMS] maxTimeMS specifies a cumulative time limit in milliseconds for processing operations on the cursor. MongoDB interrupts the operation at the earliest following interrupt point.
- * @param {boolean} [options.bypassDocumentValidation=false] Allow driver to bypass schema validation in MongoDB 3.2 or higher.
- * @param {boolean} [options.raw=false] Return document results as raw BSON buffers.
- * @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 {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
- * @param {Collection~aggregationCallback} callback The command result callback
- * @return {(null|AggregationCursor)}
- */
- Collection.prototype.aggregate = function(pipeline, options, callback) {
- if (Array.isArray(pipeline)) {
- // Set up callback if one is provided
- if (typeof options === 'function') {
- callback = options;
- options = {};
- }
-
- // If we have no options or callback we are doing
- // a cursor based aggregation
- if (options == null && callback == null) {
- options = {};
- }
- } else {
- // Aggregation pipeline passed as arguments on the method
- const args = Array.prototype.slice.call(arguments, 0);
- // Get the callback
- callback = args.pop();
- // Get the possible options object
- const opts = args[args.length - 1];
- // If it contains any of the admissible options pop it of the args
- options =
- opts &&
- (opts.readPreference ||
- opts.explain ||
- opts.cursor ||
- opts.out ||
- opts.maxTimeMS ||
- opts.hint ||
- opts.allowDiskUse)
- ? args.pop()
- : {};
- // Left over arguments is the pipeline
- pipeline = args;
- }
-
- return aggregate(this.s.db, this, pipeline, options, callback);
- };
-
- /**
- * Create a new Change Stream, watching for new changes (insertions, updates, replacements, deletions, and invalidations) in this collection.
- * @method
- * @since 3.0.0
- * @param {Array} [pipeline] An array of {@link https://docs.mongodb.com/manual/reference/operator/aggregation-pipeline/|aggregation pipeline stages} through which to pass change stream documents. This allows for filtering (using $match) and manipulating the change stream documents.
- * @param {object} [options] Optional settings
- * @param {string} [options.fullDocument='default'] Allowed values: ‘default’, ‘updateLookup’. When set to ‘updateLookup’, the change stream will include both a delta describing the changes to the document, as well as a copy of the entire document that was changed from some time after the change occurred.
- * @param {object} [options.resumeAfter] Specifies the logical starting point for the new change stream. This should be the _id field from a previously returned change stream document.
- * @param {number} [options.maxAwaitTimeMS] The maximum amount of time for the server to wait on new documents to satisfy a change stream query
- * @param {number} [options.batchSize] The number of documents to return per batch. See {@link https://docs.mongodb.com/manual/reference/command/aggregate|aggregation documentation}.
- * @param {object} [options.collation] Specify collation settings for operation. See {@link https://docs.mongodb.com/manual/reference/command/aggregate|aggregation documentation}.
- * @param {ReadPreference} [options.readPreference] The read preference. Defaults to the read preference of the database or collection. See {@link https://docs.mongodb.com/manual/reference/read-preference|read preference documentation}.
- * @param {Timestamp} [options.startAtClusterTime] receive change events that occur after the specified timestamp
- * @param {ClientSession} [options.session] optional session to use for this operation
- * @return {ChangeStream} a ChangeStream instance.
- */
- Collection.prototype.watch = function(pipeline, options) {
- pipeline = pipeline || [];
- options = options || {};
-
- // Allow optionally not specifying a pipeline
- if (!Array.isArray(pipeline)) {
- options = pipeline;
- pipeline = [];
- }
-
- return new ChangeStream(this, pipeline, options);
- };
-
- /**
- * The callback format for results
- * @callback Collection~parallelCollectionScanCallback
- * @param {MongoError} error An error instance representing the error during the execution.
- * @param {Cursor[]} cursors A list of cursors returned allowing for parallel reading of collection.
- */
-
- /**
- * Return N number of parallel cursors for a collection allowing parallel reading of entire collection. There are
- * no ordering guarantees for returned results.
- * @method
- * @param {object} [options] Optional settings.
- * @param {(ReadPreference|string)} [options.readPreference] The preferred read preference (ReadPreference.PRIMARY, ReadPreference.PRIMARY_PREFERRED, ReadPreference.SECONDARY, ReadPreference.SECONDARY_PREFERRED, ReadPreference.NEAREST).
- * @param {number} [options.batchSize] Set the batchSize for the getMoreCommand when iterating over the query results.
- * @param {number} [options.numCursors=1] The maximum number of parallel command cursors to return (the number of returned cursors will be in the range 1:numCursors)
- * @param {boolean} [options.raw=false] Return all BSON documents as Raw Buffer documents.
- * @param {Collection~parallelCollectionScanCallback} [callback] The command result callback
- * @return {Promise} returns Promise if no callback passed
- */
- Collection.prototype.parallelCollectionScan = deprecate(function(options, callback) {
- if (typeof options === 'function') (callback = options), (options = { numCursors: 1 });
- // Set number of cursors to 1
- options.numCursors = options.numCursors || 1;
- options.batchSize = options.batchSize || 1000;
-
- options = Object.assign({}, options);
- // Ensure we have the right read preference inheritance
- options.readPreference = resolveReadPreference(options, { db: this.s.db, collection: this });
-
- // Add a promiseLibrary
- options.promiseLibrary = this.s.promiseLibrary;
-
- if (options.session) {
- options.session = undefined;
- }
-
- return executeOperation(this.s.topology, parallelCollectionScan, [this, options, callback], {
- skipSessions: true
- });
- }, 'parallelCollectionScan is deprecated in MongoDB v4.1');
-
- /**
- * Execute a geo search using a geo haystack index on a collection.
- *
- * @method
- * @param {number} x Point to search on the x axis, ensure the indexes are ordered in the same order.
- * @param {number} y Point to search on the y axis, ensure the indexes are ordered in the same order.
- * @param {object} [options] Optional settings.
- * @param {(ReadPreference|string)} [options.readPreference] The preferred read preference (ReadPreference.PRIMARY, ReadPreference.PRIMARY_PREFERRED, ReadPreference.SECONDARY, ReadPreference.SECONDARY_PREFERRED, ReadPreference.NEAREST).
- * @param {number} [options.maxDistance] Include results up to maxDistance from the point.
- * @param {object} [options.search] Filter the results by a query.
- * @param {number} [options.limit=false] Max number of results to return.
- * @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.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() || {} : {};
-
- return executeOperation(this.s.topology, geoHaystackSearch, [this, x, y, options, callback]);
- };
-
- /**
- * Run a group command across a collection
- *
- * @method
- * @param {(object|array|function|code)} keys An object, array or function expressing the keys to group by.
- * @param {object} condition An optional condition that must be true for a row to be considered.
- * @param {object} initial Initial value of the aggregation counter object.
- * @param {(function|Code)} reduce The reduce function aggregates (reduces) the objects iterated
- * @param {(function|Code)} finalize An optional function to be run on each item in the result set just before the item is returned.
- * @param {boolean} command Specify if you wish to run using the internal group command or using eval, default is true.
- * @param {object} [options] Optional settings.
- * @param {(ReadPreference|string)} [options.readPreference] The preferred read preference (ReadPreference.PRIMARY, ReadPreference.PRIMARY_PREFERRED, ReadPreference.SECONDARY, ReadPreference.SECONDARY_PREFERRED, ReadPreference.NEAREST).
- * @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 MongoDB 3.6 or higher no longer supports the group command. We recommend rewriting using the aggregation framework.
- */
- Collection.prototype.group = deprecate(function(
- keys,
- condition,
- initial,
- reduce,
- finalize,
- command,
- options,
- callback
- ) {
- const args = Array.prototype.slice.call(arguments, 3);
- callback = typeof args[args.length - 1] === 'function' ? args.pop() : undefined;
- reduce = args.length ? args.shift() : null;
- finalize = args.length ? args.shift() : null;
- command = args.length ? args.shift() : null;
- options = args.length ? args.shift() || {} : {};
-
- // Make sure we are backward compatible
- if (!(typeof finalize === 'function')) {
- command = finalize;
- finalize = null;
- }
-
- if (
- !Array.isArray(keys) &&
- keys instanceof Object &&
- typeof keys !== 'function' &&
- !(keys._bsontype === 'Code')
- ) {
- keys = Object.keys(keys);
- }
-
- if (typeof reduce === 'function') {
- reduce = reduce.toString();
- }
-
- if (typeof finalize === 'function') {
- finalize = finalize.toString();
- }
-
- // Set up the command as default
- command = command == null ? true : command;
-
- return executeOperation(this.s.topology, group, [
- this,
- keys,
- condition,
- initial,
- reduce,
- finalize,
- command,
- options,
- callback
- ]);
- },
- 'MongoDB 3.6 or higher no longer supports the group command. We recommend rewriting using the aggregation framework.');
-
- /**
- * Run Map Reduce across a collection. Be aware that the inline option for out will return an array of results not a collection.
- *
- * @method
- * @param {(function|string)} map The mapping function.
- * @param {(function|string)} reduce The reduce function.
- * @param {object} [options] Optional settings.
- * @param {(ReadPreference|string)} [options.readPreference] The preferred read preference (ReadPreference.PRIMARY, ReadPreference.PRIMARY_PREFERRED, ReadPreference.SECONDARY, ReadPreference.SECONDARY_PREFERRED, ReadPreference.NEAREST).
- * @param {object} [options.out] Sets the output target for the map reduce job. *{inline:1} | {replace:'collectionName'} | {merge:'collectionName'} | {reduce:'collectionName'}*
- * @param {object} [options.query] Query filter object.
- * @param {object} [options.sort] Sorts the input objects using this key. Useful for optimization, like sorting by the emit key for fewer reduces.
- * @param {number} [options.limit] Number of objects to return from collection.
- * @param {boolean} [options.keeptemp=false] Keep temporary data.
- * @param {(function|string)} [options.finalize] Finalize function.
- * @param {object} [options.scope] Can pass in variables that can be access from map/reduce/finalize.
- * @param {boolean} [options.jsMode=false] It is possible to make the execution stay in JS. Provided in MongoDB > 2.0.X.
- * @param {boolean} [options.verbose=false] Provide statistics on job execution time.
- * @param {boolean} [options.bypassDocumentValidation=false] Allow driver to bypass schema validation in MongoDB 3.2 or higher.
- * @param {ClientSession} [options.session] optional session to use for this operation
- * @param {Collection~resultCallback} [callback] The command result callback
- * @throws {MongoError}
- * @return {Promise} returns Promise if no callback passed
- */
- Collection.prototype.mapReduce = function(map, reduce, options, callback) {
- if ('function' === typeof options) (callback = options), (options = {});
- // Out must allways be defined (make sure we don't break weirdly on pre 1.8+ servers)
- if (null == options.out) {
- throw new Error(
- 'the out option parameter must be defined, see mongodb docs for possible values'
- );
- }
-
- if ('function' === typeof map) {
- map = map.toString();
- }
-
- if ('function' === typeof reduce) {
- reduce = reduce.toString();
- }
-
- if ('function' === typeof options.finalize) {
- options.finalize = options.finalize.toString();
- }
-
- return executeOperation(this.s.topology, mapReduce, [this, map, reduce, options, callback]);
- };
-
- /**
- * Initiate an Out of order batch write operation. All operations will be buffered into insert/update/remove commands executed out of order.
- *
- * @method
- * @param {object} [options] Optional settings.
- * @param {(number|string)} [options.w] The write concern.
- * @param {number} [options.wtimeout] The write concern timeout.
- * @param {boolean} [options.j=false] Specify a journal write concern.
- * @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
- * @return {UnorderedBulkOperation}
- */
- Collection.prototype.initializeUnorderedBulkOp = function(options) {
- options = options || {};
- // Give function's options precedence over session options.
- if (options.ignoreUndefined == null) {
- options.ignoreUndefined = this.s.options.ignoreUndefined;
- }
-
- options.promiseLibrary = this.s.promiseLibrary;
- return unordered(this.s.topology, this, options);
- };
-
- /**
- * Initiate an In order bulk write operation. Operations will be serially executed in the order they are added, creating a new operation for each switch in types.
- *
- * @method
- * @param {object} [options] Optional settings.
- * @param {(number|string)} [options.w] The write concern.
- * @param {number} [options.wtimeout] The write concern timeout.
- * @param {boolean} [options.j=false] Specify a journal write concern.
- * @param {ClientSession} [options.session] optional session to use for this operation
- * @param {boolean} [options.ignoreUndefined=false] Specify if the BSON serializer should ignore undefined fields.
- * @param {OrderedBulkOperation} callback The command result callback
- * @return {null}
- */
- Collection.prototype.initializeOrderedBulkOp = function(options) {
- options = options || {};
- // Give function's options precedence over session's options.
- if (options.ignoreUndefined == null) {
- options.ignoreUndefined = this.s.options.ignoreUndefined;
- }
- options.promiseLibrary = this.s.promiseLibrary;
- return ordered(this.s.topology, this, options);
- };
-
- /**
- * Return the db logger
- * @method
- * @return {Logger} return the db logger
- * @ignore
- */
- Collection.prototype.getLogger = function() {
- return this.s.db.s.logger;
- };
-
- module.exports = Collection;
|