Skip to content

Commit

Permalink
refactor: deprecate remove, removeOne, insertMany bulk operations (#2797
Browse files Browse the repository at this point in the history
  • Loading branch information
emadum committed May 5, 2021
1 parent be269b1 commit 945e915
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 15 deletions.
8 changes: 8 additions & 0 deletions lib/bulk/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const MongoError = require('../core').MongoError;
const ObjectID = require('../core').BSON.ObjectID;
const BSON = require('../core').BSON;
const MongoWriteConcernError = require('../core').MongoWriteConcernError;
const emitWarningOnce = require('../utils').emitWarningOnce;
const toError = require('../utils').toError;
const handleCallback = require('../utils').handleCallback;
const applyRetryableWrites = require('../utils').applyRetryableWrites;
Expand Down Expand Up @@ -737,15 +738,19 @@ class FindOperators {

/**
* backwards compatability for deleteOne
* @deprecated
*/
removeOne() {
emitWarningOnce('bulk operation `removeOne` has been deprecated, please use `deleteOne`');
return this.deleteOne();
}

/**
* backwards compatability for delete
* @deprecated
*/
remove() {
emitWarningOnce('bulk operation `remove` has been deprecated, please use `delete`');
return this.delete();
}
}
Expand Down Expand Up @@ -1041,6 +1046,9 @@ class BulkOperationBase {
}

if (op.insertMany) {
emitWarningOnce(
'bulk operation `insertMany` has been deprecated; use multiple `insertOne` ops instead'
);
for (let i = 0; i < op.insertMany.length; i++) {
if (forceServerObjectId !== true && op.insertMany[i]._id == null)
op.insertMany[i]._id = new ObjectID();
Expand Down
6 changes: 1 addition & 5 deletions lib/operations/insert_many.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,7 @@ class InsertManyOperation extends OperationBase {
docs = prepareDocs(coll, docs, options);

// Generate the bulk write operations
const operations = [
{
insertMany: docs
}
];
const operations = docs.map(document => ({ insertOne: { document } }));

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

Expand Down
4 changes: 2 additions & 2 deletions test/functional/bulk.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -520,7 +520,7 @@ describe('Bulk', function() {
.find({ b: 1 })
.upsert()
.update({ b: 1 });
bulk.find({ c: 1 }).remove();
bulk.find({ c: 1 }).delete();

bulk.execute({ w: 0 }, function(err, result) {
test.equal(null, err);
Expand Down Expand Up @@ -1027,7 +1027,7 @@ describe('Bulk', function() {
.find({ b: 1 })
.upsert()
.update({ b: 1 });
bulk.find({ c: 1 }).remove();
bulk.find({ c: 1 }).delete();

bulk.execute({ w: 0 }, function(err, result) {
test.equal(null, err);
Expand Down
6 changes: 4 additions & 2 deletions test/functional/crud_api.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,8 @@ describe('CRUD API', function() {
db.collection('t2_5').bulkWrite(
[
{ insertOne: { a: 1 } },
{ insertMany: [{ g: 1 }, { g: 2 }] },
{ insertOne: { document: { g: 1 } } },
{ insertOne: { document: { g: 2 } } },
{ updateOne: { q: { a: 2 }, u: { $set: { a: 2 } }, upsert: true } },
{ updateMany: { q: { a: 2 }, u: { $set: { a: 2 } }, upsert: true } },
{ deleteOne: { q: { c: 1 } } },
Expand Down Expand Up @@ -444,7 +445,8 @@ describe('CRUD API', function() {
db.collection('t2_7').bulkWrite(
[
{ insertOne: { a: 1 } },
{ insertMany: [{ g: 1 }, { g: 2 }] },
{ insertOne: { document: { g: 1 } } },
{ insertOne: { document: { g: 2 } } },
{ updateOne: { q: { a: 2 }, u: { $set: { a: 2 } }, upsert: true } },
{ updateMany: { q: { a: 2 }, u: { $set: { a: 2 } }, upsert: true } },
{ deleteOne: { q: { c: 1 } } },
Expand Down
4 changes: 2 additions & 2 deletions test/functional/operation_example.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8746,7 +8746,7 @@ describe('Operation Examples', function() {
.upsert()
.updateOne({ $set: { b: 2 } });
batch.insert({ a: 3 });
batch.find({ a: 3 }).remove({ a: 3 });
batch.find({ a: 3 }).delete({ a: 3 });

// Execute the operations
batch.execute(function(err, result) {
Expand Down Expand Up @@ -8814,7 +8814,7 @@ describe('Operation Examples', function() {
.upsert()
.updateOne({ $set: { b: 2 } });
batch.insert({ a: 3 });
batch.find({ a: 3 }).remove({ a: 3 });
batch.find({ a: 3 }).delete({ a: 3 });

// Execute the operations
batch.execute(function(err, result) {
Expand Down
4 changes: 2 additions & 2 deletions test/functional/operation_generators_example.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5851,7 +5851,7 @@ describe('Operation (Generators)', function() {
.upsert()
.updateOne({ $set: { b: 2 } });
batch.insert({ a: 3 });
batch.find({ a: 3 }).remove({ a: 3 });
batch.find({ a: 3 }).delete({ a: 3 });

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

// Execute the operations
var result = yield batch.execute();
Expand Down
4 changes: 2 additions & 2 deletions test/functional/operation_promises_example.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6395,7 +6395,7 @@ describe('Operation (Promises)', function() {
.upsert()
.updateOne({ $set: { b: 2 } });
batch.insert({ a: 3 });
batch.find({ a: 3 }).remove({ a: 3 });
batch.find({ a: 3 }).delete({ a: 3 });

// Execute the operations
return batch.execute().then(function(result) {
Expand Down Expand Up @@ -6465,7 +6465,7 @@ describe('Operation (Promises)', function() {
.upsert()
.updateOne({ $set: { b: 2 } });
batch.insert({ a: 3 });
batch.find({ a: 3 }).remove({ a: 3 });
batch.find({ a: 3 }).delete({ a: 3 });

// Execute the operations
return batch.execute().then(function(result) {
Expand Down

0 comments on commit 945e915

Please sign in to comment.