Skip to content

Commit 945e915

Browse files
authored
refactor: deprecate remove, removeOne, insertMany bulk operations (#2797)
NODE-2978
1 parent be269b1 commit 945e915

7 files changed

+21
-15
lines changed

lib/bulk/common.js

+8
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const MongoError = require('../core').MongoError;
55
const ObjectID = require('../core').BSON.ObjectID;
66
const BSON = require('../core').BSON;
77
const MongoWriteConcernError = require('../core').MongoWriteConcernError;
8+
const emitWarningOnce = require('../utils').emitWarningOnce;
89
const toError = require('../utils').toError;
910
const handleCallback = require('../utils').handleCallback;
1011
const applyRetryableWrites = require('../utils').applyRetryableWrites;
@@ -737,15 +738,19 @@ class FindOperators {
737738

738739
/**
739740
* backwards compatability for deleteOne
741+
* @deprecated
740742
*/
741743
removeOne() {
744+
emitWarningOnce('bulk operation `removeOne` has been deprecated, please use `deleteOne`');
742745
return this.deleteOne();
743746
}
744747

745748
/**
746749
* backwards compatability for delete
750+
* @deprecated
747751
*/
748752
remove() {
753+
emitWarningOnce('bulk operation `remove` has been deprecated, please use `delete`');
749754
return this.delete();
750755
}
751756
}
@@ -1041,6 +1046,9 @@ class BulkOperationBase {
10411046
}
10421047

10431048
if (op.insertMany) {
1049+
emitWarningOnce(
1050+
'bulk operation `insertMany` has been deprecated; use multiple `insertOne` ops instead'
1051+
);
10441052
for (let i = 0; i < op.insertMany.length; i++) {
10451053
if (forceServerObjectId !== true && op.insertMany[i]._id == null)
10461054
op.insertMany[i]._id = new ObjectID();

lib/operations/insert_many.js

+1-5
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,7 @@ class InsertManyOperation extends OperationBase {
3030
docs = prepareDocs(coll, docs, options);
3131

3232
// Generate the bulk write operations
33-
const operations = [
34-
{
35-
insertMany: docs
36-
}
37-
];
33+
const operations = docs.map(document => ({ insertOne: { document } }));
3834

3935
const bulkWriteOperation = new BulkWriteOperation(coll, operations, options);
4036

test/functional/bulk.test.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -520,7 +520,7 @@ describe('Bulk', function() {
520520
.find({ b: 1 })
521521
.upsert()
522522
.update({ b: 1 });
523-
bulk.find({ c: 1 }).remove();
523+
bulk.find({ c: 1 }).delete();
524524

525525
bulk.execute({ w: 0 }, function(err, result) {
526526
test.equal(null, err);
@@ -1027,7 +1027,7 @@ describe('Bulk', function() {
10271027
.find({ b: 1 })
10281028
.upsert()
10291029
.update({ b: 1 });
1030-
bulk.find({ c: 1 }).remove();
1030+
bulk.find({ c: 1 }).delete();
10311031

10321032
bulk.execute({ w: 0 }, function(err, result) {
10331033
test.equal(null, err);

test/functional/crud_api.test.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -363,7 +363,8 @@ describe('CRUD API', function() {
363363
db.collection('t2_5').bulkWrite(
364364
[
365365
{ insertOne: { a: 1 } },
366-
{ insertMany: [{ g: 1 }, { g: 2 }] },
366+
{ insertOne: { document: { g: 1 } } },
367+
{ insertOne: { document: { g: 2 } } },
367368
{ updateOne: { q: { a: 2 }, u: { $set: { a: 2 } }, upsert: true } },
368369
{ updateMany: { q: { a: 2 }, u: { $set: { a: 2 } }, upsert: true } },
369370
{ deleteOne: { q: { c: 1 } } },
@@ -444,7 +445,8 @@ describe('CRUD API', function() {
444445
db.collection('t2_7').bulkWrite(
445446
[
446447
{ insertOne: { a: 1 } },
447-
{ insertMany: [{ g: 1 }, { g: 2 }] },
448+
{ insertOne: { document: { g: 1 } } },
449+
{ insertOne: { document: { g: 2 } } },
448450
{ updateOne: { q: { a: 2 }, u: { $set: { a: 2 } }, upsert: true } },
449451
{ updateMany: { q: { a: 2 }, u: { $set: { a: 2 } }, upsert: true } },
450452
{ deleteOne: { q: { c: 1 } } },

test/functional/operation_example.test.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -8746,7 +8746,7 @@ describe('Operation Examples', function() {
87468746
.upsert()
87478747
.updateOne({ $set: { b: 2 } });
87488748
batch.insert({ a: 3 });
8749-
batch.find({ a: 3 }).remove({ a: 3 });
8749+
batch.find({ a: 3 }).delete({ a: 3 });
87508750

87518751
// Execute the operations
87528752
batch.execute(function(err, result) {
@@ -8814,7 +8814,7 @@ describe('Operation Examples', function() {
88148814
.upsert()
88158815
.updateOne({ $set: { b: 2 } });
88168816
batch.insert({ a: 3 });
8817-
batch.find({ a: 3 }).remove({ a: 3 });
8817+
batch.find({ a: 3 }).delete({ a: 3 });
88188818

88198819
// Execute the operations
88208820
batch.execute(function(err, result) {

test/functional/operation_generators_example.test.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -5851,7 +5851,7 @@ describe('Operation (Generators)', function() {
58515851
.upsert()
58525852
.updateOne({ $set: { b: 2 } });
58535853
batch.insert({ a: 3 });
5854-
batch.find({ a: 3 }).remove({ a: 3 });
5854+
batch.find({ a: 3 }).delete({ a: 3 });
58555855

58565856
// Execute the operations
58575857
var result = yield batch.execute();
@@ -5924,7 +5924,7 @@ describe('Operation (Generators)', function() {
59245924
.upsert()
59255925
.updateOne({ $set: { b: 2 } });
59265926
batch.insert({ a: 3 });
5927-
batch.find({ a: 3 }).remove({ a: 3 });
5927+
batch.find({ a: 3 }).delete({ a: 3 });
59285928

59295929
// Execute the operations
59305930
var result = yield batch.execute();

test/functional/operation_promises_example.test.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -6395,7 +6395,7 @@ describe('Operation (Promises)', function() {
63956395
.upsert()
63966396
.updateOne({ $set: { b: 2 } });
63976397
batch.insert({ a: 3 });
6398-
batch.find({ a: 3 }).remove({ a: 3 });
6398+
batch.find({ a: 3 }).delete({ a: 3 });
63996399

64006400
// Execute the operations
64016401
return batch.execute().then(function(result) {
@@ -6465,7 +6465,7 @@ describe('Operation (Promises)', function() {
64656465
.upsert()
64666466
.updateOne({ $set: { b: 2 } });
64676467
batch.insert({ a: 3 });
6468-
batch.find({ a: 3 }).remove({ a: 3 });
6468+
batch.find({ a: 3 }).delete({ a: 3 });
64696469

64706470
// Execute the operations
64716471
return batch.execute().then(function(result) {

0 commit comments

Comments
 (0)