Skip to content

Commit 2cc73e1

Browse files
authored
refactor(collection): do not use resultMutator for insertMany
Fixes NODE-1895
1 parent 39bf692 commit 2cc73e1

File tree

3 files changed

+54
-47
lines changed

3 files changed

+54
-47
lines changed

lib/collection.js

Lines changed: 2 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,12 @@ const group = require('./operations/collection_ops').group;
4545
const indexes = require('./operations/collection_ops').indexes;
4646
const indexExists = require('./operations/collection_ops').indexExists;
4747
const indexInformation = require('./operations/collection_ops').indexInformation;
48+
const insertMany = require('./operations/collection_ops').insertMany;
4849
const insertOne = require('./operations/collection_ops').insertOne;
4950
const isCapped = require('./operations/collection_ops').isCapped;
5051
const mapReduce = require('./operations/collection_ops').mapReduce;
5152
const optionsOp = require('./operations/collection_ops').optionsOp;
5253
const parallelCollectionScan = require('./operations/collection_ops').parallelCollectionScan;
53-
const prepareDocs = require('./operations/collection_ops').prepareDocs;
5454
const reIndex = require('./operations/collection_ops').reIndex;
5555
const removeDocuments = require('./operations/collection_ops').removeDocuments;
5656
const rename = require('./operations/collection_ops').rename;
@@ -463,21 +463,6 @@ Collection.prototype.insertOne = function(doc, options, callback) {
463463
return executeOperation(this.s.topology, insertOne, [this, doc, options, callback]);
464464
};
465465

466-
function mapInsertManyResults(docs, r) {
467-
const finalResult = {
468-
result: { ok: 1, n: r.insertedCount },
469-
ops: docs,
470-
insertedCount: r.insertedCount,
471-
insertedIds: r.insertedIds
472-
};
473-
474-
if (r.getLastOp()) {
475-
finalResult.result.opTime = r.getLastOp();
476-
}
477-
478-
return finalResult;
479-
}
480-
481466
/**
482467
* Inserts an array of documents into MongoDB. If documents passed in do not contain the **_id** field,
483468
* one will be added to each of the documents missing it by the driver, mutating the document. This behavior
@@ -501,33 +486,7 @@ Collection.prototype.insertMany = function(docs, options, callback) {
501486
if (typeof options === 'function') (callback = options), (options = {});
502487
options = options ? Object.assign({}, options) : { ordered: true };
503488

504-
if (!Array.isArray(docs) && typeof callback === 'function') {
505-
return callback(
506-
MongoError.create({ message: 'docs parameter must be an array of documents', driver: true })
507-
);
508-
} else if (!Array.isArray(docs)) {
509-
return new this.s.promiseLibrary((resolve, reject) => {
510-
reject(
511-
MongoError.create({ message: 'docs parameter must be an array of documents', driver: true })
512-
);
513-
});
514-
}
515-
516-
// If keep going set unordered
517-
options['serializeFunctions'] = options['serializeFunctions'] || this.s.serializeFunctions;
518-
519-
docs = prepareDocs(this, docs, options);
520-
521-
// Generate the bulk write operations
522-
const operations = [
523-
{
524-
insertMany: docs
525-
}
526-
];
527-
528-
return executeOperation(this.s.topology, bulkWrite, [this, operations, options, callback], {
529-
resultMutator: result => mapInsertManyResults(docs, result)
530-
});
489+
return executeOperation(this.s.topology, insertMany, [this, docs, options, callback]);
531490
};
532491

533492
/**

lib/operations/collection_ops.js

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -892,6 +892,57 @@ function insertOne(coll, doc, options, callback) {
892892
});
893893
}
894894

895+
/**
896+
* Inserts an array of documents into MongoDB. If documents passed in do not contain the **_id** field,
897+
* one will be added to each of the documents missing it by the driver, mutating the document. This behavior
898+
* can be overridden by setting the **forceServerObjectId** flag.
899+
*
900+
* @method
901+
* @param {Collection} a Collection instance.
902+
* @param {object[]} docs Documents to insert.
903+
* @param {number} [options] Optional settings. See Collection.prototype.insertMany for a list of options.
904+
* @param {Collection~insertWriteOpCallback} [callback] The command result callback
905+
*/
906+
function insertMany(coll, docs, options, callback) {
907+
if (!Array.isArray(docs)) {
908+
return callback(
909+
MongoError.create({ message: 'docs parameter must be an array of documents', driver: true })
910+
);
911+
}
912+
913+
// If keep going set unordered
914+
options['serializeFunctions'] = options['serializeFunctions'] || coll.s.serializeFunctions;
915+
916+
docs = prepareDocs(coll, docs, options);
917+
918+
// Generate the bulk write operations
919+
const operations = [
920+
{
921+
insertMany: docs
922+
}
923+
];
924+
925+
bulkWrite(coll, operations, options, (err, result) => {
926+
if (err) return callback(err, null);
927+
callback(null, mapInsertManyResults(docs, result));
928+
});
929+
}
930+
931+
function mapInsertManyResults(docs, r) {
932+
const finalResult = {
933+
result: { ok: 1, n: r.insertedCount },
934+
ops: docs,
935+
insertedCount: r.insertedCount,
936+
insertedIds: r.insertedIds
937+
};
938+
939+
if (r.getLastOp()) {
940+
finalResult.result.opTime = r.getLastOp();
941+
}
942+
943+
return finalResult;
944+
}
945+
895946
/**
896947
* Determine whether the collection is a capped collection.
897948
*
@@ -1476,6 +1527,7 @@ module.exports = {
14761527
indexes,
14771528
indexExists,
14781529
indexInformation,
1530+
insertMany,
14791531
insertOne,
14801532
isCapped,
14811533
mapReduce,

lib/utils.js

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -360,7 +360,6 @@ var mergeOptionsAndWriteConcern = function(targetOptions, sourceOptions, keys, m
360360
* @param {function} operation The operation to execute
361361
* @param {array} args Arguments to apply the provided operation
362362
* @param {object} [options] Options that modify the behavior of the method
363-
* @param {function]} [options.resultMutator] Allows for the result of the operation to be changed for custom return types
364363
*/
365364
const executeOperation = (topology, operation, args, options) => {
366365
if (topology == null) {
@@ -373,7 +372,6 @@ const executeOperation = (topology, operation, args, options) => {
373372

374373
options = options || {};
375374
const Promise = topology.s.promiseLibrary;
376-
let resultMutator = options.resultMutator;
377375
let callback = args[args.length - 1];
378376

379377
// The driver sessions spec mandates that we implicitly create sessions for operations
@@ -397,12 +395,10 @@ const executeOperation = (topology, operation, args, options) => {
397395
session.endSession(() => {
398396
delete opOptions.session;
399397
if (err) return reject(err);
400-
if (resultMutator) return resolve(resultMutator(result));
401398
resolve(result);
402399
});
403400
} else {
404401
if (err) return reject(err);
405-
if (resultMutator) return resolve(resultMutator(result));
406402
resolve(result);
407403
}
408404
};

0 commit comments

Comments
 (0)