mingo
Version:
MongoDB query language for in-memory objects
19 lines (18 loc) • 455 B
JavaScript
import { computeValue } from "../../../core/_internal";
import { assert, isDate } from "../../../util";
const $add = (obj, expr, options) => {
const args = computeValue(obj, expr, null, options);
let hasDate = false;
let sum = 0;
for (const n of args) {
if (isDate(n)) {
assert(!hasDate, "'$add' can only have one date value");
hasDate = true;
}
sum += +n;
}
return hasDate ? new Date(sum) : sum;
};
export {
$add
};