mingo
Version:
MongoDB query language for in-memory objects
16 lines (15 loc) • 559 B
JavaScript
import { evalExpr } from "../../core/_internal";
import { compare, isInteger, isNil } from "../../util";
import { errExpectInteger } from "../expression/_internal";
import { $push } from "./push";
const $maxN = (coll, expr, options) => {
const copts = options;
const n = evalExpr(copts?.local?.groupId, expr.n, copts);
if (!isInteger(n) || n < 1) {
return errExpectInteger(options.failOnError, "$maxN 'n'", { min: 1 });
}
return $push(coll, expr.input, options).filter((o) => !isNil(o)).sort(compare).slice(-n).reverse();
};
export {
$maxN
};