Skip to content

Commit a657ddc

Browse files
authored
Merge pull request #15705 from Automattic/vkarpov15/gh-15701
fix(model+plugins): correctly apply shard key on deleteOne()
2 parents 52fdd0f + 1f9342f commit a657ddc

File tree

3 files changed

+50
-3
lines changed

3 files changed

+50
-3
lines changed

lib/model.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -802,8 +802,18 @@ Model.prototype.deleteOne = function deleteOne(options) {
802802
}
803803
}
804804

805-
query.pre(function queryPreDeleteOne(cb) {
806-
self.constructor._middleware.execPre('deleteOne', self, [self], cb);
805+
query.pre(async function queryPreDeleteOne() {
806+
await new Promise((resolve, reject) => {
807+
self.constructor._middleware.execPre('deleteOne', self, [self], err => {
808+
if (err) reject(err);
809+
else resolve();
810+
});
811+
});
812+
// Apply custom where conditions _after_ document deleteOne middleware for
813+
// consistency with save() - sharding plugin needs to set $where
814+
if (self.$where != null) {
815+
this.where(self.$where);
816+
}
807817
});
808818
query.pre(function callSubdocPreHooks(cb) {
809819
each(self.$getAllSubdocs(), (subdoc, cb) => {

lib/plugins/sharding.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ module.exports = function shardingPlugin(schema) {
1616
applyWhere.call(this);
1717
next();
1818
});
19-
schema.pre('remove', function shardingPluginPreRemove(next) {
19+
schema.pre('deleteOne', { document: true, query: false }, function shardingPluginPreRemove(next) {
2020
applyWhere.call(this);
2121
next();
2222
});

test/sharding.test.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
'use strict';
2+
3+
const assert = require('assert');
4+
const start = require('./common');
5+
6+
const mongoose = start.mongoose;
7+
8+
describe('plugins.sharding', function() {
9+
let db;
10+
11+
before(function() {
12+
db = start();
13+
});
14+
15+
after(async function() {
16+
await db.close();
17+
});
18+
19+
beforeEach(() => db.deleteModel(/.*/));
20+
afterEach(() => require('./util').clearTestData(db));
21+
afterEach(() => require('./util').stopRemainingOps(db));
22+
23+
it('applies shard key to deleteOne (gh-15701)', async function() {
24+
const TestModel = db.model('Test', new mongoose.Schema({ name: String, shardKey: String }));
25+
const doc = await TestModel.create({ name: 'test', shardKey: 'test1' });
26+
doc.$__.shardval = { shardKey: 'test2' };
27+
let res = await doc.deleteOne();
28+
assert.strictEqual(res.deletedCount, 0);
29+
doc.$__.shardval = { shardKey: 'test1' };
30+
res = await doc.deleteOne();
31+
assert.strictEqual(res.deletedCount, 1);
32+
33+
await TestModel.create({ name: 'test2', shardKey: 'test2' });
34+
res = await TestModel.deleteOne({ name: 'test2' });
35+
assert.strictEqual(res.deletedCount, 1);
36+
});
37+
});

0 commit comments

Comments
 (0)