mingo
Version:
MongoDB query language for in-memory objects
19 lines (18 loc) • 659 B
JavaScript
import { computeValue } from "../../core/_internal";
import { compare, isInteger, isNil } from "../../util";
import { errExpectNumber, INT_OPTS } from "../expression/_internal";
import { $push } from "./push";
const $minN = (collection, expr, options) => {
const copts = options;
const m = collection.length;
const n = computeValue(copts?.local?.groupId, expr.n, null, copts);
if (!isInteger(n) || n < 1) {
return errExpectNumber(options.failOnError, "$minN 'n'", INT_OPTS.pos);
}
const arr = $push(collection, expr.input, options).filter((o) => !isNil(o));
arr.sort(compare);
return m <= n ? arr : arr.slice(0, n);
};
export {
$minN
};