@@ -2707,6 +2707,139 @@ describe('model: updateOne: ', function() {
27072707 assert . equal ( doc . age , 20 ) ;
27082708 } ) ;
27092709
2710+ describe ( 'bulkWrite overwriteImmutable option (gh-15781)' , function ( ) {
2711+ it ( 'updateOne can update immutable field with overwriteImmutable: true' , async function ( ) {
2712+ // Arrange
2713+ const { User } = createTestContext ( ) ;
2714+ const user = await User . create ( { name : 'John' , ssn : '123-45-6789' } ) ;
2715+ const customCreatedAt = new Date ( '2020-01-01' ) ;
2716+
2717+ // Act
2718+ await User . bulkWrite ( [ {
2719+ updateOne : {
2720+ filter : { _id : user . _id } ,
2721+ update : { createdAt : customCreatedAt , ssn : '999-99-9999' } ,
2722+ overwriteImmutable : true
2723+ }
2724+ } ] ) ;
2725+
2726+ // Assert
2727+ const updatedUser = await User . findById ( user . _id ) ;
2728+ assert . strictEqual ( updatedUser . ssn , '999-99-9999' ) ;
2729+ assert . strictEqual ( updatedUser . createdAt . valueOf ( ) , customCreatedAt . valueOf ( ) ) ;
2730+ } ) ;
2731+
2732+ it ( 'updateMany can update immutable field with overwriteImmutable: true' , async function ( ) {
2733+ // Arrange
2734+ const { User } = createTestContext ( ) ;
2735+ const user = await User . create ( { name : 'Alice' , ssn : '111-11-1111' } ) ;
2736+ const customCreatedAt = new Date ( '2020-01-01' ) ;
2737+
2738+ // Act
2739+ await User . bulkWrite ( [ {
2740+ updateMany : {
2741+ filter : { _id : user . _id } ,
2742+ update : { createdAt : customCreatedAt , ssn : '000-00-0000' } ,
2743+ overwriteImmutable : true
2744+ }
2745+ } ] ) ;
2746+
2747+ // Assert
2748+ const updatedUser = await User . findById ( user . _id ) ;
2749+ assert . strictEqual ( updatedUser . ssn , '000-00-0000' ) ;
2750+ assert . strictEqual ( updatedUser . createdAt . valueOf ( ) , customCreatedAt . valueOf ( ) ) ;
2751+ } ) ;
2752+
2753+ for ( const timestamps of [ true , false , null , undefined ] ) {
2754+ it ( `overwriting immutable createdAt with bulkWrite (gh-15781) when \`timestamps\` is \`${ timestamps } \`` , async function ( ) {
2755+ // Arrange
2756+ const schema = Schema ( { name : String } , { timestamps : true } ) ;
2757+
2758+ const Model = db . model ( 'Test' , schema ) ;
2759+
2760+ const doc1 = await Model . create ( { name : 'gh-15781-1' } ) ;
2761+ const doc2 = await Model . create ( { name : 'gh-15781-2' } ) ;
2762+
2763+ // Act
2764+ const createdAt = new Date ( '2011-06-01' ) ;
2765+
2766+ await Model . bulkWrite ( [
2767+ {
2768+ updateOne : {
2769+ filter : { _id : doc1 . _id } ,
2770+ update : { createdAt } ,
2771+ overwriteImmutable : true ,
2772+ timestamps
2773+ }
2774+ } ,
2775+ {
2776+ updateMany : {
2777+ filter : { _id : doc2 . _id } ,
2778+ update : { createdAt } ,
2779+ overwriteImmutable : true ,
2780+ timestamps
2781+ }
2782+ }
2783+ ] ) ;
2784+
2785+ // Assert
2786+ const updatesDocs = await Model . find ( { _id : { $in : [ doc1 . _id , doc2 . _id ] } } ) ;
2787+
2788+ assert . equal ( updatesDocs [ 0 ] . createdAt . valueOf ( ) , createdAt . valueOf ( ) ) ;
2789+ assert . equal ( updatesDocs [ 1 ] . createdAt . valueOf ( ) , createdAt . valueOf ( ) ) ;
2790+ } ) ;
2791+
2792+ it ( `can not update immutable fields without overwriteImmutable: true and timestamps: ${ timestamps } ` , async function ( ) {
2793+ // Arrange
2794+ const { User } = createTestContext ( ) ;
2795+ const users = await User . create ( [
2796+ { name : 'Bob' , ssn : '222-22-2222' } ,
2797+ { name : 'Eve' , ssn : '333-33-3333' }
2798+ ] ) ;
2799+ const newCreatedAt = new Date ( '2020-01-01' ) ;
2800+
2801+ // Act
2802+ await User . bulkWrite ( [
2803+ {
2804+ updateOne : {
2805+ filter : { _id : users [ 0 ] . _id } ,
2806+ update : { ssn : '888-88-8888' , createdAt : newCreatedAt }
2807+ } ,
2808+ timestamps
2809+ } ,
2810+ {
2811+ updateMany : {
2812+ filter : { _id : users [ 1 ] . _id } ,
2813+ update : { ssn : '777-77-7777' , createdAt : newCreatedAt }
2814+ } ,
2815+ timestamps
2816+ }
2817+ ] ) ;
2818+
2819+
2820+ // Assert
2821+ const [ updatedUser1 , updatedUser2 ] = await Promise . all ( [
2822+ User . findById ( users [ 0 ] . _id ) ,
2823+ User . findById ( users [ 1 ] . _id )
2824+ ] ) ;
2825+ assert . strictEqual ( updatedUser1 . ssn , '222-22-2222' ) ;
2826+ assert . notStrictEqual ( updatedUser1 . createdAt . valueOf ( ) , newCreatedAt . valueOf ( ) ) ;
2827+
2828+ assert . strictEqual ( updatedUser2 . ssn , '333-33-3333' ) ;
2829+ assert . notStrictEqual ( updatedUser2 . createdAt . valueOf ( ) , newCreatedAt . valueOf ( ) ) ;
2830+ } ) ;
2831+ }
2832+
2833+ function createTestContext ( ) {
2834+ const userSchema = new Schema ( {
2835+ name : String ,
2836+ ssn : { type : String , immutable : true }
2837+ } , { timestamps : true } ) ;
2838+ const User = db . model ( 'User' , userSchema ) ;
2839+ return { User } ;
2840+ }
2841+ } ) ;
2842+
27102843 it ( 'updates buffers with `runValidators` successfully (gh-8580)' , async function ( ) {
27112844 const Test = db . model ( 'Test' , Schema ( {
27122845 data : { type : Buffer , required : true }
0 commit comments