mingo
Version:
MongoDB query language for in-memory objects
91 lines (90 loc) • 3.35 kB
JavaScript
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var lookup_exports = {};
__export(lookup_exports, {
$lookup: () => $lookup
});
module.exports = __toCommonJS(lookup_exports);
var import_aggregator = require("../../aggregator");
var import_internal = require("../../core/_internal");
var import_util = require("../../util");
var import_internal2 = require("./_internal");
const $lookup = (collection, expr, options) => {
let joinColl = (0, import_util.isString)(expr.from) ? options?.collectionResolver(expr.from) : expr.from;
const { let: letExpr, foreignField, localField } = expr;
let lookupEq = (_) => [true, []];
const { documents, pipeline } = (0, import_internal2.filterDocumentsStage)(
expr.pipeline ?? [],
options
);
(0, import_util.assert)(
!joinColl !== !documents,
"$lookup: must specify single join input with `expr.from` or `expr.pipeline`."
);
joinColl = joinColl ?? documents;
(0, import_util.assert)(
(0, import_util.isArray)(joinColl),
"$lookup: join collection must resolve to an array."
);
if (foreignField && localField) {
const map = import_util.HashMap.init();
for (const doc of joinColl) {
(0, import_util.ensureArray)((0, import_util.resolve)(doc, foreignField) ?? null).forEach((v) => {
const xs = map.get(v);
const arr = xs ?? [];
arr.push(doc);
if (arr !== xs) map.set(v, arr);
});
}
lookupEq = (o) => {
const local = (0, import_util.resolve)(o, localField) ?? null;
if ((0, import_util.isArray)(local)) {
if (pipeline.length) {
return [local.some((v) => map.has(v)), null];
}
const result2 = Array.from(new Set((0, import_util.flatten)(local.map((v) => map.get(v)))));
return [result2.length > 0, result2];
}
const result = map.get(local) ?? null;
return [result !== null, result ?? []];
};
if (pipeline.length === 0) {
return collection.map((obj) => {
return {
...obj,
[expr.as]: lookupEq(obj).pop()
};
});
}
}
const agg = new import_aggregator.Aggregator(pipeline, options);
const opts = import_internal.ComputeOptions.init(options);
return collection.map((obj) => {
const vars = (0, import_internal.computeValue)(obj, letExpr, null, options);
opts.update({ root: null, variables: vars });
const [ok, res] = lookupEq(obj);
return {
...obj,
[expr.as]: ok ? agg.run(joinColl, opts) : res
};
});
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
$lookup
});