mingo
Version:
MongoDB query language for in-memory objects
32 lines (31 loc) • 746 B
JavaScript
import { assert, isNumber, resolve } from "../../util";
import { applyUpdate, DEFAULT_OPTIONS, walkExpression } from "./_internal";
const $inc = (obj, expr, arrayFilters = [], options = DEFAULT_OPTIONS) => {
return walkExpression(
expr,
arrayFilters,
options,
(val, node, queries) => {
if (!node.position) {
const n = resolve(obj, node.selector);
assert(
n === void 0 || isNumber(n),
`cannot apply $inc to a value of non-numeric type`
);
}
return applyUpdate(
obj,
node,
queries,
(o, k) => {
o[k] = (o[k] ||= 0) + val;
return true;
},
{ buildGraph: true }
);
}
);
};
export {
$inc
};