@@ -1615,4 +1615,49 @@ describe('Bulk', function() {
1615
1615
} )
1616
1616
. then ( ( ) => client . close ( ) ) ;
1617
1617
} ) ;
1618
+
1619
+ it ( 'should preserve order of operation index in unordered bulkWrite' , function ( ) {
1620
+ const client = this . configuration . newClient ( ) ;
1621
+ return client . connect ( ) . then ( ( ) => {
1622
+ this . defer ( ( ) => client . close ( ) ) ;
1623
+
1624
+ const coll = client . db ( ) . collection ( 'bulk_write_ordering_test' ) ;
1625
+ function ignoreNsNotFound ( err ) {
1626
+ if ( ! err . message . match ( / n s n o t f o u n d / ) ) throw err ;
1627
+ }
1628
+
1629
+ return coll
1630
+ . drop ( )
1631
+ . catch ( ignoreNsNotFound )
1632
+ . then ( ( ) => coll . insert ( Array . from ( { length : 4 } , ( _ , i ) => ( { _id : i , a : i } ) ) ) )
1633
+ . then ( ( ) =>
1634
+ coll
1635
+ . createIndex ( { a : 1 } , { unique : true } )
1636
+ . then ( ( ) =>
1637
+ coll . bulkWrite (
1638
+ [
1639
+ { insertOne : { _id : 5 , a : 0 } } ,
1640
+ { updateOne : { filter : { _id : 1 } , update : { $set : { a : 0 } } } } ,
1641
+ { insertOne : { _id : 6 , a : 0 } } ,
1642
+ { updateOne : { filter : { _id : 2 } , update : { $set : { a : 0 } } } }
1643
+ ] ,
1644
+ { ordered : false }
1645
+ )
1646
+ )
1647
+ )
1648
+ . then (
1649
+ ( ) => {
1650
+ throw new Error ( 'expected a bulk error' ) ;
1651
+ } ,
1652
+ err => {
1653
+ expect ( err )
1654
+ . to . have . property ( 'writeErrors' )
1655
+ . with . length ( 2 ) ;
1656
+
1657
+ expect ( err ) . to . have . nested . property ( 'writeErrors[0].err.index' , 0 ) ;
1658
+ expect ( err ) . to . have . nested . property ( 'writeErrors[1].err.index' , 2 ) ;
1659
+ }
1660
+ ) ;
1661
+ } ) ;
1662
+ } ) ;
1618
1663
} ) ;
0 commit comments