File tree Expand file tree Collapse file tree 4 files changed +49
-6
lines changed Expand file tree Collapse file tree 4 files changed +49
-6
lines changed Original file line number Diff line number Diff line change @@ -79,7 +79,7 @@ export interface ReplaceOneModel<TSchema extends Document = Document> {
79
79
/** The filter to limit the replaced document. */
80
80
filter : Filter < TSchema > ;
81
81
/** The document with which to replace the matched document. */
82
- replacement : TSchema ;
82
+ replacement : OptionalId < TSchema > ;
83
83
/** Specifies a collation. */
84
84
collation ?: CollationOptions ;
85
85
/** The index to use. If specified, then the query system will only consider plans using the hinted index. */
Original file line number Diff line number Diff line change @@ -459,26 +459,29 @@ export class Collection<TSchema extends Document = Document> {
459
459
* @param options - Optional settings for the command
460
460
* @param callback - An optional callback, a Promise will be returned if none is provided
461
461
*/
462
- replaceOne ( filter : Filter < TSchema > , replacement : TSchema ) : Promise < UpdateResult | Document > ;
463
462
replaceOne (
464
463
filter : Filter < TSchema > ,
465
- replacement : TSchema ,
464
+ replacement : OptionalId < TSchema >
465
+ ) : Promise < UpdateResult | Document > ;
466
+ replaceOne (
467
+ filter : Filter < TSchema > ,
468
+ replacement : OptionalId < TSchema > ,
466
469
callback : Callback < UpdateResult | Document >
467
470
) : void ;
468
471
replaceOne (
469
472
filter : Filter < TSchema > ,
470
- replacement : TSchema ,
473
+ replacement : OptionalId < TSchema > ,
471
474
options : ReplaceOptions
472
475
) : Promise < UpdateResult | Document > ;
473
476
replaceOne (
474
477
filter : Filter < TSchema > ,
475
- replacement : TSchema ,
478
+ replacement : OptionalId < TSchema > ,
476
479
options : ReplaceOptions ,
477
480
callback : Callback < UpdateResult | Document >
478
481
) : void ;
479
482
replaceOne (
480
483
filter : Filter < TSchema > ,
481
- replacement : TSchema ,
484
+ replacement : OptionalId < TSchema > ,
482
485
options ?: ReplaceOptions | Callback < UpdateResult | Document > ,
483
486
callback ?: Callback < UpdateResult | Document >
484
487
) : Promise < UpdateResult | Document > | void {
Original file line number Diff line number Diff line change @@ -181,6 +181,24 @@ collectionType.bulkWrite([
181
181
}
182
182
}
183
183
] ) ;
184
+ // allow a replacement doc without an _id
185
+ collectionType . bulkWrite ( [
186
+ {
187
+ replaceOne : {
188
+ filter : { } ,
189
+ replacement : {
190
+ dateField : new Date ( ) ,
191
+ fruitTags : [ ] ,
192
+ numberField : 0 ,
193
+ readonlyFruitTags : [ ] ,
194
+ stringField : 'string' ,
195
+ subInterfaceArray : [ ] ,
196
+ subInterfaceField : { field1 : '1' , field2 : '2' }
197
+ } ,
198
+ upsert : true
199
+ }
200
+ }
201
+ ] ) ;
184
202
185
203
expectError (
186
204
collectionType . bulkWrite ( [
Original file line number Diff line number Diff line change
1
+ import { expectError } from 'tsd' ;
2
+ import { MongoClient , ObjectId } from '../../../../src' ;
3
+
4
+ // test collection.replaceX functions
5
+ const client = new MongoClient ( '' ) ;
6
+ const db = client . db ( 'test' ) ;
7
+
8
+ interface TestModel {
9
+ _id : ObjectId ;
10
+ stringField : string ;
11
+ }
12
+
13
+ const collection = db . collection < TestModel > ( 'testCollection' ) ;
14
+
15
+ // should accept a replacement doc with an _id
16
+ await collection . replaceOne ( { } , { _id : new ObjectId ( ) , stringField : 'a' } ) ;
17
+
18
+ // should accept a replacement doc without an _id
19
+ await collection . replaceOne ( { } , { stringField : 'b' } ) ;
20
+
21
+ // should not allow _id with a non-ObjectId type
22
+ expectError ( await collection . replaceOne ( { } , { _id : 1 , stringField : 'c' } ) ) ;
You can’t perform that action at this time.
0 commit comments