Skip to content

Commit 78bba8f

Browse files
committed
NODE-549 findAndModify take a write concern
1 parent 0c628e9 commit 78bba8f

File tree

3 files changed

+62
-16
lines changed

3 files changed

+62
-16
lines changed

lib/collection.js

Lines changed: 32 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1981,14 +1981,16 @@ Collection.prototype.findOneAndDelete = function(filter, options, callback) {
19811981
}
19821982

19831983
var findOneAndDelete = function(self, filter, options, callback) {
1984+
// Final options
1985+
var finalOptions = shallowClone(options);
1986+
finalOptions['fields'] = options.projection;
1987+
finalOptions['remove'] = true;
1988+
// Execute find and Modify
19841989
self.findAndModify(
19851990
filter
19861991
, options.sort
19871992
, null
1988-
, {
1989-
fields: options.projection
1990-
, remove:true
1991-
}
1993+
, finalOptions
19921994
, callback
19931995
);
19941996
}
@@ -2030,16 +2032,19 @@ Collection.prototype.findOneAndReplace = function(filter, replacement, options,
20302032
}
20312033

20322034
var findOneAndReplace = function(self, filter, replacement, options, callback) {
2035+
// Final options
2036+
var finalOptions = shallowClone(options);
2037+
finalOptions['fields'] = options.projection;
2038+
finalOptions['update'] = true;
2039+
finalOptions['new'] = typeof options.returnOriginal == 'boolean' ? !options.returnOriginal : false;
2040+
finalOptions['upsert'] = typeof options.upsert == 'boolean' ? options.upsert : false;
2041+
2042+
// Execute findAndModify
20332043
self.findAndModify(
20342044
filter
20352045
, options.sort
20362046
, replacement
2037-
, {
2038-
fields: options.projection
2039-
, update: true
2040-
, new: typeof options.returnOriginal == 'boolean' ? !options.returnOriginal : false
2041-
, upsert: typeof options.upsert == 'boolean' ? options.upsert : false
2042-
}
2047+
, finalOptions
20432048
, callback
20442049
);
20452050
}
@@ -2081,16 +2086,19 @@ Collection.prototype.findOneAndUpdate = function(filter, update, options, callba
20812086
}
20822087

20832088
var findOneAndUpdate = function(self, filter, update, options, callback) {
2089+
// Final options
2090+
var finalOptions = shallowClone(options);
2091+
finalOptions['fields'] = options.projection;
2092+
finalOptions['update'] = true;
2093+
finalOptions['new'] = typeof options.returnOriginal == 'boolean' ? !options.returnOriginal : false;
2094+
finalOptions['upsert'] = typeof options.upsert == 'boolean' ? options.upsert : false;
2095+
2096+
// Execute findAndModify
20842097
self.findAndModify(
20852098
filter
20862099
, options.sort
20872100
, update
2088-
, {
2089-
fields: options.projection
2090-
, update: true
2091-
, new: typeof options.returnOriginal == 'boolean' ? !options.returnOriginal : false
2092-
, upsert: typeof options.upsert == 'boolean' ? options.upsert : false
2093-
}
2101+
, finalOptions
20942102
, callback
20952103
);
20962104
}
@@ -2178,6 +2186,14 @@ var findAndModify = function(self, query, sort, doc, options, callback) {
21782186
// No check on the documents
21792187
options.checkKeys = false;
21802188

2189+
// Get the write concern settings
2190+
var finalOptions = writeConcern(options, self.s.db, self, options);
2191+
2192+
// Decorate the findAndModify command with the write Concern
2193+
if(finalOptions.writeConcern) {
2194+
queryObject.writeConcern = finalOptions.writeConcern;
2195+
}
2196+
21812197
// Execute the command
21822198
self.s.db.command(queryObject
21832199
, options, function(err, result) {

test/functional/find_tests.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2301,3 +2301,32 @@ exports['Should simulate closed cursor'] = {
23012301
});
23022302
}
23032303
}
2304+
2305+
/**
2306+
* Find and modify should allow for a write Concern without failing
2307+
* @ignore
2308+
*/
2309+
exports['should correctly execute a findAndModifyWithAWriteConcern'] = {
2310+
metadata: { requires: { topology: ['single', 'replicaset', 'sharded', 'ssl', 'heap', 'wiredtiger'] } },
2311+
2312+
// The actual test we wish to run
2313+
test: function(configuration, test) {
2314+
var db = configuration.newDbInstance(configuration.writeConcernMax(), {poolSize:1});
2315+
db.open(function(err, db) {
2316+
db.createCollection('test_find_and_modify_a_document', function(err, collection) {
2317+
// Test return new document on change
2318+
collection.insert({'a':1, 'b':2}, configuration.writeConcernMax(), function(err, doc) {
2319+
// Let's modify the document in place
2320+
collection.findAndModify({'a':1}
2321+
, [['a', 1]], {'$set':{'b':3}}, {'new':true, j:1}, function(err, updated_doc) {
2322+
test.equal(1, updated_doc.value.a);
2323+
test.equal(3, updated_doc.value.b);
2324+
2325+
db.close();
2326+
test.done();
2327+
})
2328+
});
2329+
});
2330+
});
2331+
}
2332+
}

test/runner.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,7 @@ var testFiles =[
261261
, '/test/functional/operation_example_tests.js'
262262
, '/test/functional/crud_api_tests.js'
263263
, '/test/functional/reconnect_tests.js'
264+
, '/test/functional/find_and_modify_tests.js'
264265

265266
// Logging tests
266267
, '/test/functional/logger_tests.js'

0 commit comments

Comments
 (0)