mingo
Version:
MongoDB query language for in-memory objects
15 lines (14 loc) • 509 B
JavaScript
import { computeValue } from "../../core/_internal";
import { Lazy } from "../../lazy";
import { $sort } from "../pipeline/sort";
import { $push } from "./push";
const $bottomN = (collection, expr, options) => {
const copts = options;
const n = computeValue(copts?.local?.groupId, expr.n, null, copts);
const result = $sort(Lazy(collection), expr.sortBy, options).collect();
const m = result.length;
return $push(m <= n ? result : result.slice(m - n), expr.output, copts);
};
export {
$bottomN
};