mingo
Version:
MongoDB query language for in-memory objects
30 lines (29 loc) • 666 B
JavaScript
import { computeValue } from "../../../core/_internal";
import { assert, isNil, isObject, isString } from "../../../util";
const $setField = (obj, expr, options) => {
const { input, field, value } = computeValue(
obj,
expr,
null,
options
);
if (isNil(input)) return null;
assert(
isObject(input),
"$setField expression 'input' must evaluate to an object"
);
assert(
isString(field),
"$setField expression 'field' must evaluate to a string"
);
const newObj = { ...input };
if (expr.value == "$$REMOVE") {
delete newObj[field];
} else {
newObj[field] = value;
}
return newObj;
};
export {
$setField
};