Skip to content
This repository was archived by the owner on May 17, 2021. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions lib/data-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,19 @@ class DataService extends EventEmitter {
this.client.findOneAndReplace(ns, filter, replacement, options, callback);
}

/**
* Find one document and update it with the update operations.
*
* @param {String} ns - The namespace to search on.
* @param {Object} filter - The filter.
* @param {Object} update - The update operations doc.
* @param {Object} options - The query options.
* @param {Function} callback - The callback.
*/
findOneAndUpdate(ns, filter, update, options, callback) {
this.client.findOneAndUpdate(ns, filter, update, options, callback);
}

/**
* Returns explain plan for the provided filter and options on the collection.
*
Expand Down
23 changes: 23 additions & 0 deletions lib/native-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -629,6 +629,29 @@ class NativeClient extends EventEmitter {
);
}

/**
* Find one document and update it with the update operations.
*
* @param {String} ns - The namespace to search on.
* @param {Object} filter - The filter.
* @param {Object} update - The update operations doc.
* @param {Object} options - The query options.
* @param {Function} callback - The callback.
*/
findOneAndUpdate(ns, filter, update, options, callback) {
this._collection(ns).findOneAndUpdate(
filter,
update,
options,
(error, result) => {
if (error) {
return callback(this._translateMessage(error));
}
callback(null, result.value);
}
);
}

/**
* Returns explain plan for the provided filter and options on the collection.
*
Expand Down
45 changes: 45 additions & 0 deletions test/data-service.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,51 @@ describe('DataService', function() {
});
});

describe('#findOneAndUpdate', function() {
after(function(done) {
helper.deleteTestDocuments(service.client, function() {
done();
});
});

var id = new ObjectId();

it('returns the updated document', function(done) {
service.insertOne(
'data-service.test',
{
_id: id,
a: 500
},
{},
function(err) {
assert.equal(null, err);
service.findOneAndUpdate(
'data-service.test',
{
_id: id
},
{
$set: {
b: 5
}
},
{
returnOriginal: false
},
function(error, result) {
expect(error).to.equal(null);
expect(result._id.toString()).to.deep.equal(id.toString());
expect(result.b).to.equal(5);
expect(result.hasOwnProperty('a')).to.equal(true);
done();
}
);
}
);
});
});

describe('#collection', function() {
it('returns the collection details', function(done) {
service.collection('data-service.test', {}, function(err, coll) {
Expand Down