Skip to content

Commit

Permalink
feat(NODE-4684)!: remove collection insert, update, remove methods (#…
Browse files Browse the repository at this point in the history
…3500)

Co-authored-by: Bailey Pearson <bailey.pearson@mongodb.com>
  • Loading branch information
nbbeeken and baileympearson authored Jan 11, 2023
1 parent 10d757a commit 14427d1
Show file tree
Hide file tree
Showing 15 changed files with 356 additions and 647 deletions.
30 changes: 30 additions & 0 deletions etc/notes/CHANGES_5.0.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -161,3 +161,33 @@ cursor.closed // true

Everywhere the driver sends a `hello` command (initial handshake and monitoring), it will now pass the command value as `1` instead of the
previous `true` for spec compliance.

### Removed `Collection.insert`, `Collection.update`, and `Collection.remove`

Three legacy operation helpers on the collection class have been removed:

| Removed API | API to migrate to |
|------------------------------------------------|----------------------------------------------------|
| `insert(document)` | `insertOne(document)` |
| `insert(arrayOfDocuments)` | `insertMany(arrayOfDocuments)` |
| `update(filter)` | `updateMany(filter)` |
| `remove(filter)` | `deleteMany(filter)` |

The `insert` method accepted an array of documents for multi-document inserts and a single document for single document inserts. `insertOne` should now be used for single-document inserts and `insertMany` should be used for multi-document inserts.

```ts
// Single document insert:
await collection.insert({ name: 'spot' });
// Migration:
await collection.insertOne({ name: 'spot' });

// Multi-document insert:
await collection.insert([{ name: 'fido' }, { name: 'luna' }])
// Migration:
await collection.insertMany([{ name: 'fido' }, { name: 'luna' }])
```

### Removed `keepGoing` option from `BulkWriteOptions`

The `keepGoing` option was a legacy name for setting `ordered` to `false` for bulk inserts.
It was only supported by the legacy `collection.insert()` method which is now removed as noted above.
71 changes: 0 additions & 71 deletions src/collection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1551,77 +1551,6 @@ export class Collection<TSchema extends Document = Document> {
return this.s.db.s.logger;
}

/**
* 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.
*
* @deprecated Use insertOne, insertMany or bulkWrite instead. Callbacks are deprecated and will be removed in the next major version. See [mongodb-legacy](https://github.com/mongodb-js/nodejs-mongodb-legacy) for migration assistance
* @param docs - The documents to insert
* @param options - Optional settings for the command
* @param callback - An optional callback, a Promise will be returned if none is provided
*/
insert(
docs: OptionalUnlessRequiredId<TSchema>[],
options: BulkWriteOptions,
callback: Callback<InsertManyResult<TSchema>>
): Promise<InsertManyResult<TSchema>> | void {
emitWarningOnce(
'collection.insert is deprecated. Use insertOne, insertMany or bulkWrite instead.'
);
if (typeof options === 'function') (callback = options), (options = {});
options = options || { ordered: false };
docs = !Array.isArray(docs) ? [docs] : docs;

return this.insertMany(docs, options, callback);
}

/**
* Updates documents.
*
* @deprecated use updateOne, updateMany or bulkWrite. Callbacks are deprecated and will be removed in the next major version. See [mongodb-legacy](https://github.com/mongodb-js/nodejs-mongodb-legacy) for migration assistance
* @param filter - The filter for the update operation.
* @param update - The update operations to be applied to the documents
* @param options - Optional settings for the command
* @param callback - An optional callback, a Promise will be returned if none is provided
*/
update(
filter: Filter<TSchema>,
update: UpdateFilter<TSchema>,
options: UpdateOptions,
callback: Callback<Document>
): Promise<UpdateResult> | void {
emitWarningOnce(
'collection.update is deprecated. Use updateOne, updateMany, or bulkWrite instead.'
);
if (typeof options === 'function') (callback = options), (options = {});
options = options ?? {};

return this.updateMany(filter, update, options, callback);
}

/**
* Remove documents.
*
* @deprecated use deleteOne, deleteMany or bulkWrite. Callbacks are deprecated and will be removed in the next major version. See [mongodb-legacy](https://github.com/mongodb-js/nodejs-mongodb-legacy) for migration assistance
* @param filter - The filter for the remove operation.
* @param options - Optional settings for the command
* @param callback - An optional callback, a Promise will be returned if none is provided
*/
remove(
filter: Filter<TSchema>,
options: DeleteOptions,
callback: Callback
): Promise<DeleteResult> | void {
emitWarningOnce(
'collection.remove is deprecated. Use deleteOne, deleteMany, or bulkWrite instead.'
);
if (typeof options === 'function') (callback = options), (options = {});
options = options ?? {};

return this.deleteMany(filter, options, callback);
}

/**
* An estimated count of matching documents in the db to a filter.
*
Expand Down
22 changes: 0 additions & 22 deletions test/integration/collection-management/collection.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -227,28 +227,6 @@ describe('Collection', function () {
);
});

it('should perform collection remove with no callback', function (done) {
const collection = db.collection('remove_with_no_callback_bug_test');
collection.insertOne({ a: 1 }, configuration.writeConcernMax(), err => {
expect(err).to.not.exist;
collection.insertOne({ b: 1 }, configuration.writeConcernMax(), err => {
expect(err).to.not.exist;
collection.insertOne({ c: 1 }, configuration.writeConcernMax(), err => {
expect(err).to.not.exist;
collection.remove({ a: 1 }, configuration.writeConcernMax(), err => {
expect(err).to.not.exist;
// Let's perform a count
collection.countDocuments((err, count) => {
expect(err).to.not.exist;
expect(count).to.equal(2);
done();
});
});
});
});
});
});

it('should correctly read back document with null', function (done) {
db.createCollection('shouldCorrectlyReadBackDocumentWithNull', {}, (err, collection) => {
// Insert a document with a date
Expand Down
65 changes: 0 additions & 65 deletions test/integration/crud/crud_api.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -655,71 +655,6 @@ describe('CRUD API', function () {
}
});

it('should correctly execute remove methods using crud api', {
// Add a tag that our runner can trigger on
// in this case we are setting that node needs to be higher than 0.10.X to run
metadata: {
requires: { topology: ['single', 'replicaset', 'sharded', 'ssl', 'heap', 'wiredtiger'] }
},

test: function (done) {
client.connect(function (err, client) {
const db = client.db();

//
// Legacy update method
// -------------------------------------------------
const legacyRemove = function () {
deleteOne();
};

//
// Update one method
// -------------------------------------------------
const deleteOne = function () {
db.collection('t4_2').insertMany(
[{ a: 1 }, { a: 1 }],
{ writeConcern: { w: 1 } },
(err, r) => {
expect(err).to.not.exist;
expect(r).property('insertedCount').to.equal(2);

db.collection('t4_2').deleteOne({ a: 1 }, (err, r) => {
expect(err).to.not.exist;
expect(r).property('deletedCount').to.equal(1);

deleteMany();
});
}
);
};

//
// Update many method
// -------------------------------------------------
const deleteMany = function () {
db.collection('t4_3').insertMany(
[{ a: 1 }, { a: 1 }],
{ writeConcern: { w: 1 } },
(err, r) => {
expect(err).to.not.exist;
expect(r).property('insertedCount').to.equal(2);

db.collection('t4_3').deleteMany({ a: 1 }, (err, r) => {
expect(err).to.not.exist;
expect(r).property('deletedCount').to.equal(2);

client.close(done);
});
}
);
};

legacyRemove();
});
}
});

it('should correctly execute findAndModify methods using crud api', {
// Add a tag that our runner can trigger on
// in this case we are setting that node needs to be higher than 0.10.X to run
Expand Down
Loading

0 comments on commit 14427d1

Please sign in to comment.