UNPKG

mingo

Version:

MongoDB query language for in-memory objects

16 lines (15 loc) 551 B
import { evalExpr } from "../../core/_internal"; import { compare, isInteger, isNil } from "../../util"; import { errExpectInteger } from "../expression/_internal"; import { $push } from "./push"; const $minN = (coll, expr, options) => { const copts = options; const n = evalExpr(copts?.local?.groupId, expr.n, copts); if (!isInteger(n) || n < 1) { return errExpectInteger(options.failOnError, "$minN 'n'", { min: 1 }); } return $push(coll, expr.input, options).filter((o) => !isNil(o)).sort(compare).slice(0, n); }; export { $minN };