Skip to content

Commit

Permalink
fix: make _id optional in replacement for replaceOne operations
Browse files Browse the repository at this point in the history
The _id field is immutable in a replaceOne operation, so it does not
make sense to require passing it. Also, this makes replaceOne consistent
with insertOne.
  • Loading branch information
tusbar committed Nov 17, 2021
1 parent 52520aa commit 6d30520
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/bulk/common.ts
Expand Up @@ -79,7 +79,7 @@ export interface ReplaceOneModel<TSchema extends Document = Document> {
/** The filter to limit the replaced document. */
filter: Filter<TSchema>;
/** The document with which to replace the matched document. */
replacement: TSchema;
replacement: OptionalId<TSchema>;
/** Specifies a collation. */
collation?: CollationOptions;
/** The index to use. If specified, then the query system will only consider plans using the hinted index. */
Expand Down
13 changes: 8 additions & 5 deletions src/collection.ts
Expand Up @@ -459,26 +459,29 @@ export class Collection<TSchema extends Document = Document> {
* @param options - Optional settings for the command
* @param callback - An optional callback, a Promise will be returned if none is provided
*/
replaceOne(filter: Filter<TSchema>, replacement: TSchema): Promise<UpdateResult | Document>;
replaceOne(
filter: Filter<TSchema>,
replacement: TSchema,
replacement: OptionalId<TSchema>
): Promise<UpdateResult | Document>;
replaceOne(
filter: Filter<TSchema>,
replacement: OptionalId<TSchema>,
callback: Callback<UpdateResult | Document>
): void;
replaceOne(
filter: Filter<TSchema>,
replacement: TSchema,
replacement: OptionalId<TSchema>,
options: ReplaceOptions
): Promise<UpdateResult | Document>;
replaceOne(
filter: Filter<TSchema>,
replacement: TSchema,
replacement: OptionalId<TSchema>,
options: ReplaceOptions,
callback: Callback<UpdateResult | Document>
): void;
replaceOne(
filter: Filter<TSchema>,
replacement: TSchema,
replacement: OptionalId<TSchema>,
options?: ReplaceOptions | Callback<UpdateResult | Document>,
callback?: Callback<UpdateResult | Document>
): Promise<UpdateResult | Document> | void {
Expand Down
18 changes: 18 additions & 0 deletions test/types/community/collection/bulkWrite.test-d.ts
Expand Up @@ -181,6 +181,24 @@ collectionType.bulkWrite([
}
}
]);
// allow a replacement doc without an _id
collectionType.bulkWrite([
{
replaceOne: {
filter: {},
replacement: {
dateField: new Date(),
fruitTags: [],
numberField: 0,
readonlyFruitTags: [],
stringField: 'string',
subInterfaceArray: [],
subInterfaceField: { field1: '1', field2: '2' }
},
upsert: true
}
}
]);

expectError(
collectionType.bulkWrite([
Expand Down
22 changes: 22 additions & 0 deletions test/types/community/collection/replaceX.test-d.ts
@@ -0,0 +1,22 @@
import { expectError } from 'tsd';
import { MongoClient, ObjectId } from '../../../../src';

// test collection.replaceX functions
const client = new MongoClient('');
const db = client.db('test');

interface TestModel {
_id: ObjectId;
stringField: string;
}

const collection = db.collection<TestModel>('testCollection');

// should accept a replacement doc with an _id
await collection.replaceOne({}, { _id: new ObjectId(), stringField: 'a' });

// should accept a replacement doc without an _id
await collection.replaceOne({}, { stringField: 'b' });

// should not allow _id with a non-ObjectId type
expectError(await collection.replaceOne({}, { _id: 1, stringField: 'c' }));

0 comments on commit 6d30520

Please sign in to comment.