/node_modules/mongoose/node_modules/mongodb/lib/collection.js
https://bitbucket.org/coleman333/smartsite · JavaScript · 3143 lines · 1548 code · 426 blank · 1169 comment · 525 complexity · cd07cd466b16d64808c290bfbd7f811e MD5 · raw file
Large files are truncated click here to view the full file
- 'use strict';
- var checkCollectionName = require('./utils').checkCollectionName,
- ObjectID = require('mongodb-core').BSON.ObjectID,
- Long = require('mongodb-core').BSON.Long,
- Code = require('mongodb-core').BSON.Code,
- f = require('util').format,
- AggregationCursor = require('./aggregation_cursor'),
- MongoError = require('mongodb-core').MongoError,
- shallowClone = require('./utils').shallowClone,
- isObject = require('./utils').isObject,
- toError = require('./utils').toError,
- normalizeHintField = require('./utils').normalizeHintField,
- handleCallback = require('./utils').handleCallback,
- decorateCommand = require('./utils').decorateCommand,
- formattedOrderClause = require('./utils').formattedOrderClause,
- ReadPreference = require('mongodb-core').ReadPreference,
- CommandCursor = require('./command_cursor'),
- Define = require('./metadata'),
- Cursor = require('./cursor'),
- unordered = require('./bulk/unordered'),
- ordered = require('./bulk/ordered'),
- ChangeStream = require('./change_stream'),
- executeOperation = require('./utils').executeOperation;
- /**
- * @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();
- * });
- * });
- */
- var mergeKeys = ['readPreference', '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.
- */
- var Collection = function(db, topology, dbName, name, pkFactory, options) {
- checkCollectionName(name);
- // Unpack variables
- var internalHint = null;
- var slaveOk = options == null || options.slaveOk == null ? db.slaveOk : options.slaveOk;
- var serializeFunctions =
- options == null || options.serializeFunctions == null
- ? db.s.options.serializeFunctions
- : options.serializeFunctions;
- var raw = options == null || options.raw == null ? db.s.options.raw : options.raw;
- var promoteLongs =
- options == null || options.promoteLongs == null
- ? db.s.options.promoteLongs
- : options.promoteLongs;
- var promoteValues =
- options == null || options.promoteValues == null
- ? db.s.options.promoteValues
- : options.promoteValues;
- var promoteBuffers =
- options == null || options.promoteBuffers == null
- ? db.s.options.promoteBuffers
- : options.promoteBuffers;
- var readPreference = null;
- var collectionHint = null;
- var namespace = f('%s.%s', dbName, name);
- // Get the promiseLibrary
- var 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
- };
- };
- var define = (Collection.define = new Define('Collection', Collection, false));
- 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() {
- var ops = {};
- 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);
- }
- });
- /**
- * 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=null] Optional settings.
- * @param {number} [options.limit=0] Sets the limit of documents returned in the query.
- * @param {(array|object)} [options.sort=null] Set to sort the documents coming back from the query. Array of indexes, [['a', 1]] etc.
- * @param {object} [options.projection=null] The fields to return in the query. Object of fields to include or exclude (not both), {'a':1}
- * @param {object} [options.fields=null] **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=null] 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] 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=null] Limit the number of items to scan.
- * @param {number} [options.min=null] Set index bounds.
- * @param {number} [options.max=null] Set index bounds.
- * @param {boolean} [options.showDiskLoc=false] Show disk location of results.
- * @param {string} [options.comment=null] 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=null] 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=null] Number of miliseconds to wait before aborting the query.
- * @param {object} [options.collation=null] 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 = function(query, options, callback) {
- 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
- var object = selector;
- if (Buffer.isBuffer(object)) {
- var object_size = object[0] | (object[1] << 8) | (object[2] << 16) | (object[3] << 24);
- if (object_size !== object.length) {
- var 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 };
- }
- var newOptions = {};
- // Make a shallow copy of the collection options
- for (var key in this.s.options) {
- if (mergeKeys.indexOf(key) !== -1) {
- newOptions[key] = this.s.options[key];
- }
- }
- // Make a shallow copy of options
- for (var optKey in options) {
- newOptions[optKey] = options[optKey];
- }
- // 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 = getReadPreference(this, newOptions, this.s.db);
- // 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
- var 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;
- // Merge in options to command
- for (var name in newOptions) {
- if (newOptions[name] != null && name !== 'session') {
- findCommand[name] = newOptions[name];
- }
- }
- 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
- decorateWithCollation(findCommand, this, options);
- // Create the cursor
- if (typeof callback === 'function')
- return handleCallback(
- callback,
- null,
- this.s.topology.cursor(this.s.namespace, findCommand, newOptions)
- );
- return this.s.topology.cursor(this.s.namespace, findCommand, newOptions);
- };
- define.classMethod('find', { callback: false, promise: false, returns: [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=null] Optional settings.
- * @param {(number|string)} [options.w=null] The write concern.
- * @param {number} [options.wtimeout=null] 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 ignoreUndfined
- if (this.s.options.ignoreUndefined) {
- options = shallowClone(options);
- options.ignoreUndefined = this.s.options.ignoreUndefined;
- }
- return executeOperation(this.s.topology, insertOne, [this, doc, options, callback]);
- };
- var insertOne = function(self, doc, options, callback) {
- if (Array.isArray(doc)) {
- return callback(
- MongoError.create({ message: 'doc parameter must be an object', driver: true })
- );
- }
- insertDocuments(self, [doc], options, function(err, r) {
- if (callback == null) return;
- if (err && callback) return callback(err);
- // Workaround for pre 2.6 servers
- if (r == null) return callback(null, { result: { ok: 1 } });
- // Add values to top level to ensure crud spec compatibility
- r.insertedCount = r.result.n;
- r.insertedId = doc._id;
- if (callback) callback(null, r);
- });
- };
- var mapInserManyResults = function(docs, r) {
- var finalResult = {
- result: { ok: 1, n: r.insertedCount },
- ops: docs,
- insertedCount: r.insertedCount,
- insertedIds: r.insertedIds
- };
- if (r.getLastOp()) {
- finalResult.result.opTime = r.getLastOp();
- }
- return finalResult;
- };
- define.classMethod('insertOne', { callback: true, promise: true });
- /**
- * 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=null] Optional settings.
- * @param {(number|string)} [options.w=null] The write concern.
- * @param {number} [options.wtimeout=null] 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) {
- var self = this;
- if (typeof options === 'function') (callback = options), (options = {});
- options = options ? shallowClone(options) : { ordered: true };
- if (!Array.isArray(docs) && typeof callback === 'function') {
- return callback(
- MongoError.create({ message: 'docs parameter must be an array of documents', driver: true })
- );
- } else if (!Array.isArray(docs)) {
- return new this.s.promiseLibrary(function(resolve, reject) {
- reject(
- MongoError.create({ message: 'docs parameter must be an array of documents', driver: true })
- );
- });
- }
- // If keep going set unordered
- options['serializeFunctions'] = options['serializeFunctions'] || self.s.serializeFunctions;
- // Set up the force server object id
- var forceServerObjectId =
- typeof options.forceServerObjectId === 'boolean'
- ? options.forceServerObjectId
- : self.s.db.options.forceServerObjectId;
- // Do we want to force the server to assign the _id key
- if (forceServerObjectId !== true) {
- // Add _id if not specified
- for (var i = 0; i < docs.length; i++) {
- if (docs[i]._id == null) docs[i]._id = self.s.pkFactory.createPk();
- }
- }
- // Generate the bulk write operations
- var operations = [
- {
- insertMany: docs
- }
- ];
- return executeOperation(this.s.topology, bulkWrite, [this, operations, options, callback], {
- resultMutator: result => mapInserManyResults(docs, result)
- });
- };
- define.classMethod('insertMany', { callback: true, promise: true });
- /**
- * @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=null] Optional settings.
- * @param {(number|string)} [options.w=null] The write concern.
- * @param {number} [options.wtimeout=null] 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]);
- };
- var bulkWrite = function(self, operations, options, callback) {
- // Add ignoreUndfined
- if (self.s.options.ignoreUndefined) {
- options = shallowClone(options);
- options.ignoreUndefined = self.s.options.ignoreUndefined;
- }
- // Create the bulk operation
- var bulk =
- options.ordered === true || options.ordered == null
- ? self.initializeOrderedBulkOp(options)
- : self.initializeUnorderedBulkOp(options);
- // Do we have a collation
- var collation = false;
- // for each op go through and add to the bulk
- try {
- for (var i = 0; i < operations.length; i++) {
- // Get the operation type
- var key = Object.keys(operations[i])[0];
- // Check if we have a collation
- if (operations[i][key].collation) {
- collation = true;
- }
- // Pass to the raw bulk
- bulk.raw(operations[i]);
- }
- } catch (err) {
- return callback(err, null);
- }
- // Final options for write concern
- var finalOptions = writeConcern(shallowClone(options), self.s.db, self, options);
- var writeCon = finalOptions.writeConcern ? finalOptions.writeConcern : {};
- var capabilities = self.s.topology.capabilities();
- // Did the user pass in a collation, check if our write server supports it
- if (collation && capabilities && !capabilities.commandsTakeCollation) {
- return callback(new MongoError(f('server/primary/mongos does not support collation')));
- }
- // Execute the bulk
- bulk.execute(writeCon, finalOptions, function(err, r) {
- // We have connection level error
- if (!r && err) {
- return callback(err, null);
- }
- r.insertedCount = r.nInserted;
- r.matchedCount = r.nMatched;
- r.modifiedCount = r.nModified || 0;
- r.deletedCount = r.nRemoved;
- r.upsertedCount = r.getUpsertedIds().length;
- r.upsertedIds = {};
- r.insertedIds = {};
- // Update the n
- r.n = r.insertedCount;
- // Inserted documents
- var inserted = r.getInsertedIds();
- // Map inserted ids
- for (var i = 0; i < inserted.length; i++) {
- r.insertedIds[inserted[i].index] = inserted[i]._id;
- }
- // Upserted documents
- var upserted = r.getUpsertedIds();
- // Map upserted ids
- for (i = 0; i < upserted.length; i++) {
- r.upsertedIds[upserted[i].index] = upserted[i]._id;
- }
- // Return the results
- callback(null, r);
- });
- };
- var insertDocuments = function(self, docs, options, callback) {
- if (typeof options === 'function') (callback = options), (options = {});
- options = options || {};
- // Ensure we are operating on an array op docs
- docs = Array.isArray(docs) ? docs : [docs];
- // Get the write concern options
- var finalOptions = writeConcern(shallowClone(options), self.s.db, self, options);
- // If keep going set unordered
- if (finalOptions.keepGoing === true) finalOptions.ordered = false;
- finalOptions['serializeFunctions'] = options['serializeFunctions'] || self.s.serializeFunctions;
- // Set up the force server object id
- var forceServerObjectId =
- typeof options.forceServerObjectId === 'boolean'
- ? options.forceServerObjectId
- : self.s.db.options.forceServerObjectId;
- // Add _id if not specified
- if (forceServerObjectId !== true) {
- for (var i = 0; i < docs.length; i++) {
- if (docs[i]._id === void 0) docs[i]._id = self.s.pkFactory.createPk();
- }
- }
- // File inserts
- self.s.topology.insert(self.s.namespace, docs, finalOptions, function(err, result) {
- if (callback == null) return;
- if (err) return handleCallback(callback, err);
- if (result == null) return handleCallback(callback, null, null);
- if (result.result.code) return handleCallback(callback, toError(result.result));
- if (result.result.writeErrors)
- return handleCallback(callback, toError(result.result.writeErrors[0]));
- // Add docs to the list
- result.ops = docs;
- // Return the results
- handleCallback(callback, null, result);
- });
- };
- define.classMethod('bulkWrite', { callback: true, promise: true });
- /**
- * @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=null] Optional settings.
- * @param {(number|string)} [options.w=null] The write concern.
- * @param {number} [options.wtimeout=null] 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 = 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);
- };
- define.classMethod('insert', { callback: true, promise: true });
- /**
- * @typedef {Object} Collection~updateWriteOpResult
- * @property {Object} result The raw result returned from MongoDB, field 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.
- */
- /**
- * 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 on MongoDB
- * @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=null] Optional settings.
- * @param {boolean} [options.upsert=false] Update operation is an upsert.
- * @param {(number|string)} [options.w=null] The write concern.
- * @param {number} [options.wtimeout=null] 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=null] 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 || {};
- var err = checkForAtomicOperators(update);
- if (err) {
- if (typeof callback === 'function') return callback(err);
- return this.s.promiseLibrary.reject(err);
- }
- options = shallowClone(options);
- // Add ignoreUndfined
- if (this.s.options.ignoreUndefined) {
- options = shallowClone(options);
- options.ignoreUndefined = this.s.options.ignoreUndefined;
- }
- return executeOperation(this.s.topology, updateOne, [this, filter, update, options, callback]);
- };
- var checkForAtomicOperators = function(update) {
- var keys = Object.keys(update);
- // same errors as the server would give for update doc lacking atomic operators
- if (keys.length === 0) {
- return toError('The update operation document must contain at least one atomic operator.');
- }
- if (keys[0][0] !== '$') {
- return toError('the update operation document must contain atomic operators.');
- }
- };
- var updateOne = function(self, filter, update, options, callback) {
- // Set single document update
- options.multi = false;
- // Execute update
- updateDocuments(self, filter, update, options, function(err, r) {
- if (callback == null) return;
- if (err && callback) return callback(err);
- if (r == null) return callback(null, { result: { ok: 1 } });
- r.modifiedCount = r.result.nModified != null ? r.result.nModified : r.result.n;
- r.upsertedId =
- Array.isArray(r.result.upserted) && r.result.upserted.length > 0
- ? r.result.upserted[0]
- : null;
- r.upsertedCount =
- Array.isArray(r.result.upserted) && r.result.upserted.length ? r.result.upserted.length : 0;
- r.matchedCount =
- Array.isArray(r.result.upserted) && r.result.upserted.length > 0 ? 0 : r.result.n;
- if (callback) callback(null, r);
- });
- };
- define.classMethod('updateOne', { callback: true, promise: true });
- /**
- * Replace a document on MongoDB
- * @method
- * @param {object} filter The Filter used to select the document to update
- * @param {object} doc The Document that replaces the matching document
- * @param {object} [options=null] Optional settings.
- * @param {boolean} [options.upsert=false] Update operation is an upsert.
- * @param {(number|string)} [options.w=null] The write concern.
- * @param {number} [options.wtimeout=null] 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} returns Promise if no callback passed
- */
- Collection.prototype.replaceOne = function(filter, doc, options, callback) {
- if (typeof options === 'function') (callback = options), (options = {});
- options = shallowClone(options);
- // Add ignoreUndfined
- if (this.s.options.ignoreUndefined) {
- options = shallowClone(options);
- options.ignoreUndefined = this.s.options.ignoreUndefined;
- }
- return executeOperation(this.s.topology, replaceOne, [this, filter, doc, options, callback]);
- };
- var replaceOne = function(self, filter, doc, options, callback) {
- // Set single document update
- options.multi = false;
- // Execute update
- updateDocuments(self, filter, doc, options, function(err, r) {
- if (callback == null) return;
- if (err && callback) return callback(err);
- if (r == null) return callback(null, { result: { ok: 1 } });
- r.modifiedCount = r.result.nModified != null ? r.result.nModified : r.result.n;
- r.upsertedId =
- Array.isArray(r.result.upserted) && r.result.upserted.length > 0
- ? r.result.upserted[0]
- : null;
- r.upsertedCount =
- Array.isArray(r.result.upserted) && r.result.upserted.length ? r.result.upserted.length : 0;
- r.matchedCount =
- Array.isArray(r.result.upserted) && r.result.upserted.length > 0 ? 0 : r.result.n;
- r.ops = [doc];
- if (callback) callback(null, r);
- });
- };
- define.classMethod('replaceOne', { callback: true, promise: true });
- /**
- * Update multiple documents on MongoDB
- * @method
- * @param {object} filter The Filter used to select the documents to update
- * @param {object} update The update operations to be applied to the document
- * @param {object} [options=null] Optional settings.
- * @param {boolean} [options.upsert=false] Update operation is an upsert.
- * @param {(number|string)} [options.w=null] The write concern.
- * @param {number} [options.wtimeout=null] The write concern timeout.
- * @param {boolean} [options.j=false] Specify a journal write concern.
- * @param {Array} [options.arrayFilters=null] 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.updateMany = function(filter, update, options, callback) {
- if (typeof options === 'function') (callback = options), (options = {});
- options = options || {};
- var err = checkForAtomicOperators(update);
- if (err) {
- if (typeof callback === 'function') return callback(err);
- return this.s.promiseLibrary.reject(err);
- }
- options = shallowClone(options);
- // Add ignoreUndfined
- if (this.s.options.ignoreUndefined) {
- options = shallowClone(options);
- options.ignoreUndefined = this.s.options.ignoreUndefined;
- }
- return executeOperation(this.s.topology, updateMany, [this, filter, update, options, callback]);
- };
- var updateMany = function(self, filter, update, options, callback) {
- // Set single document update
- options.multi = true;
- // Execute update
- updateDocuments(self, filter, update, options, function(err, r) {
- if (callback == null) return;
- if (err && callback) return callback(err);
- if (r == null) return callback(null, { result: { ok: 1 } });
- r.modifiedCount = r.result.nModified != null ? r.result.nModified : r.result.n;
- r.upsertedId =
- Array.isArray(r.result.upserted) && r.result.upserted.length > 0
- ? r.result.upserted[0]
- : null;
- r.upsertedCount =
- Array.isArray(r.result.upserted) && r.result.upserted.length ? r.result.upserted.length : 0;
- r.matchedCount =
- Array.isArray(r.result.upserted) && r.result.upserted.length > 0 ? 0 : r.result.n;
- if (callback) callback(null, r);
- });
- };
- define.classMethod('updateMany', { callback: true, promise: true });
- var updateDocuments = function(self, selector, document, options, callback) {
- if ('function' === typeof options) (callback = options), (options = null);
- if (options == null) options = {};
- if (!('function' === typeof callback)) callback = null;
- // If we are not providing a selector or document throw
- if (selector == null || typeof selector !== 'object')
- return callback(toError('selector must be a valid JavaScript object'));
- if (document == null || typeof document !== 'object')
- return callback(toError('document must be a valid JavaScript object'));
- // Get the write concern options
- var finalOptions = writeConcern(shallowClone(options), self.s.db, self, options);
- // Do we return the actual result document
- // Either use override on the function, or go back to default on either the collection
- // level or db
- finalOptions['serializeFunctions'] = options['serializeFunctions'] || self.s.serializeFunctions;
- // Execute the operation
- var op = { q: selector, u: document };
- op.upsert = options.upsert !== void 0 ? !!options.upsert : false;
- op.multi = options.multi !== void 0 ? !!options.multi : false;
- if (finalOptions.arrayFilters) {
- op.arrayFilters = finalOptions.arrayFilters;
- delete finalOptions.arrayFilters;
- }
- // Have we specified collation
- decorateWithCollation(finalOptions, self, options);
- // Update options
- self.s.topology.update(self.s.namespace, [op], finalOptions, function(err, result) {
- if (callback == null) return;
- if (err) return handleCallback(callback, err, null);
- if (result == null) return handleCallback(callback, null, null);
- if (result.result.code) return handleCallback(callback, toError(result.result));
- if (result.result.writeErrors)
- return handleCallback(callback, toError(result.result.writeErrors[0]));
- // Return the results
- handleCallback(callback, null, result);
- });
- };
- /**
- * Updates documents.
- * @method
- * @param {object} selector The selector for the update operation.
- * @param {object} document The update document.
- * @param {object} [options=null] Optional settings.
- * @param {(number|string)} [options.w=null] The write concern.
- * @param {number} [options.wtimeout=null] 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=null] Specify collation (MongoDB 3.4 or higher) settings for update operation (see 3.4 documentation for available fields).
- * @param {Array} [options.arrayFilters=null] 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 = function(selector, document, options, callback) {
- if (typeof options === 'function') (callback = options), (options = {});
- options = options || {};
- // Add ignoreUndfined
- if (this.s.options.ignoreUndefined) {
- options = shallowClone(options);
- options.ignoreUndefined = this.s.options.ignoreUndefined;
- }
- return executeOperation(this.s.topology, updateDocuments, [
- this,
- selector,
- document,
- options,
- callback
- ]);
- };
- define.classMethod('update', { callback: true, promise: true });
- /**
- * @typedef {Object} Collection~deleteWriteOpResult
- * @property {Object} result The raw result returned from MongoDB, field 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 on MongoDB
- * @method
- * @param {object} filter The Filter used to select the document to remove
- * @param {object} [options=null] Optional settings.
- * @param {(number|string)} [options.w=null] The write concern.
- * @param {number} [options.wtimeout=null] 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 = shallowClone(options);
- // Add ignoreUndfined
- if (this.s.options.ignoreUndefined) {
- options = shallowClone(options);
- options.ignoreUndefined = this.s.options.ignoreUndefined;
- }
- return executeOperation(this.s.topology, deleteOne, [this, filter, options, callback]);
- };
- var deleteOne = function(self, filter, options, callback) {
- options.single = true;
- removeDocuments(self, filter, options, function(err, r) {
- if (callback == null) return;
- if (err && callback) return callback(err);
- if (r == null) return callback(null, { result: { ok: 1 } });
- r.deletedCount = r.result.n;
- if (callback) callback(null, r);
- });
- };
- define.classMethod('deleteOne', { callback: true, promise: true });
- Collection.prototype.removeOne = Collection.prototype.deleteOne;
- define.classMethod('removeOne', { callback: true, promise: true });
- /**
- * Delete multiple documents on MongoDB
- * @method
- * @param {object} filter The Filter used to select the documents to remove
- * @param {object} [options=null] Optional settings.
- * @param {(number|string)} [options.w=null] The write concern.
- * @param {number} [options.wtimeout=null] 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 = shallowClone(options);
- // Add ignoreUndfined
- if (this.s.options.ignoreUndefined) {
- options = shallowClone(options);
- options.ignoreUndefined = this.s.options.ignoreUndefined;
- }
- return executeOperation(this.s.topology, deleteMany, [this, filter, options, callback]);
- };
- var deleteMany = function(self, filter, options, callback) {
- options.single = false;
- removeDocuments(self, filter, options, function(err, r) {
- if (callback == null) return;
- if (err && callback) return callback(err);
- if (r == null) return callback(null, { result: { ok: 1 } });
- r.deletedCount = r.result.n;
- if (callback) callback(null, r);
- });
- };
- var removeDocuments = function(self, selector, options, callback) {
- if (typeof options === 'function') {
- (callback = options), (options = {});
- } else if (typeof selector === 'function') {
- callback = selector;
- options = {};
- selector = {};
- }
- // Create an empty options object if the provided one is null
- options = options || {};
- // Get the write concern options
- var finalOptions = writeConcern(shallowClone(options), self.s.db, self, options);
- // If selector is null set empty
- if (selector == null) selector = {};
- // Build the op
- var op = { q: selector, limit: 0 };
- if (options.single) op.limit = 1;
- // Have we specified collation
- decorateWithCollation(finalOptions, self, options);
- // Execute the remove
- self.s.topology.remove(self.s.namespace, [op], finalOptions, function(err, result) {
- if (callback == null) return;
- if (err) return handleCallback(callback, err, null);
- if (result == null) return handleCallback(callback, null, null);
- if (result.result.code) return handleCallback(callback, toError(result.result));
- if (result.result.writeErrors)
- return handleCallback(callback, toError(result.result.writeErrors[0]));
- // Return the results
- handleCallback(callback, null, result);
- });
- };
- define.classMethod('deleteMany', { callback: true, promise: true });
- Collection.prototype.removeMany = Collection.prototype.deleteMany;
- define.classMethod('removeMany', { callback: true, promise: true });
- /**
- * Remove documents.
- * @method
- * @param {object} selector The selector for the update operation.
- * @param {object} [options=null] Optional settings.
- * @param {(number|string)} [options.w=null] The write concern.
- * @param {number} [options.wtimeout=null] 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 = function(selector, options, callback) {
- if (typeof options === 'function') (callback = options), (options = {});…