Skip to content

Commit

Permalink
Merge pull request #6959 from lineus/fix-6940
Browse files Browse the repository at this point in the history
Fixes #6940 add doc.{updateOne, replaceOne}. add alias doc.delete for doc.remove
  • Loading branch information
vkarpov15 committed Sep 3, 2018
2 parents 92fcdac + eb89c89 commit 95de37d
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 1 deletion.
52 changes: 51 additions & 1 deletion lib/document.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ let DocumentArray;
let MongooseArray;
let Embedded;

var specialProperties = ['__proto__', 'constructor', 'prototype'];
const specialProperties = ['__proto__', 'constructor', 'prototype'];

/**
* Document constructor.
Expand Down Expand Up @@ -558,6 +558,56 @@ Document.prototype.update = function update() {
return this.constructor.update.apply(this.constructor, args);
};

/**
* Sends an updateOne command with this document `_id` as the query selector.
*
* ####Example:
*
* weirdCar.updateOne({$inc: {wheels:1}}, { w: 1 }, callback);
*
* ####Valid options:
*
* - same as in [Model.updateOne](#model_Model.updateOne)
*
* @see Model.updateOne #model_Model.updateOne
* @param {Object} doc
* @param {Object} options
* @param {Function} callback
* @return {Query}
* @api public
* @memberOf Document
* @instance
*/

Document.prototype.updateOne = function updateOne() {
const args = utils.args(arguments);
args.unshift({_id: this._id});
return this.constructor.updateOne.apply(this.constructor, args);
};

/**
* Sends a replaceOne command with this document `_id` as the query selector.
*
* ####Valid options:
*
* - same as in [Model.replaceOne](#model_Model.replaceOne)
*
* @see Model.replaceOne #model_Model.replaceOne
* @param {Object} doc
* @param {Object} options
* @param {Function} callback
* @return {Query}
* @api public
* @memberOf Document
* @instance
*/

Document.prototype.replaceOne = function replaceOne() {
const args = utils.args(arguments);
args.unshift({ _id: this._id });
return this.constructor.replaceOne.apply(this.constructor, args);
};

/**
* Getter/setter around the session associated with this document. Used to
* automatically set `session` if you `save()` a doc that you got from a
Expand Down
6 changes: 6 additions & 0 deletions lib/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -826,6 +826,12 @@ Model.prototype.remove = function remove(options, fn) {
});
};

/**
* Alias for remove
*/

Model.prototype.delete = Model.prototype.remove;

/*!
* ignore
*/
Expand Down
42 changes: 42 additions & 0 deletions test/document.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,48 @@ describe('document', function() {
db.close(done);
});

describe('delete', function() {
it('deletes the document', function() {
const schema = new Schema({ x: String });
const Test = db.model('gh6940', schema);
return co(function* () {
const test = new Test({ x: 'test' });
const doc = yield test.save();
yield doc.delete();
const found = yield Test.findOne({ _id: doc._id });
assert.strictEqual(found, null);
});
});
});

describe('updateOne', function() {
it('updates the document', function() {
const schema = new Schema({ x: String, y: String });
const Test = db.model('gh6940_2', schema);
return co(function* () {
const test = new Test({ x: 'test' });
const doc = yield test.save();
yield doc.updateOne({ y: 'test' });
const found = yield Test.findOne({ _id: doc._id });
assert.strictEqual(found.y, 'test');
});
});
});

describe('replaceOne', function() {
it('replaces the document', function() {
const schema = new Schema({ x: String });
const Test = db.model('gh6940_3', schema);
return co(function* () {
const test = new Test({ x: 'test' });
const doc = yield test.save();
yield doc.replaceOne({ x: 'updated' });
const found = yield Test.findOne({ _id: doc._id });
assert.strictEqual(found.x, 'updated');
});
});
});

describe('shortcut getters', function() {
it('return undefined for properties with a null/undefined parent object (gh-1326)', function(done) {
const doc = new TestDocument;
Expand Down

0 comments on commit 95de37d

Please sign in to comment.