@@ -2058,11 +2058,6 @@ Query.prototype._distinct = async function _distinct() {
20582058 */
20592059
20602060Query . prototype . updateMany = function updateMany ( criteria , doc , options ) {
2061- if ( arguments . length === 1 ) {
2062- doc = criteria ;
2063- criteria = options = undefined ;
2064- }
2065-
20662061 return _update ( this , 'updateMany' , criteria , doc , options ) ;
20672062} ;
20682063
@@ -2093,11 +2088,6 @@ Query.prototype._updateMany = async function() {
20932088 */
20942089
20952090Query . prototype . updateOne = function updateOne ( criteria , doc , options ) {
2096- if ( arguments . length === 1 ) {
2097- doc = criteria ;
2098- criteria = options = undefined ;
2099- }
2100-
21012091 return _update ( this , 'updateOne' , criteria , doc , options ) ;
21022092} ;
21032093
@@ -2128,11 +2118,6 @@ Query.prototype._updateOne = async function() {
21282118 */
21292119
21302120Query . prototype . replaceOne = function replaceOne ( criteria , doc , options ) {
2131- if ( arguments . length === 1 ) {
2132- doc = criteria ;
2133- criteria = options = undefined ;
2134- }
2135-
21362121 this . setOptions ( { overwrite : true } ) ;
21372122 return _update ( this , 'replaceOne' , criteria , doc , options ) ;
21382123} ;
@@ -2255,7 +2240,7 @@ Query.prototype._deleteMany = async function() {
22552240} ;
22562241
22572242/**
2258- * Issues a mongodb [findAndModify](http://www.mongodb.org/display/DOCS/findAndModify+Command) update command.
2243+ * Issues a mongodb findOneAndUpdate command.
22592244 *
22602245 * Finds a matching document, updates it according to the `update` arg, passing any `options`, and returns the found document (if any).
22612246 *
@@ -2288,11 +2273,6 @@ Query.prototype.findOneAndUpdate = function(criteria, doc, options) {
22882273 this . op = 'findOneAndUpdate' ;
22892274 this . _validate ( ) ;
22902275
2291- if ( arguments . length === 1 ) {
2292- doc = criteria ;
2293- criteria = options = undefined ;
2294- }
2295-
22962276 if ( Query . canMerge ( criteria ) ) {
22972277 this . merge ( criteria ) ;
22982278 }
@@ -2319,6 +2299,68 @@ Query.prototype._findOneAndUpdate = async function() {
23192299 return this . _collection . findOneAndUpdate ( conds , update , options ) ;
23202300} ;
23212301
2302+ /**
2303+ * Issues a mongodb findOneAndReplace command.
2304+ *
2305+ * Finds a matching document, replaces it according to the `replacement` arg, passing any `options`, and returns the found document (if any).
2306+ *
2307+ * #### Available options
2308+ *
2309+ * - `new`: bool - true to return the modified document rather than the original. defaults to true
2310+ * - `upsert`: bool - creates the object if it doesn't exist. defaults to false.
2311+ * - `sort`: if multiple docs are found by the conditions, sets the sort order to choose which doc to update
2312+ *
2313+ * #### Examples:
2314+ *
2315+ * await query.findOneAndReplace(conditions, replacement, options) // executes
2316+ * query.findOneAndReplace(conditions, replacement, options) // returns Query
2317+ * await query.findOneAndReplace(conditions, replacement) // executes
2318+ * query.findOneAndReplace(conditions, replacement) // returns Query
2319+ * await query.findOneAndReplace(replacement) // returns Query
2320+ * query.findOneAndReplace(replacement) // returns Query
2321+ * await query.findOneAndReplace() // executes
2322+ * query.findOneAndReplace() // returns Query
2323+ *
2324+ * @param {Object|Query } [query]
2325+ * @param {Object } [replacement]
2326+ * @param {Object } [options]
2327+ * @see mongodb http://www.mongodb.org/display/DOCS/findAndModify+Command
2328+ * @return {Query } this
2329+ * @api public
2330+ */
2331+
2332+ Query . prototype . findOneAndReplace = function ( criteria , replacement , options ) {
2333+ this . op = 'findOneAndReplace' ;
2334+ this . _validate ( ) ;
2335+
2336+ if ( Query . canMerge ( criteria ) ) {
2337+ this . merge ( criteria ) ;
2338+ }
2339+
2340+ // apply replacement
2341+ if ( replacement ) {
2342+ this . _updateDoc = replacement ;
2343+ this . options = this . options || { } ;
2344+ this . options . overwrite = true ;
2345+ }
2346+
2347+ options && this . setOptions ( options ) ;
2348+
2349+ return this ;
2350+ } ;
2351+
2352+ /**
2353+ * Executes a `findOneAndReplace` Query
2354+ * @returns the results
2355+ */
2356+ Query . prototype . _findOneAndReplace = async function ( ) {
2357+ const conds = this . _conditions ;
2358+ const replacement = this . _updateForExec ( ) ;
2359+ const options = this . _optionsForExec ( ) ;
2360+
2361+ return this . _collection . findOneAndReplace ( conds , replacement , options ) ;
2362+ } ;
2363+
23222364/**
23232365 * Issues a mongodb findOneAndDelete.
23242366 *
@@ -2573,7 +2615,7 @@ Query.prototype._fieldsForExec = function() {
25732615 */
25742616
25752617Query . prototype . _updateForExec = function ( ) {
2576- const update = utils . clone ( this . _updateDoc ) ;
2618+ const update = this . _updateDoc == null ? { } : utils . clone ( this . _updateDoc ) ;
25772619 const ops = utils . keys ( update ) ;
25782620 const ret = { } ;
25792621
0 commit comments