@@ -1692,6 +1692,77 @@ MongoDB.prototype.update = MongoDB.prototype.updateAll = function updateAll(
1692
1692
) ;
1693
1693
} ;
1694
1694
1695
+ MongoDB . prototype . upsertWithWhere = function upsertWithWhere (
1696
+ modelName ,
1697
+ where ,
1698
+ data ,
1699
+ options ,
1700
+ cb ,
1701
+ ) {
1702
+ const self = this ;
1703
+
1704
+ if ( self . debug ) {
1705
+ debug ( 'upsertWithWhere' , modelName , where , data ) ;
1706
+ }
1707
+
1708
+ let updateData = Object . assign ( { } , data ) ;
1709
+
1710
+ const idValue = self . getIdValue ( modelName , updateData ) ;
1711
+ const idName = self . idName ( modelName ) ;
1712
+
1713
+ where = self . buildWhere ( modelName , where , options ) ;
1714
+
1715
+ if ( idValue === null || idValue === undefined ) {
1716
+ delete updateData [ idName ] ; // Allow MongoDB to generate the id
1717
+ } else {
1718
+ const oid = self . coerceId ( modelName , idValue , options ) ; // Is it an Object ID?c
1719
+ updateData . _id = oid ; // Set it to _id
1720
+ if ( idName !== '_id' ) {
1721
+ delete updateData [ idName ] ;
1722
+ }
1723
+ }
1724
+
1725
+ updateData = self . toDatabase ( modelName , updateData ) ;
1726
+
1727
+ // Check for other operators and sanitize the data obj
1728
+ updateData = self . parseUpdateData ( modelName , updateData , options ) ;
1729
+
1730
+ this . execute (
1731
+ modelName ,
1732
+ 'findOneAndUpdate' ,
1733
+ where ,
1734
+ updateData ,
1735
+ {
1736
+ upsert : true ,
1737
+ returnOriginal : false ,
1738
+ sort : [ [ '_id' , 'asc' ] ] ,
1739
+ } ,
1740
+ function ( err , result ) {
1741
+ if ( err ) return cb && cb ( err ) ;
1742
+
1743
+ if ( self . debug )
1744
+ debug ( 'upsertWithWhere.callback' , modelName , where , updateData , err , info ) ;
1745
+
1746
+ const object = result && result . value ;
1747
+ self . setIdValue ( modelName , object , object . _id ) ;
1748
+ if ( object && idName !== '_id' ) {
1749
+ delete object . _id ;
1750
+ }
1751
+
1752
+ let info ;
1753
+ if ( result && result . lastErrorObject ) {
1754
+ info = { isNewInstance : ! result . lastErrorObject . updatedExisting } ;
1755
+ } else {
1756
+ debug ( 'upsertWithWhere result format not recognized: %j' , result ) ;
1757
+ }
1758
+
1759
+ if ( cb ) {
1760
+ cb ( err , self . fromDatabase ( modelName , object ) , info ) ;
1761
+ }
1762
+ } ,
1763
+ ) ;
1764
+ } ;
1765
+
1695
1766
/**
1696
1767
* Disconnect from MongoDB
1697
1768
*/
0 commit comments