@@ -6780,7 +6780,7 @@ describe('Model', function() {
6780
6780
await users [ 2 ] . save ( ) ;
6781
6781
users [ 2 ] . name = 'I am the updated third name' ;
6782
6782
6783
- const writeOperations = User . buildBulkWriteOperations ( users ) ;
6783
+ const writeOperations = await User . buildBulkWriteOperations ( users ) ;
6784
6784
6785
6785
const desiredWriteOperations = [
6786
6786
{ insertOne : { document : users [ 0 ] } } ,
@@ -6795,7 +6795,7 @@ describe('Model', function() {
6795
6795
6796
6796
} ) ;
6797
6797
6798
- it ( 'throws an error when one document is invalid' , ( ) => {
6798
+ it ( 'throws an error when one document is invalid' , async ( ) => {
6799
6799
const userSchema = new Schema ( {
6800
6800
name : { type : String , minLength : 5 }
6801
6801
} ) ;
@@ -6810,12 +6810,11 @@ describe('Model', function() {
6810
6810
6811
6811
let err ;
6812
6812
try {
6813
- User . buildBulkWriteOperations ( users ) ;
6813
+ await User . buildBulkWriteOperations ( users ) ;
6814
6814
} catch ( error ) {
6815
6815
err = error ;
6816
6816
}
6817
6817
6818
-
6819
6818
assert . ok ( err ) ;
6820
6819
} ) ;
6821
6820
@@ -6827,10 +6826,8 @@ describe('Model', function() {
6827
6826
const User = db . model ( 'User' , userSchema ) ;
6828
6827
6829
6828
6830
- assert . throws (
6831
- function ( ) {
6832
- User . buildBulkWriteOperations ( null ) ;
6833
- } ,
6829
+ assert . rejects (
6830
+ User . buildBulkWriteOperations ( null ) ,
6834
6831
/ b u l k S a v e e x p e c t s a n a r r a y o f d o c u m e n t s t o b e p a s s e d /
6835
6832
) ;
6836
6833
} ) ;
@@ -6842,17 +6839,15 @@ describe('Model', function() {
6842
6839
const User = db . model ( 'User' , userSchema ) ;
6843
6840
6844
6841
6845
- assert . throws (
6846
- function ( ) {
6847
- User . buildBulkWriteOperations ( [
6848
- new User ( { name : 'Hafez' } ) ,
6849
- { name : 'I am not a document' }
6850
- ] ) ;
6851
- } ,
6842
+ assert . rejects (
6843
+ User . buildBulkWriteOperations ( [
6844
+ new User ( { name : 'Hafez' } ) ,
6845
+ { name : 'I am not a document' }
6846
+ ] ) ,
6852
6847
/ d o c u m e n t s \. 1 w a s n o t a m o n g o o s e d o c u m e n t /
6853
6848
) ;
6854
6849
} ) ;
6855
- it ( 'skips validation when given `skipValidation` true' , ( ) => {
6850
+ it ( 'skips validation when given `skipValidation` true' , async ( ) => {
6856
6851
const userSchema = new Schema ( {
6857
6852
name : { type : String , minLength : 5 }
6858
6853
} ) ;
@@ -6865,7 +6860,7 @@ describe('Model', function() {
6865
6860
new User ( { name : 'b' } )
6866
6861
] ;
6867
6862
6868
- const writeOperations = User . buildBulkWriteOperations ( users , { skipValidation : true } ) ;
6863
+ const writeOperations = await User . buildBulkWriteOperations ( users , { skipValidation : true } ) ;
6869
6864
6870
6865
assert . equal ( writeOperations . length , 3 ) ;
6871
6866
} ) ;
@@ -6904,7 +6899,7 @@ describe('Model', function() {
6904
6899
userToUpdate . name = 'John Doe' ;
6905
6900
6906
6901
// Act
6907
- const writeOperations = User . buildBulkWriteOperations ( [ newUser , userToUpdate ] , { timestamps : false , skipValidation : true } ) ;
6902
+ const writeOperations = await User . buildBulkWriteOperations ( [ newUser , userToUpdate ] , { timestamps : false , skipValidation : true } ) ;
6908
6903
6909
6904
// Assert
6910
6905
const timestampsOptions = writeOperations . map ( writeOperationContainer => {
@@ -6926,7 +6921,7 @@ describe('Model', function() {
6926
6921
userToUpdate . name = 'John Doe' ;
6927
6922
6928
6923
// Act
6929
- const writeOperations = User . buildBulkWriteOperations ( [ newUser , userToUpdate ] , { timestamps : true , skipValidation : true } ) ;
6924
+ const writeOperations = await User . buildBulkWriteOperations ( [ newUser , userToUpdate ] , { timestamps : true , skipValidation : true } ) ;
6930
6925
6931
6926
// Assert
6932
6927
const timestampsOptions = writeOperations . map ( writeOperationContainer => {
@@ -6948,7 +6943,7 @@ describe('Model', function() {
6948
6943
userToUpdate . name = 'John Doe' ;
6949
6944
6950
6945
// Act
6951
- const writeOperations = User . buildBulkWriteOperations ( [ newUser , userToUpdate ] , { skipValidation : true } ) ;
6946
+ const writeOperations = await User . buildBulkWriteOperations ( [ newUser , userToUpdate ] , { skipValidation : true } ) ;
6952
6947
6953
6948
// Assert
6954
6949
const timestampsOptions = writeOperations . map ( writeOperationContainer => {
@@ -7062,6 +7057,63 @@ describe('Model', function() {
7062
7057
7063
7058
} ) ;
7064
7059
7060
+ it ( 'saves documents with embedded discriminators (gh-15410)' , async function ( ) {
7061
+ const requirementSchema = new Schema ( {
7062
+ kind : { type : String , required : true } ,
7063
+ quantity : Number ,
7064
+ notes : String
7065
+ } , { _id : false , discriminatorKey : 'kind' } ) ;
7066
+
7067
+ const componentRequirementSchema = new Schema ( {
7068
+ componentTest : 'ObjectId'
7069
+ } , { _id : false } ) ;
7070
+
7071
+ const toolRequirementSchema = new Schema ( {
7072
+ toolTest : 'ObjectId'
7073
+ } , { _id : false } ) ;
7074
+
7075
+ const subRowSchema = new Schema ( {
7076
+ requirements : [ requirementSchema ]
7077
+ } , { _id : false } ) ;
7078
+
7079
+ const rowSchema = new Schema ( {
7080
+ rows : [ subRowSchema ]
7081
+ } , { _id : false } ) ;
7082
+
7083
+ const orderSchema = new Schema ( {
7084
+ code : String ,
7085
+ rows : [ rowSchema ]
7086
+ } , { timestamps : true } ) ;
7087
+
7088
+ const Requirement = requirementSchema ;
7089
+ Requirement . discriminators = { } ;
7090
+ Requirement . discriminators [ 'ComponentRequirement' ] = componentRequirementSchema ;
7091
+ Requirement . discriminators [ 'ToolRequirement' ] = toolRequirementSchema ;
7092
+
7093
+ subRowSchema . path ( 'requirements' ) . discriminator ( 'ComponentRequirement' , componentRequirementSchema ) ;
7094
+ subRowSchema . path ( 'requirements' ) . discriminator ( 'ToolRequirement' , toolRequirementSchema ) ;
7095
+
7096
+ const Order = db . model ( 'Order' , orderSchema ) ;
7097
+
7098
+ const order = await Order . create ( {
7099
+ code : 'test-2' ,
7100
+ rows : [ {
7101
+ rows : [ {
7102
+ requirements : [
7103
+ { kind : 'ComponentRequirement' , quantity : 1 } ,
7104
+ { kind : 'ToolRequirement' , quantity : 1 }
7105
+ ]
7106
+ } ]
7107
+ } ]
7108
+ } ) ;
7109
+
7110
+ const newObjectId = new mongoose . Types . ObjectId ( ) ;
7111
+ order . rows [ 0 ] . rows [ 0 ] . requirements [ 1 ] . set ( { toolTest : newObjectId . toString ( ) } ) ;
7112
+ await Order . bulkSave ( [ order ] ) ;
7113
+ const reread = await Order . findById ( order . _id ) . lean ( ) ;
7114
+ assert . strictEqual ( reread . rows [ 0 ] . rows [ 0 ] . requirements [ 1 ] . toolTest ?. toHexString ( ) , newObjectId . toHexString ( ) ) ;
7115
+ } ) ;
7116
+
7065
7117
it ( 'insertMany should throw an error if there were operations that failed validation, ' +
7066
7118
'but all operations that passed validation succeeded (gh-14572) (gh-13256)' , async function ( ) {
7067
7119
const userSchema = new Schema ( {
0 commit comments