Skip to content

Commit

Permalink
fix(query): run casting on arrayFilters option
Browse files Browse the repository at this point in the history
Fix #7079
  • Loading branch information
vkarpov15 committed Jan 14, 2019
1 parent c53a52a commit 58f8ff9
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 0 deletions.
17 changes: 17 additions & 0 deletions lib/query.js
Expand Up @@ -11,6 +11,7 @@ const QueryCursor = require('./cursor/QueryCursor');
const ReadPreference = require('./driver').get().ReadPreference;
const applyWriteConcern = require('./helpers/schema/applyWriteConcern');
const cast = require('./cast');
const castArrayFilters = require('./helpers/update/castArrayFilters');
const castUpdate = require('./helpers/query/castUpdate');
const completeMany = require('./helpers/query/completeMany');
const get = require('./helpers/get');
Expand Down Expand Up @@ -1728,6 +1729,18 @@ Query.prototype._castConditions = function() {
}
};

/*!
* ignore
*/

function _castArrayFilters(query) {
try {
castArrayFilters(query);
} catch (err) {
query.error(err);
}
}

/**
* Thunk around find()
*
Expand Down Expand Up @@ -3147,6 +3160,8 @@ Query.prototype._findAndModify = function(type, callback) {
return callback(castedQuery);
}

_castArrayFilters(this);

const opts = this._optionsForExec(model);

if ('strict' in opts) {
Expand Down Expand Up @@ -3385,6 +3400,8 @@ function _updateThunk(op, callback) {

this._castConditions();

_castArrayFilters(this);

if (this.error() != null) {
callback(this.error());
return null;
Expand Down
21 changes: 21 additions & 0 deletions test/helpers/update.castArrayFilters.test.js
Expand Up @@ -41,4 +41,25 @@ describe('castArrayFilters', function() {

done();
});

it('sane error on same filter twice', function(done) {
const schema = new Schema({
comments: [{
text: String,
replies: [{ date: Date }]
}]
});
const q = new Query();
q.schema = schema;

q.updateOne({}, { $set: { 'comments.$[x].replies.$[x].date': '2018-01-01' } }, {
arrayFilters: [{ 'x.text': 123 }]
});

assert.throws(() => {
castArrayFilters(q);
}, /same array filter/);

done();
});
});
26 changes: 26 additions & 0 deletions test/model.test.js
Expand Up @@ -4947,6 +4947,32 @@ describe('Model', function() {
});
});

it('arrayFilter casting (gh-5965) (gh-7079)', function() {
return co(function*() {
const MyModel = db.model('gh5965', new Schema({
_id: Number,
grades: [Number]
}));

yield MyModel.create([
{ _id: 1, grades: [95, 92, 90] },
{ _id: 2, grades: [98, 100, 102] },
{ _id: 3, grades: [95, 110, 100] }
]);

yield MyModel.updateMany({}, { $set: { 'grades.$[element]': 100 } }, {
arrayFilters: [{
element: { $gte: '100', $lte: { valueOf: () => 109 } }
}]
});

const docs = yield MyModel.find().sort({ _id: 1 });
assert.deepEqual(docs[0].toObject().grades, [95, 92, 90]);
assert.deepEqual(docs[1].toObject().grades, [98, 100, 100]);
assert.deepEqual(docs[2].toObject().grades, [95, 110, 100]);
});
});

describe('watch()', function() {
before(function() {
if (!process.env.REPLICA_SET) {
Expand Down

0 comments on commit 58f8ff9

Please sign in to comment.