Skip to content

Commit 4ec6b40

Browse files
committed
fix(query): handle $round in $expr as array
Fix #13881
1 parent 34fea9e commit 4ec6b40

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

lib/helpers/query/cast$expr.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ const arithmeticOperatorNumber = new Set([
2929
'$floor',
3030
'$ln',
3131
'$log10',
32-
'$round',
3332
'$sqrt',
3433
'$sin',
3534
'$cos',
@@ -118,6 +117,16 @@ function _castExpression(val, schema, strictQuery) {
118117
if (val.$size) {
119118
val.$size = castNumberOperator(val.$size, schema, strictQuery);
120119
}
120+
if (val.$round) {
121+
const $round = val.$round;
122+
if (!Array.isArray($round) || $round.length !== 2) {
123+
throw new CastError('Array', $round, '$round');
124+
}
125+
val.$round = [
126+
castNumberOperator($round[0], schema, strictQuery),
127+
castNumberOperator($round[1], schema, strictQuery)
128+
];
129+
}
121130

122131
_omitUndefined(val);
123132

test/helpers/query.cast$expr.test.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,4 +108,11 @@ describe('castexpr', function() {
108108
const res = cast$expr({ $not: { $in: ['42', '$nums'] } }, testSchema);
109109
assert.deepStrictEqual(res, { $not: { $in: [42, '$nums'] } });
110110
});
111+
112+
it('casts $round (gh-13881)', function() {
113+
const testSchema = new Schema({ value: Number });
114+
115+
const res = cast$expr({ $eq: [{ $round: ['$value', '00'] }, 2] }, testSchema);
116+
assert.deepStrictEqual(res, { $eq: [{ $round: ['$value', 0] }, 2] });
117+
});
111118
});

0 commit comments

Comments
 (0)