Skip to content
This repository was archived by the owner on May 17, 2021. It is now read-only.

Commit 3402949

Browse files
authored
COMPASS-4345: Add findOneAndUpdate (#240)
1 parent 44e19f9 commit 3402949

File tree

3 files changed

+81
-0
lines changed

3 files changed

+81
-0
lines changed

lib/data-service.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,19 @@ class DataService extends EventEmitter {
324324
this.client.findOneAndReplace(ns, filter, replacement, options, callback);
325325
}
326326

327+
/**
328+
* Find one document and update it with the update operations.
329+
*
330+
* @param {String} ns - The namespace to search on.
331+
* @param {Object} filter - The filter.
332+
* @param {Object} update - The update operations doc.
333+
* @param {Object} options - The query options.
334+
* @param {Function} callback - The callback.
335+
*/
336+
findOneAndUpdate(ns, filter, update, options, callback) {
337+
this.client.findOneAndUpdate(ns, filter, update, options, callback);
338+
}
339+
327340
/**
328341
* Returns explain plan for the provided filter and options on the collection.
329342
*

lib/native-client.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -629,6 +629,29 @@ class NativeClient extends EventEmitter {
629629
);
630630
}
631631

632+
/**
633+
* Find one document and update it with the update operations.
634+
*
635+
* @param {String} ns - The namespace to search on.
636+
* @param {Object} filter - The filter.
637+
* @param {Object} update - The update operations doc.
638+
* @param {Object} options - The query options.
639+
* @param {Function} callback - The callback.
640+
*/
641+
findOneAndUpdate(ns, filter, update, options, callback) {
642+
this._collection(ns).findOneAndUpdate(
643+
filter,
644+
update,
645+
options,
646+
(error, result) => {
647+
if (error) {
648+
return callback(this._translateMessage(error));
649+
}
650+
callback(null, result.value);
651+
}
652+
);
653+
}
654+
632655
/**
633656
* Returns explain plan for the provided filter and options on the collection.
634657
*

test/data-service.test.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,51 @@ describe('DataService', function() {
317317
});
318318
});
319319

320+
describe('#findOneAndUpdate', function() {
321+
after(function(done) {
322+
helper.deleteTestDocuments(service.client, function() {
323+
done();
324+
});
325+
});
326+
327+
var id = new ObjectId();
328+
329+
it('returns the updated document', function(done) {
330+
service.insertOne(
331+
'data-service.test',
332+
{
333+
_id: id,
334+
a: 500
335+
},
336+
{},
337+
function(err) {
338+
assert.equal(null, err);
339+
service.findOneAndUpdate(
340+
'data-service.test',
341+
{
342+
_id: id
343+
},
344+
{
345+
$set: {
346+
b: 5
347+
}
348+
},
349+
{
350+
returnOriginal: false
351+
},
352+
function(error, result) {
353+
expect(error).to.equal(null);
354+
expect(result._id.toString()).to.deep.equal(id.toString());
355+
expect(result.b).to.equal(5);
356+
expect(result.hasOwnProperty('a')).to.equal(true);
357+
done();
358+
}
359+
);
360+
}
361+
);
362+
});
363+
});
364+
320365
describe('#collection', function() {
321366
it('returns the collection details', function(done) {
322367
service.collection('data-service.test', {}, function(err, coll) {

0 commit comments

Comments
 (0)