mingo
Version:
MongoDB query language for in-memory objects
33 lines (32 loc) • 954 B
JavaScript
import { ComputeOptions } from "../../core/_internal";
import { isObject } from "../../util";
import { applyUpdate, DEFAULT_OPTIONS, walkExpression } from "./_internal";
const $currentDate = (obj, expr, arrayFilters = [], options = DEFAULT_OPTIONS) => {
const copts = options instanceof ComputeOptions ? options : ComputeOptions.init(options);
assert(
Object.values(expr).every(
(v) => v === true || isObject(v) && (v.$type === "date" || v.$type === "timestamp")
),
`$currentDate: $type must be either 'date' or 'timestamp' if specified.`
);
return walkExpression(
expr,
arrayFilters,
options,
(val, node, queries) => {
return applyUpdate(
obj,
node,
queries,
(o, k) => {
o[k] = val === true || val.$type === "date" ? copts.now : copts.now.getTime();
return true;
},
{ buildGraph: true }
);
}
);
};
export {
$currentDate
};