Skip to content

Commit

Permalink
feat(NODE-4648)!: remove collection insert, update, remove methods
Browse files Browse the repository at this point in the history
  • Loading branch information
nbbeeken committed Jan 3, 2023
1 parent 8900d40 commit 672ddc1
Show file tree
Hide file tree
Showing 14 changed files with 324 additions and 610 deletions.
21 changes: 21 additions & 0 deletions etc/notes/CHANGES_5.0.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,24 @@ for await (const doc of cursor) {

cursor.closed // true
```
### Removed `Collection.insert`, `Collection.update`, and `Collection.remove`

These legacy methods have been removed. `update` and `remove` invocations are identical to `updateMany` and `deleteMany` respectively.
The `insert` method is equivalent to `insertMany` but the first argument **MUST** be an array.

```ts
// Single document insert:
await collection.insert({ name: 'spot' });
// Migration:
await collection.insertMany([{ 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.
2 changes: 0 additions & 2 deletions src/bulk/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -876,8 +876,6 @@ export interface BulkWriteOptions extends CommandOperationOptions {
bypassDocumentValidation?: boolean;
/** If true, when an insert fails, don't execute the remaining writes. If false, continue with remaining inserts when one fails. */
ordered?: boolean;
/** @deprecated use `ordered` instead */
keepGoing?: boolean;
/** Force server to assign _id values instead of driver. */
forceServerObjectId?: boolean;
/** Map of parameter names and values that can be accessed using $$var (requires MongoDB 5.0). */
Expand Down
75 changes: 0 additions & 75 deletions src/collection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1634,81 +1634,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;

if (options.keepGoing === true) {
options.ordered = false;
}

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
Loading

0 comments on commit 672ddc1

Please sign in to comment.