Skip to content

Commit 14427d1

Browse files
feat(NODE-4684)!: remove collection insert, update, remove methods (#3500)
Co-authored-by: Bailey Pearson <[email protected]>
1 parent 10d757a commit 14427d1

15 files changed

+356
-647
lines changed

etc/notes/CHANGES_5.0.0.md

+30
Original file line numberDiff line numberDiff line change
@@ -161,3 +161,33 @@ cursor.closed // true
161161

162162
Everywhere the driver sends a `hello` command (initial handshake and monitoring), it will now pass the command value as `1` instead of the
163163
previous `true` for spec compliance.
164+
165+
### Removed `Collection.insert`, `Collection.update`, and `Collection.remove`
166+
167+
Three legacy operation helpers on the collection class have been removed:
168+
169+
| Removed API | API to migrate to |
170+
|------------------------------------------------|----------------------------------------------------|
171+
| `insert(document)` | `insertOne(document)` |
172+
| `insert(arrayOfDocuments)` | `insertMany(arrayOfDocuments)` |
173+
| `update(filter)` | `updateMany(filter)` |
174+
| `remove(filter)` | `deleteMany(filter)` |
175+
176+
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.
177+
178+
```ts
179+
// Single document insert:
180+
await collection.insert({ name: 'spot' });
181+
// Migration:
182+
await collection.insertOne({ name: 'spot' });
183+
184+
// Multi-document insert:
185+
await collection.insert([{ name: 'fido' }, { name: 'luna' }])
186+
// Migration:
187+
await collection.insertMany([{ name: 'fido' }, { name: 'luna' }])
188+
```
189+
190+
### Removed `keepGoing` option from `BulkWriteOptions`
191+
192+
The `keepGoing` option was a legacy name for setting `ordered` to `false` for bulk inserts.
193+
It was only supported by the legacy `collection.insert()` method which is now removed as noted above.

src/collection.ts

-71
Original file line numberDiff line numberDiff line change
@@ -1551,77 +1551,6 @@ export class Collection<TSchema extends Document = Document> {
15511551
return this.s.db.s.logger;
15521552
}
15531553

1554-
/**
1555-
* Inserts a single document or a an array of documents into MongoDB. If documents passed in do not contain the **_id** field,
1556-
* one will be added to each of the documents missing it by the driver, mutating the document. This behavior
1557-
* can be overridden by setting the **forceServerObjectId** flag.
1558-
*
1559-
* @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
1560-
* @param docs - The documents to insert
1561-
* @param options - Optional settings for the command
1562-
* @param callback - An optional callback, a Promise will be returned if none is provided
1563-
*/
1564-
insert(
1565-
docs: OptionalUnlessRequiredId<TSchema>[],
1566-
options: BulkWriteOptions,
1567-
callback: Callback<InsertManyResult<TSchema>>
1568-
): Promise<InsertManyResult<TSchema>> | void {
1569-
emitWarningOnce(
1570-
'collection.insert is deprecated. Use insertOne, insertMany or bulkWrite instead.'
1571-
);
1572-
if (typeof options === 'function') (callback = options), (options = {});
1573-
options = options || { ordered: false };
1574-
docs = !Array.isArray(docs) ? [docs] : docs;
1575-
1576-
return this.insertMany(docs, options, callback);
1577-
}
1578-
1579-
/**
1580-
* Updates documents.
1581-
*
1582-
* @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
1583-
* @param filter - The filter for the update operation.
1584-
* @param update - The update operations to be applied to the documents
1585-
* @param options - Optional settings for the command
1586-
* @param callback - An optional callback, a Promise will be returned if none is provided
1587-
*/
1588-
update(
1589-
filter: Filter<TSchema>,
1590-
update: UpdateFilter<TSchema>,
1591-
options: UpdateOptions,
1592-
callback: Callback<Document>
1593-
): Promise<UpdateResult> | void {
1594-
emitWarningOnce(
1595-
'collection.update is deprecated. Use updateOne, updateMany, or bulkWrite instead.'
1596-
);
1597-
if (typeof options === 'function') (callback = options), (options = {});
1598-
options = options ?? {};
1599-
1600-
return this.updateMany(filter, update, options, callback);
1601-
}
1602-
1603-
/**
1604-
* Remove documents.
1605-
*
1606-
* @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
1607-
* @param filter - The filter for the remove operation.
1608-
* @param options - Optional settings for the command
1609-
* @param callback - An optional callback, a Promise will be returned if none is provided
1610-
*/
1611-
remove(
1612-
filter: Filter<TSchema>,
1613-
options: DeleteOptions,
1614-
callback: Callback
1615-
): Promise<DeleteResult> | void {
1616-
emitWarningOnce(
1617-
'collection.remove is deprecated. Use deleteOne, deleteMany, or bulkWrite instead.'
1618-
);
1619-
if (typeof options === 'function') (callback = options), (options = {});
1620-
options = options ?? {};
1621-
1622-
return this.deleteMany(filter, options, callback);
1623-
}
1624-
16251554
/**
16261555
* An estimated count of matching documents in the db to a filter.
16271556
*

test/integration/collection-management/collection.test.ts

-22
Original file line numberDiff line numberDiff line change
@@ -227,28 +227,6 @@ describe('Collection', function () {
227227
);
228228
});
229229

230-
it('should perform collection remove with no callback', function (done) {
231-
const collection = db.collection('remove_with_no_callback_bug_test');
232-
collection.insertOne({ a: 1 }, configuration.writeConcernMax(), err => {
233-
expect(err).to.not.exist;
234-
collection.insertOne({ b: 1 }, configuration.writeConcernMax(), err => {
235-
expect(err).to.not.exist;
236-
collection.insertOne({ c: 1 }, configuration.writeConcernMax(), err => {
237-
expect(err).to.not.exist;
238-
collection.remove({ a: 1 }, configuration.writeConcernMax(), err => {
239-
expect(err).to.not.exist;
240-
// Let's perform a count
241-
collection.countDocuments((err, count) => {
242-
expect(err).to.not.exist;
243-
expect(count).to.equal(2);
244-
done();
245-
});
246-
});
247-
});
248-
});
249-
});
250-
});
251-
252230
it('should correctly read back document with null', function (done) {
253231
db.createCollection('shouldCorrectlyReadBackDocumentWithNull', {}, (err, collection) => {
254232
// Insert a document with a date

test/integration/crud/crud_api.test.ts

-65
Original file line numberDiff line numberDiff line change
@@ -655,71 +655,6 @@ describe('CRUD API', function () {
655655
}
656656
});
657657

658-
it('should correctly execute remove methods using crud api', {
659-
// Add a tag that our runner can trigger on
660-
// in this case we are setting that node needs to be higher than 0.10.X to run
661-
metadata: {
662-
requires: { topology: ['single', 'replicaset', 'sharded', 'ssl', 'heap', 'wiredtiger'] }
663-
},
664-
665-
test: function (done) {
666-
client.connect(function (err, client) {
667-
const db = client.db();
668-
669-
//
670-
// Legacy update method
671-
// -------------------------------------------------
672-
const legacyRemove = function () {
673-
deleteOne();
674-
};
675-
676-
//
677-
// Update one method
678-
// -------------------------------------------------
679-
const deleteOne = function () {
680-
db.collection('t4_2').insertMany(
681-
[{ a: 1 }, { a: 1 }],
682-
{ writeConcern: { w: 1 } },
683-
(err, r) => {
684-
expect(err).to.not.exist;
685-
expect(r).property('insertedCount').to.equal(2);
686-
687-
db.collection('t4_2').deleteOne({ a: 1 }, (err, r) => {
688-
expect(err).to.not.exist;
689-
expect(r).property('deletedCount').to.equal(1);
690-
691-
deleteMany();
692-
});
693-
}
694-
);
695-
};
696-
697-
//
698-
// Update many method
699-
// -------------------------------------------------
700-
const deleteMany = function () {
701-
db.collection('t4_3').insertMany(
702-
[{ a: 1 }, { a: 1 }],
703-
{ writeConcern: { w: 1 } },
704-
(err, r) => {
705-
expect(err).to.not.exist;
706-
expect(r).property('insertedCount').to.equal(2);
707-
708-
db.collection('t4_3').deleteMany({ a: 1 }, (err, r) => {
709-
expect(err).to.not.exist;
710-
expect(r).property('deletedCount').to.equal(2);
711-
712-
client.close(done);
713-
});
714-
}
715-
);
716-
};
717-
718-
legacyRemove();
719-
});
720-
}
721-
});
722-
723658
it('should correctly execute findAndModify methods using crud api', {
724659
// Add a tag that our runner can trigger on
725660
// in this case we are setting that node needs to be higher than 0.10.X to run

0 commit comments

Comments
 (0)