mingo
Version:
MongoDB query language for in-memory objects
22 lines (21 loc) • 624 B
JavaScript
import { computeValue } from "../../core/_internal";
import { isInteger } from "../../util";
import { errExpectNumber, INT_OPTS } from "../expression/_internal";
import { $push } from "./push";
const $lastN = (collection, expr, options) => {
const copts = options;
const m = collection.length;
const n = computeValue(copts?.local?.groupId, expr.n, null, copts);
const foe = options.failOnError;
if (!isInteger(n) || n < 1) {
return errExpectNumber(foe, "$lastN 'n'", INT_OPTS.pos);
}
return $push(
m <= n ? collection : collection.slice(m - n),
expr.input,
options
);
};
export {
$lastN
};