Skip to content

Commit d5bb496

Browse files
authored
feat: remove the collection save method
This method has been deprecated for quite some time, and now is being fully removed in favor of the standard CRUD operations with atomic operators. NODE-2736
1 parent fae7a45 commit d5bb496

12 files changed

+28
-703
lines changed

src/collection.ts

+11-80
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,7 @@ import {
1111
checkCollectionName,
1212
deprecateOptions,
1313
executeLegacyOperation,
14-
MongoDBNamespace,
15-
handleCallback,
16-
applyWriteConcern
14+
MongoDBNamespace
1715
} from './utils';
1816
import { ObjectId } from './bson';
1917
import { MongoError } from './error';
@@ -23,7 +21,7 @@ import ChangeStream = require('./change_stream');
2321
import WriteConcern = require('./write_concern');
2422
import ReadConcern = require('./read_concern');
2523
import { AggregationCursor, CommandCursor } from './cursor';
26-
import { removeDocuments, updateDocuments, insertDocuments } from './operations/common_functions';
24+
import { removeDocuments, updateDocuments } from './operations/common_functions';
2725
import AggregateOperation = require('./operations/aggregate');
2826
import BulkWriteOperation = require('./operations/bulk_write');
2927
import CountDocumentsOperation = require('./operations/count_documents');
@@ -70,7 +68,6 @@ interface Collection {
7068
insert(docs: any, options: any, callback: any): void;
7169
update(selector: any, update: any, options: any, callback: any): void;
7270
remove(selector: any, options: any, callback: any): void;
73-
save(doc: any, options: any, callback: any): void;
7471
findOne(query: any, options: any, callback: any): void;
7572
dropAllIndexes(): void;
7673
ensureIndex(fieldOrSpec: any, options: any, callback: any): void;
@@ -1455,7 +1452,7 @@ Collection.prototype.find = deprecateOptions(
14551452
deprecatedOptions: DEPRECATED_FIND_OPTIONS,
14561453
optionsIndex: 1
14571454
},
1458-
function(this: any, query: any, options: any) {
1455+
function (this: any, query: any, options: any) {
14591456
if (arguments.length > 2) {
14601457
throw new TypeError('Third parameter to `collection.find()` must be undefined');
14611458
}
@@ -1670,7 +1667,7 @@ Collection.prototype.find = deprecateOptions(
16701667
* @returns {Promise<void>} returns Promise if no callback passed
16711668
* @deprecated Use insertOne, insertMany or bulkWrite
16721669
*/
1673-
Collection.prototype.insert = deprecate(function(
1670+
Collection.prototype.insert = deprecate(function (
16741671
this: any,
16751672
docs: any,
16761673
options: any,
@@ -1710,7 +1707,7 @@ Collection.prototype.insert = deprecate(function(
17101707
* @returns {Promise<void>} returns Promise if no callback passed
17111708
* @deprecated use updateOne, updateMany or bulkWrite
17121709
*/
1713-
Collection.prototype.update = deprecate(function(
1710+
Collection.prototype.update = deprecate(function (
17141711
this: any,
17151712
selector: any,
17161713
update: any,
@@ -1755,7 +1752,7 @@ Collection.prototype.removeMany = Collection.prototype.deleteMany;
17551752
* @returns {Promise<void>} returns Promise if no callback passed
17561753
* @deprecated use deleteOne, deleteMany or bulkWrite
17571754
*/
1758-
Collection.prototype.remove = deprecate(function(
1755+
Collection.prototype.remove = deprecate(function (
17591756
this: any,
17601757
selector: any,
17611758
options: any,
@@ -1779,40 +1776,6 @@ Collection.prototype.remove = deprecate(function(
17791776
},
17801777
'collection.remove is deprecated. Use deleteOne, deleteMany, or bulkWrite instead.');
17811778

1782-
/**
1783-
* Save a document. Simple full document replacement function. Not recommended for efficiency, use atomic
1784-
* operators and update instead for more efficient operations.
1785-
*
1786-
* @function
1787-
* @param {object} doc Document to save
1788-
* @param {object} [options] Optional settings.
1789-
* @param {(number|string)} [options.w] The write concern.
1790-
* @param {number} [options.wtimeout] The write concern timeout.
1791-
* @param {boolean} [options.j=false] Specify a journal write concern.
1792-
* @param {ClientSession} [options.session] optional session to use for this operation
1793-
* @param {Collection~writeOpCallback} [callback] The command result callback
1794-
* @returns {Promise<void>} returns Promise if no callback passed
1795-
* @deprecated use insertOne, insertMany, updateOne or updateMany
1796-
*/
1797-
Collection.prototype.save = deprecate(function(
1798-
this: any,
1799-
doc: any,
1800-
options: any,
1801-
callback: Function
1802-
) {
1803-
if (typeof options === 'function') (callback = options), (options = {});
1804-
options = options || {};
1805-
1806-
// Add ignoreUndefined
1807-
if (this.s.options.ignoreUndefined) {
1808-
options = Object.assign({}, options);
1809-
options.ignoreUndefined = this.s.options.ignoreUndefined;
1810-
}
1811-
1812-
return executeLegacyOperation(this.s.topology, save, [this, doc, options, callback]);
1813-
},
1814-
'collection.save is deprecated. Use insertOne, insertMany, updateOne, or updateMany instead.');
1815-
18161779
/**
18171780
* The callback format for results
18181781
*
@@ -1870,7 +1833,7 @@ Collection.prototype.findOne = deprecateOptions(
18701833
deprecatedOptions: DEPRECATED_FIND_OPTIONS,
18711834
optionsIndex: 1
18721835
},
1873-
function(this: any, query: any, options: any, callback: Function) {
1836+
function (this: any, query: any, options: any, callback: Function) {
18741837
if (callback !== undefined && typeof callback !== 'function') {
18751838
throw new TypeError('Third parameter to `findOne()` must be a callback or undefined');
18761839
}
@@ -1921,7 +1884,7 @@ Collection.prototype.dropAllIndexes = deprecate(
19211884
* @param {Collection~resultCallback} [callback] The command result callback
19221885
* @returns {Promise<void>} returns Promise if no callback passed
19231886
*/
1924-
Collection.prototype.ensureIndex = deprecate(function(
1887+
Collection.prototype.ensureIndex = deprecate(function (
19251888
this: any,
19261889
fieldOrSpec: any,
19271890
options: any,
@@ -1967,7 +1930,7 @@ Collection.prototype.ensureIndex = deprecate(function(
19671930
* @returns {Promise<void>} returns Promise if no callback passed
19681931
* @deprecated use {@link Collection#countDocuments countDocuments} or {@link Collection#estimatedDocumentCount estimatedDocumentCount} instead
19691932
*/
1970-
Collection.prototype.count = deprecate(function(
1933+
Collection.prototype.count = deprecate(function (
19711934
this: any,
19721935
query: any,
19731936
options: any,
@@ -2059,7 +2022,7 @@ function _findAndModify(
20592022
* @returns {Promise<void>} returns Promise if no callback passed
20602023
* @deprecated use findOneAndDelete instead
20612024
*/
2062-
Collection.prototype.findAndRemove = deprecate(function(
2025+
Collection.prototype.findAndRemove = deprecate(function (
20632026
this: any,
20642027
query: any,
20652028
sort: any,
@@ -2099,7 +2062,7 @@ Collection.prototype.findAndRemove = deprecate(function(
20992062
* @returns {Promise<void>} returns Promise if no callback passed
21002063
* @deprecated MongoDB 3.6 or higher no longer supports the group command. We recommend rewriting using the aggregation framework.
21012064
*/
2102-
Collection.prototype.group = deprecate(function(
2065+
Collection.prototype.group = deprecate(function (
21032066
this: any,
21042067
keys: any,
21052068
condition: any,
@@ -2159,36 +2122,4 @@ Collection.prototype.group = deprecate(function(
21592122
},
21602123
'MongoDB 3.6 or higher no longer supports the group command. We recommend rewriting using the aggregation framework.');
21612124

2162-
/**
2163-
* Save a document.
2164-
*
2165-
* @function
2166-
* @param {Collection} coll Collection instance.
2167-
* @param {any} doc Document to save
2168-
* @param {any} [options] Optional settings. See Collection.prototype.save for a list of options.
2169-
* @param {Collection~writeOpCallback} [callback] The command result callback
2170-
* @deprecated use insertOne, insertMany, updateOne or updateMany
2171-
*/
2172-
function save(coll: any, doc: any, options?: any, callback?: Function) {
2173-
// Get the write concern options
2174-
const finalOptions = applyWriteConcern(
2175-
Object.assign({}, options),
2176-
{ db: coll.s.db, collection: coll },
2177-
options
2178-
);
2179-
// Establish if we need to perform an insert or update
2180-
if (doc._id != null) {
2181-
finalOptions.upsert = true;
2182-
return updateDocuments(coll, { _id: doc._id }, doc, finalOptions, callback);
2183-
}
2184-
2185-
// Insert the document
2186-
insertDocuments(coll, [doc], finalOptions, (err?: any, result?: any) => {
2187-
if (callback == null) return;
2188-
if (doc == null) return handleCallback(callback, null, null);
2189-
if (err) return handleCallback(callback, err, null);
2190-
handleCallback(callback, null, result);
2191-
});
2192-
}
2193-
21942125
export = Collection;

test/disabled/authentication.test.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ describe('Authentication', function () {
273273
client.logout(function (err) {
274274
test.equal(null, err);
275275

276-
// Attempt to save a document
276+
// Attempt to insert a document
277277
db.collection('test').insert({ a: 1 }, function (err) {
278278
test.ok(err != null);
279279

@@ -295,7 +295,7 @@ describe('Authentication', function () {
295295
client.logout(function (err) {
296296
test.equal(null, err);
297297

298-
// Attempt to save a document
298+
// Attempt to insert a document
299299
db.collection('test').insert({ a: 1 }, function (err) {
300300
test.ok(err != null);
301301
client.close();
@@ -338,7 +338,7 @@ describe('Authentication', function () {
338338
db.admin().addUser('admin', 'admin', function (err) {
339339
test.equal(null, err);
340340

341-
// Attempt to save a document
341+
// Attempt to insert a document
342342
db.collection('test').insert({ a: 1 }, function (err) {
343343
test.ok(err != null);
344344
client.close();
@@ -414,7 +414,7 @@ describe('Authentication', function () {
414414
db.admin().addUser('admin', 'admin', function (err) {
415415
test.equal(null, err);
416416

417-
// Attempt to save a document
417+
// Attempt to insert a document
418418
var col = db.collection('test');
419419

420420
// Initialize the Ordered Batch
@@ -466,7 +466,7 @@ describe('Authentication', function () {
466466
db.admin().addUser('admin', 'admin', function (err) {
467467
test.equal(null, err);
468468

469-
// Attempt to save a document
469+
// Attempt to insert a document
470470
var col = db.collection('test');
471471

472472
// Initialize the Ordered Batch

test/disabled/replset_failover.test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,7 @@ describe.skip('ReplSet (Failover)', function () {
323323
});
324324

325325
// Run second check
326-
collection.save({ a: 80 }, { w: 1 }, function (err) {
326+
collection.insertOne({ a: 80 }, { w: 1 }, function (err) {
327327
expect(err).to.not.exist;
328328

329329
collection.find().toArray(function (err, items) {

0 commit comments

Comments
 (0)