@@ -2680,34 +2680,137 @@ describe('model: updateOne: ', function() {
26802680 assert . equal ( doc . age , 20 ) ;
26812681 } ) ;
26822682
2683- it ( 'overwriting immutable createdAt with bulkWrite (gh-15781)' , async function ( ) {
2684- const start = new Date ( ) . valueOf ( ) ;
2685- const schema = Schema ( {
2686- createdAt : {
2687- type : mongoose . Schema . Types . Date ,
2688- immutable : true
2689- } ,
2690- name : String
2691- } , { timestamps : true } ) ;
2683+ describe ( 'bulkWrite overwriteImmutable option (gh-15781)' , function ( ) {
2684+ it ( 'updateOne can update immutable field with overwriteImmutable: true' , async function ( ) {
2685+ // Arrange
2686+ const { User } = createTestContext ( ) ;
2687+ const user = await User . create ( { name : 'John' , ssn : '123-45-6789' } ) ;
2688+ const customCreatedAt = new Date ( '2020-01-01' ) ;
2689+
2690+ // Act
2691+ await User . bulkWrite ( [ {
2692+ updateOne : {
2693+ filter : { _id : user . _id } ,
2694+ update : { createdAt : customCreatedAt , ssn : '999-99-9999' } ,
2695+ overwriteImmutable : true
2696+ }
2697+ } ] ) ;
2698+
2699+ // Assert
2700+ const updatedUser = await User . findById ( user . _id ) ;
2701+ assert . strictEqual ( updatedUser . ssn , '999-99-9999' ) ;
2702+ assert . strictEqual ( updatedUser . createdAt . valueOf ( ) , customCreatedAt . valueOf ( ) ) ;
2703+ } ) ;
2704+
2705+ it ( 'updateMany can update immutable field with overwriteImmutable: true' , async function ( ) {
2706+ // Arrange
2707+ const { User } = createTestContext ( ) ;
2708+ const user = await User . create ( { name : 'Alice' , ssn : '111-11-1111' } ) ;
2709+ const customCreatedAt = new Date ( '2020-01-01' ) ;
2710+
2711+ // Act
2712+ await User . bulkWrite ( [ {
2713+ updateMany : {
2714+ filter : { _id : user . _id } ,
2715+ update : { createdAt : customCreatedAt , ssn : '000-00-0000' } ,
2716+ overwriteImmutable : true
2717+ }
2718+ } ] ) ;
26922719
2693- const Model = db . model ( 'Test' , schema ) ;
2720+ // Assert
2721+ const updatedUser = await User . findById ( user . _id ) ;
2722+ assert . strictEqual ( updatedUser . ssn , '000-00-0000' ) ;
2723+ assert . strictEqual ( updatedUser . createdAt . valueOf ( ) , customCreatedAt . valueOf ( ) ) ;
2724+ } ) ;
26942725
2695- await Model . create ( { name : 'gh-15781' } ) ;
2696- let doc = await Model . collection . findOne ( { name : 'gh-15781' } ) ;
2697- assert . ok ( doc . createdAt . valueOf ( ) >= start ) ;
2726+ for ( const timestamps of [ true , false , null , undefined ] ) {
2727+ it ( `overwriting immutable createdAt with bulkWrite (gh-15781) when \`timestamps\` is \`${ timestamps } \`` , async function ( ) {
2728+ // Arrange
2729+ const schema = Schema ( { name : String } , { timestamps : true } ) ;
26982730
2699- const createdAt = new Date ( '2011-06-01' ) ;
2700- assert . ok ( createdAt . valueOf ( ) < start . valueOf ( ) ) ;
2701- await Model . bulkWrite ( [ {
2702- updateOne : {
2703- filter : { _id : doc . _id } ,
2704- update : { name : 'gh-15781 update' , createdAt } ,
2705- overwriteImmutable : true ,
2706- timestamps : false
2707- }
2708- } ] ) ;
2709- doc = await Model . collection . findOne ( { name : 'gh-15781 update' } ) ;
2710- assert . equal ( doc . createdAt . valueOf ( ) , createdAt . valueOf ( ) ) ;
2731+ const Model = db . model ( 'Test' , schema ) ;
2732+
2733+ const doc1 = await Model . create ( { name : 'gh-15781-1' } ) ;
2734+ const doc2 = await Model . create ( { name : 'gh-15781-2' } ) ;
2735+
2736+ // Act
2737+ const createdAt = new Date ( '2011-06-01' ) ;
2738+
2739+ await Model . bulkWrite ( [
2740+ {
2741+ updateOne : {
2742+ filter : { _id : doc1 . _id } ,
2743+ update : { createdAt } ,
2744+ overwriteImmutable : true ,
2745+ timestamps
2746+ }
2747+ } ,
2748+ {
2749+ updateMany : {
2750+ filter : { _id : doc2 . _id } ,
2751+ update : { createdAt } ,
2752+ overwriteImmutable : true ,
2753+ timestamps
2754+ }
2755+ }
2756+ ] ) ;
2757+
2758+ // Assert
2759+ const updatesDocs = await Model . find ( { _id : { $in : [ doc1 . _id , doc2 . _id ] } } ) ;
2760+
2761+ assert . equal ( updatesDocs [ 0 ] . createdAt . valueOf ( ) , createdAt . valueOf ( ) ) ;
2762+ assert . equal ( updatesDocs [ 1 ] . createdAt . valueOf ( ) , createdAt . valueOf ( ) ) ;
2763+ } ) ;
2764+
2765+ it ( `can not update immutable fields without overwriteImmutable: true and timestamps: ${ timestamps } ` , async function ( ) {
2766+ // Arrange
2767+ const { User } = createTestContext ( ) ;
2768+ const users = await User . create ( [
2769+ { name : 'Bob' , ssn : '222-22-2222' } ,
2770+ { name : 'Eve' , ssn : '333-33-3333' }
2771+ ] ) ;
2772+ const newCreatedAt = new Date ( '2020-01-01' ) ;
2773+
2774+ // Act
2775+ await User . bulkWrite ( [
2776+ {
2777+ updateOne : {
2778+ filter : { _id : users [ 0 ] . _id } ,
2779+ update : { ssn : '888-88-8888' , createdAt : newCreatedAt }
2780+ } ,
2781+ timestamps
2782+ } ,
2783+ {
2784+ updateMany : {
2785+ filter : { _id : users [ 1 ] . _id } ,
2786+ update : { ssn : '777-77-7777' , createdAt : newCreatedAt }
2787+ } ,
2788+ timestamps
2789+ }
2790+ ] ) ;
2791+
2792+
2793+ // Assert
2794+ const [ updatedUser1 , updatedUser2 ] = await Promise . all ( [
2795+ User . findById ( users [ 0 ] . _id ) ,
2796+ User . findById ( users [ 1 ] . _id )
2797+ ] ) ;
2798+ assert . strictEqual ( updatedUser1 . ssn , '222-22-2222' ) ;
2799+ assert . notStrictEqual ( updatedUser1 . createdAt . valueOf ( ) , newCreatedAt . valueOf ( ) ) ;
2800+
2801+ assert . strictEqual ( updatedUser2 . ssn , '333-33-3333' ) ;
2802+ assert . notStrictEqual ( updatedUser2 . createdAt . valueOf ( ) , newCreatedAt . valueOf ( ) ) ;
2803+ } ) ;
2804+ }
2805+
2806+ function createTestContext ( ) {
2807+ const userSchema = new Schema ( {
2808+ name : String ,
2809+ ssn : { type : String , immutable : true }
2810+ } , { timestamps : true } ) ;
2811+ const User = db . model ( 'User' , userSchema ) ;
2812+ return { User } ;
2813+ }
27112814 } ) ;
27122815
27132816 it ( 'updates buffers with `runValidators` successfully (gh-8580)' , async function ( ) {
0 commit comments