mingo
Version:
MongoDB query language for in-memory objects
1 lines • 93.8 kB
JavaScript
var mingo=(()=>{var lr=Object.defineProperty;var Ro=Object.getOwnPropertyDescriptor;var Uo=Object.getOwnPropertyNames;var Fo=Object.prototype.hasOwnProperty;var xt=(e,t)=>{for(var r in t)lr(e,r,{get:t[r],enumerable:!0})},Lo=(e,t,r,n)=>{if(t&&typeof t=="object"||typeof t=="function")for(let o of Uo(t))!Fo.call(e,o)&&o!==r&&lr(e,o,{get:()=>t[o],enumerable:!(n=Ro(t,o))||n.enumerable});return e};var Bo=e=>Lo(lr({},"__esModule",{value:!0}),e);var up={};xt(up,{Aggregator:()=>Ie,Context:()=>Ot,ProcessingMode:()=>$t,Query:()=>Ee,aggregate:()=>ko,default:()=>pp,find:()=>So,update:()=>Co,updateMany:()=>_o,updateOne:()=>Mo});function H(e,t){return e*16777619^t>>>0}function On(e){if(Number.isNaN(e))return 2143289344;if(!Number.isFinite(e))return e>0?2139095040:4286578688;let t=Math.trunc(e),r=e-t,n=t|0;if(r!==0){let o=Math.floor(r*4294967296);n=H(n,o|0)}return n>>>0}function Ut(e){let t=0;for(let r=0;r<e.length;r++)t=H(t,e.charCodeAt(r));return t>>>0}function zo(e){let t=0,r=e<0n,n=r?-e:e;if(n===0n)t=H(t,0);else for(;n>0n;){let o=Number(n&0xffn);t=H(t,o),n>>=8n}return H(t,+r)>>>0}function Wo(e){let t=Ut((e.name||"")+e.toString());return t=H(t,e.length),t>>>0}function qo(e){let t=0;for(let r=0;r<e.length;r++)t=H(t,e[r]);return t>>>0}function Yo(e){let t=Ut(e.constructor.name),r=new Uint8Array(e.buffer,e.byteOffset,e.byteLength);return t=H(t,qo(r)),t>>>0}function Vo(e,t){if(t.has(e))return 13;t.add(e);let r=1;for(let n=0;n<e.length;n++)r=H(r,yr(e[n],t));return t.delete(e),r>>>0}function Ko(e,t){if(t.has(e))return 13;t.add(e);let r=Object.keys(e).sort(),n=Ut(e?.constructor?.name);for(let o of r)n=H(n,Ut(o)),n=H(n,yr(e[o],t));return t.delete(e),n>>>0}var Qo=[3735928559,305441741].map(e=>H(3,e)),Go=H(1,0),Ho=H(2,0);function yr(e,t){if(e===null)return Go;switch(typeof e){case"undefined":return Ho;case"boolean":return Qo[+e];case"number":return H(4,On(e));case"string":return H(5,Ut(e));case"bigint":return H(6,zo(e));case"function":return H(7,Wo(e));default:{if(ArrayBuffer.isView(e)&&!(e instanceof DataView))return H(12,Yo(e));if(e instanceof Date)return H(10,On(e.getTime()));if(e instanceof RegExp){let n=Ut(e.source);return H(11,H(n,Ut(e.flags)))}return Array.isArray(e)?H(8,Vo(e,t)):H(9,Ko(e,t))}}}function ut(e){return yr(e,new WeakSet)>>>0}var Ft=class extends Error{},ee=Symbol("missing"),Xo="mingo: cycle detected while processing object/array",St=e=>typeof e!="object"&&typeof e!="function"||e===null,bn=e=>St(e)||U(e)||ot(e),An={undefined:1,null:2,number:3,string:4,symbol:5,object:6,array:7,arraybuffer:8,boolean:9,date:10,regexp:11,function:12},lt=(e,t)=>e<t?-1:e>t?1:0,Jo=(e,t)=>{let r=new Uint8Array(e.buffer,e.byteOffset,e.byteLength),n=new Uint8Array(t.buffer,t.byteOffset,t.byteLength),o=Math.min(r.length,n.length);for(let i=0;i<o;i++){let s=lt(r[i],n[i]);if(s!==0)return s}return lt(r.length,n.length)};function ht(e,t,r=!1){if(e===ee&&(e=void 0),t===ee&&(t=void 0),e===t||Object.is(e,t))return 0;let n=mt(e),o=mt(t),i=0;if(n===o)switch(n){case"number":case"string":case"boolean":return lt(e,t);case"date":return lt(+e,+t);case"regexp":return(i=lt(e.source,t.source))?i:lt(e.flags,t.flags);case"arraybuffer":return Jo(e,t);case"array":{let c=e.slice().sort(ht),p=t.slice().sort(ht),u=Math.min(c.length,p.length);for(let O=0;O<u;O++)if(i=ht(c[O],p[O]))return i;return lt(c.length,p.length)}default:{if(n!=="object"){if(e?.constructor===t?.constructor&&dn(e))return lt(e.toString(),t.toString());if(i=lt(e?.constructor?.name,t?.constructor?.name))return i}let c=Object.keys(e).sort(),p=Object.keys(t).sort();if(i=ht(c,p))return i;for(let u of c)if(i=ht(e[u],t[u]))return i;return 0}}if(n=="undefined")return-1;if(o=="undefined")return 1;if(r){if(n=="array"){let c=e;if(!c.length)return-1;let p=c.slice().sort(ht);i=1;for(let u of p)if((i=Math.min(i,ht(u,t)))<0)return i;return i}if(o=="array"){let c=t;if(!c.length)return 1;let p=c.slice().sort(ht);i=-1;for(let u of p)if((i=Math.max(i,ht(e,u)))>0)return i;return i}}let s=An[n]??Number.MAX_VALUE,a=An[o]??Number.MAX_VALUE;return s!==a?lt(s,a):lt(n,o)}var M=(e,t)=>ht(e,t,!0),dn=e=>e!=null&&e.toString!==Object.prototype.toString;function G(e,t){if(e===t||Object.is(e,t))return!0;if(e===null||t===null||typeof e!=typeof t||typeof e!="object"||e.constructor!==t?.constructor)return!1;if(U(e))return U(t)&&+e==+t;if(ot(e))return ot(t)&&e.source===t.source&&e.flags===t.flags;if(l(e)&&l(t))return e.length===t.length&&e.every((s,a)=>G(s,t[a]));if(e?.constructor!==Object&&dn(e))return e?.toString()===t?.toString();let r=e,n=t,o=Object.keys(r),i=Object.keys(n);return o.length!==i.length?!1:o.every(s=>v(n,s)&&G(r[s],n[s]))}var Z=class e extends Map{#t=new Map;#e=t=>{let r=ut(t);return[(this.#t.get(r)??[]).find(o=>G(o,t)),r]};constructor(){super()}static init(){return new e}clear(){super.clear(),this.#t.clear()}delete(t){if(St(t))return super.delete(t);let[r,n]=this.#e(t);return super.delete(r)?(this.#t.set(n,this.#t.get(n).filter(o=>!G(o,r))),!0):!1}get(t){if(St(t))return super.get(t);let[r,n]=this.#e(t);return super.get(r)}has(t){if(St(t))return super.has(t);let[r,n]=this.#e(t);return super.has(r)}set(t,r){if(St(t))return super.set(t,r);let[n,o]=this.#e(t);if(super.has(n))super.set(n,r);else{super.set(t,r);let i=this.#t.get(o)||[];i.push(t),this.#t.set(o,i)}return this}get size(){return super.size}};function m(e,t){if(!e)throw new Ft(t)}function mt(e){let t=typeof e;switch(t){case"number":case"string":case"boolean":case"undefined":case"function":case"symbol":return t}return e===null?"null":l(e)?"array":U(e)?"date":ot(e)?"regexp":gn(e)?"arraybuffer":e?.constructor===Object?"object":e?.constructor?.name?.toLowerCase()??"object"}var kt=e=>typeof e=="boolean",$=e=>typeof e=="string";var x=e=>!Number.isNaN(e)&&typeof e=="number",D=Number.isInteger,l=Array.isArray,h=e=>mt(e)==="object",Ar=e=>!St(e),U=e=>e instanceof Date,ot=e=>e instanceof RegExp,br=e=>typeof e=="function",y=e=>e==null,et=(e,t=!0)=>!!e||t&&e==="",Lt=e=>y(e)||$(e)&&!e||l(e)&&e.length===0||h(e)&&Object.keys(e).length===0,at=e=>l(e)?e:[e],v=(e,...t)=>!!e&&t.every(r=>Object.prototype.hasOwnProperty.call(e,r)),gn=e=>typeof ArrayBuffer<"u"&&ArrayBuffer.isView(e),nt=(e,t)=>{if(y(e)||kt(e)||x(e)||$(e))return e;if(U(e))return new Date(e);if(ot(e))return new RegExp(e);if(gn(e)){let r=e.constructor;return new r(e)}if(t instanceof WeakSet||(t=new WeakSet),t.has(e))throw new Error(Xo);t.add(e);try{if(l(e)){let r=new Array(e.length);for(let n=0;n<e.length;n++)r[n]=nt(e[n],t);return r}if(h(e)){let r={};for(let n of Object.keys(e))r[n]=nt(e[n],t);return r}}finally{t.delete(e)}return e};function Bt(e){if(e.length===0)return[];if(e.length===1)return e[0].slice();for(let n of e)if(n.length===0)return[];let t=[Z.init(),Z.init()],r=0;for(let n of e[e.length-1])t[0].set(n,!0);for(let n=e.length-2;n>=0;n--){for(let o=0;o<e[n].length;o++){let i=e[n][o];t[r].has(i)&&t[r^1].set(i,!0)}if(t[r^1].size===0)return[];t[r].clear(),r=r^1}return Array.from(t[r].keys())}function ct(e,t=1){let r=new Array;function n(o,i){for(let s=0,a=o.length;s<a;s++)l(o[s])&&(i>0||i<0)?n(o[s],Math.max(-1,i-1)):r.push(o[s])}return n(e,t),r}function jt(e){let t=Z.init();for(let r of e)t.set(r,!0);return Array.from(t.keys())}function zt(e,t){if(e.length<1)return new Map;let r=Z.init();for(let n=0;n<e.length;n++){let o=e[n],i=t(o,n)??null,s=r.get(i);s?s.push(o):(s=[o],r.set(i,s))}return r}function te(e,t){return Ar(e)?e[t]:void 0}function Zo(e,t){if(t<1)return e;for(;t--&&e.length===1&&l(e[0]);)e=e[0];return e}function R(e,t,r){if(bn(e))return e;let n=r?.pathArray??t.split(".");if(n.length===1&&!l(e))return te(e,n[0]);if(n.length===2&&!l(e)){let a=te(e,n[0]);if(a==null)return;if(!l(a))return te(a,n[1])}let o=0;function i(a,c){let p=a;for(let u=0;u<c.length;u++){let O=c[u];if(!Or.test(O)&&l(p)){if(u===0&&o>0)break;o+=1;let b=c.slice(u);p=p.reduce((d,g)=>{let E=i(g,b);return E!==void 0&&d.push(E),d},[]);break}else p=te(p,O);if(p===void 0)break}return p}let s=i(e,n);return l(s)&&r?.unwrapArray?Zo(s,o):s}function It(e,t,r){let n=t.indexOf("."),o=n==-1?t:t.substring(0,n),i=t.substring(n+1),s=n!=-1;if(l(e)){let p=/^\d+$/.test(o),u=p&&r?.preserveIndex?e.slice():[];if(p){let O=parseInt(o),A=te(e,O);s&&(A=It(A,i,r)),r?.preserveIndex?u[O]=A:u.push(A)}else for(let O of e){let A=It(O,t,r);r?.preserveMissing?u.push(A??ee):(A!=null||r?.preserveIndex)&&u.push(A)}return u}let a=r?.preserveKeys?{...e}:{},c=te(e,o);if(s&&(c=It(c,i,r)),c!==void 0)return a[o]=c,a}function ve(e){if(l(e))for(let t=e.length-1;t>=0;t--)e[t]===ee?e.splice(t,1):ve(e[t]);else if(h(e))for(let t of Object.keys(e))v(e,t)&&ve(e[t])}var Or=/^\d+$/;function re(e,t,r,n){let o=t.split("."),i=o[0],s=o.slice(1).join(".");if(o.length===1)(h(e)||l(e)&&Or.test(i))&&r(e,i);else{n?.buildGraph&&y(e[i])&&(e[i]={});let a=e[i];if(!a)return;let c=!!(o.length>1&&Or.test(o[1]));l(a)&&n?.descendArray&&!c?a.forEach((p=>re(p,s,r,n))):re(a,s,r,n)}}function yt(e,t,r){re(e,t,(n,o)=>n[o]=r,{buildGraph:!0})}function Wt(e,t,r){re(e,t,((n,o)=>{l(n)?n.splice(parseInt(o),1):h(n)&&delete n[o]}),r)}var pt=e=>!!e&&e[0]==="$"&&/^\$[a-zA-Z0-9_]+$/.test(e);function qt(e){if(bn(e))return ot(e)?{$regex:e}:{$eq:e};if(Ar(e)){if(!Object.keys(e).some(pt))return{$eq:e};if(h(e)&&v(e,"$regex")){let t={...e};return t.$regex=new RegExp(e.$regex,e.$options),delete t.$options,t}}return e}function Yt(e,t,r=M){let n=0,o=e.length-1;for(;n<=o;){let i=Math.round(n+(o-n)/2);if(r(t,e[i])<0)o=i-1;else if(r(t,e[i])>0)n=i+1;else return i}return n}var Tt=class{constructor(){this.root={children:new Map,isTerminal:!1}}add(t){let r=t.split("."),n=this.root;for(let o of r){if(n.isTerminal)return!1;n.children.has(o)||n.children.set(o,{children:new Map,isTerminal:!1}),n=n.children.get(o)}return n.isTerminal||n.children.size?!1:n.isTerminal=!0}};var $t=(o=>(o[o.CLONE_OFF=0]="CLONE_OFF",o[o.CLONE_INPUT=1]="CLONE_INPUT",o[o.CLONE_OUTPUT=2]="CLONE_OUTPUT",o[o.CLONE_ALL=3]="CLONE_ALL",o))($t||{}),k=class e{constructor(t,r){this.options=t;this.#t=r?{...r}:{}}#t;static init(t){return t instanceof e?new e(t.options,t.#t):new e({idKey:"_id",scriptEnabled:!0,useStrictMode:!0,failOnError:!0,processingMode:0,...t,context:t?.context?Ot.from(t?.context):Ot.init()})}update(t){return Object.assign(this.#t,t,{timestamp:this.#t.timestamp,variables:{...this.#t?.variables,...t?.variables}}),this}get local(){return this.#t}get now(){let t=this.#t.timestamp??0;return t||(t=Date.now(),Object.assign(this.#t,{timestamp:t})),new Date(t)}get idKey(){return this.options.idKey}get collation(){return this.options?.collation}get processingMode(){return this.options?.processingMode}get useStrictMode(){return this.options?.useStrictMode}get scriptEnabled(){return this.options?.scriptEnabled}get failOnError(){return this.options?.failOnError}get collectionResolver(){return this.options?.collectionResolver}get jsonSchemaValidator(){return this.options?.jsonSchemaValidator}get variables(){return this.options?.variables}get context(){return this.options?.context}},Vt=(s=>(s.ACCUMULATOR="accumulator",s.EXPRESSION="expression",s.PIPELINE="pipeline",s.PROJECTION="projection",s.QUERY="query",s.WINDOW="window",s))(Vt||{}),Ot=class e{#t;constructor(){this.#t={accumulator:{},expression:{},pipeline:{},projection:{},query:{},window:{}}}static init(t={}){let r=new e;for(let n of Object.keys(t))r.#t[n]={...t[n]};return r}static from(...t){if(t.length===1)return e.init(t[0].#t);let r=new e;for(let n of t)for(let o of Object.values(Vt))r.addOps(o,n.#t[o]);return r}addOps(t,r){return this.#t[t]=Object.assign({},r,this.#t[t]),this}getOperator(t,r){return this.#t[t][r]??null}addAccumulatorOps(t){return this.addOps("accumulator",t)}addExpressionOps(t){return this.addOps("expression",t)}addQueryOps(t){return this.addOps("query",t)}addPipelineOps(t){return this.addOps("pipeline",t)}addProjectionOps(t){return this.addOps("projection",t)}addWindowOps(t){return this.addOps("window",t)}};function f(e,t,r){let n=!(r instanceof k)||y(r.local.root)?k.init(r).update({root:e}):r;return Ne(e,t,n)}var ti=new Set(["$$ROOT","$$CURRENT","$$REMOVE","$$NOW"]);function Ne(e,t,r){if($(t)&&t.length>0&&t[0]==="$"){if(t==="$$KEEP"||t==="$$PRUNE"||t==="$$DESCEND")return t;let n=r.local.root,o=t.split("."),i=t.indexOf("."),s=i===-1?t:t.substring(0,i);if(ti.has(o[0])){switch(s){case"$$ROOT":break;case"$$CURRENT":n=e;break;case"$$REMOVE":n=void 0;break;case"$$NOW":n=new Date(r.now);break}t=i===-1?"":t.substring(i+1)}else if(s.length>=2&&s[1]==="$"){n=Object.assign({},r.variables,{this:e},r?.local?.variables);let a=s.substring(2);m(v(n,a),`Use of undefined variable: ${a}`),t=t.substring(2)}else t=t.substring(1);return t===""?n:R(n,t)}if(l(t))return t.map(n=>Ne(e,n,r));if(h(t)){let n=Object.keys(t);if(pt(n[0]))return m(n.length===1,"Expression must contain a single operator."),ei(e,t[n[0]],n[0],r);let o={};for(let i=0;i<n.length;i++)o[n[i]]=Ne(e,t[n[i]],r);return o}return t}function ei(e,t,r,n){let o=n.context,i=o.getOperator("expression",r);if(i)return i(e,t,n);let s=o.getOperator("accumulator",r);return m(!!s,`accumulator '${r}' is not registered.`),l(e)||(e=Ne(e,t,n),t=null),m(l(e),`arguments must resolve to array for ${r}.`),s(e,t,n)}function S(e){return new fe(e)}function vt(...e){let t=0;return S(()=>{for(;t<e.length;){let r=e[t].next();if(!r.done)return r;t++}return{done:!0,value:void 0}})}function ri(e){return!!e&&typeof e=="object"&&typeof e?.next=="function"}function ni(e){return!!e&&(typeof e=="object"||typeof e=="function")&&typeof e[Symbol.iterator]=="function"}var fe=class{#t=[];#e=[];#r;#o=!1;constructor(t){let r;ni(t)?r=t[Symbol.iterator]():ri(t)?r=t:typeof t=="function"?r={next:t}:m(0,"mingo: iterator requires an iterable, generator or function");let n=-1;this.#r=()=>{for(;;){let{value:o,done:i}=r.next();if(i)return{done:i};let s=!0;n++;for(let a=0;a<this.#t.length;a++){let{op:c,fn:p}=this.#t[a],u=p(o,n);if(c==="map")o=u;else if(!u){s=!1;break}}if(s)return{value:o,done:i}}}}push(t,r){return this.#t.push({op:t,fn:r}),this}next(){return this.#r()}map(t){return this.push("map",t)}filter(t){return this.push("filter",t)}take(t){return m(t>=0,"value must be a non-negative integer"),this.filter(r=>t-- >0)}drop(t){return m(t>=0,"value must be a non-negative integer"),this.filter(r=>t--<=0)}transform(t){let r=this,n;return S(()=>(n||(n=t(r.collect())),n.next()))}collect(){for(;!this.#o;){let{done:t,value:r}=this.#r();t||this.#e.push(r),this.#o=t}return this.#e}each(t){for(let r=this.next();r.done!==!0;r=this.next())t(r.value)}reduce(t,r){let n=this.next();for(r===void 0&&!n.done&&(r=n.value,n=this.next());!n.done;)r=t(r,n.value),n=this.next();return r}size(){return this.collect().length}[Symbol.iterator](){return this}};var At=class{#t;#e;constructor(t,r){this.#t=t,this.#e=k.init(r)}stream(t,r){let n=S(t),o=r??this.#e,i=o.processingMode;return i&1&&n.map(s=>nt(s)),n=this.#t.map((s,a)=>{let c=Object.keys(s);m(c.length===1,`aggregation stage must have single operator, got ${c.toString()}.`);let p=c[0];m(p!=="$documents"||a==0,"$documents must be first stage in pipeline.");let u=o.context.getOperator("pipeline",p);return m(!!u,`unregistered pipeline operator ${p}.`),[u,s[p]]}).reduce((s,[a,c])=>a(s,c,o),n),i&2&&n.map(s=>nt(s)),n}run(t,r){return this.stream(t,r).collect()}};var Er={};xt(Er,{$accumulator:()=>oi,$addToSet:()=>ii,$avg:()=>si,$bottom:()=>pi,$bottomN:()=>dr,$count:()=>ui,$covariancePop:()=>mi,$covarianceSamp:()=>fi,$first:()=>gr,$firstN:()=>Ce,$last:()=>xr,$lastN:()=>_e,$max:()=>li,$maxN:()=>Me,$median:()=>hr,$mergeObjects:()=>jr,$min:()=>yi,$minN:()=>De,$percentile:()=>le,$push:()=>N,$stdDevPop:()=>Oi,$stdDevSamp:()=>Ai,$sum:()=>bi,$top:()=>di,$topN:()=>$r});var N=(e,t,r)=>{if(y(t))return e;let n=k.init(r),o=new Array(e.length);for(let i=0;i<e.length;i++){let s=e[i];o[i]=f(s,t,n.update({root:s}))??null}return o};var oi=(e,t,r)=>{m(r.scriptEnabled,"$accumulator requires 'scriptEnabled' option to be true");let n=k.init(r),o=t,i=f(n?.local?.groupId,o.initArgs||[],n.update({root:n?.local?.groupId})),s=N(e,o.accumulateArgs,n);for(let u=0;u<s.length;u++)for(let O=0;O<s[u].length;O++)s[u][O]=s[u][O]??null;let a=o.init.apply(null,i),c=o.accumulate,p=a;for(let u=0;u<s.length;u++)p=c.apply(null,[p,...s[u]]);return o.finalize&&(p=o.finalize.call(null,p)),p};var ii=(e,t,r)=>jt(N(e,t,r));var si=(e,t,r)=>{let n=N(e,t,r).filter(x);return n.length===0?null:n.reduce((i,s)=>i+s,0)/n.length};function it(e,t,r){m(h(t)&&Object.keys(t).length>0,"$sort specification is invalid");let n=M,o=r.collation;return h(o)&&$(o.locale)&&(n=ci(o)),e.transform(i=>{let s=Object.keys(t);for(let a of s.reverse()){let c=zt(i,A=>R(A,a)),p=Array.from(c.keys()),u=!1;if(n===M){let A=!0,b=!0;for(let d of p)if(A&&=$(d),b&&=x(d),!A&&!b)break;if(u=A||b,A)p.sort();else if(b){let d=new Float64Array(p).sort();for(let g=0;g<d.length;g++)p[g]=d[g]}}u||p.sort(n),t[a]===-1&&p.reverse();let O=0;for(let A of p)for(let b of c.get(A))i[O++]=b;m(O==i.length,"bug: counter must match collection size.")}return S(i)})}var ai={1:"base",2:"accent",3:"variant"};function ci(e){let t={sensitivity:ai[e.strength||3],caseFirst:e.caseFirst==="off"?"false":e.caseFirst,numeric:e.numericOrdering||!1,ignorePunctuation:e.alternate==="shifted"};e.caseLevel===!0&&(t.sensitivity==="base"&&(t.sensitivity="case"),t.sensitivity==="accent"&&(t.sensitivity="variant"));let r=new Intl.Collator(e.locale,t);return(n,o)=>$(n)&&$(o)?r.compare(n,o):M(n,o)}var dr=(e,t,r)=>{let n=r,o=t,i=f(n?.local?.groupId,o.n,n),s=it(S(e),o.sortBy,r).collect(),a=s.length;return N(a<=i?s:s.slice(a-i),o.output,n)};var pi=(e,t,r)=>dr(e,{...t,n:1},r);var ui=(e,t,r)=>e.length;function we(e,t=!0){let r=e.reduce((i,s)=>i+s,0),n=Math.max(e.length,1),o=r/n;return Math.sqrt(e.reduce((i,s)=>i+Math.pow(s-o,2),0)/(n-Number(t)))}function Se(e,t=!0){if(e.length<2)return t?null:0;let r=0,n=0;for(let[i,s]of e)r+=i,n+=s;r/=e.length,n/=e.length;let o=0;for(let[i,s]of e)o+=(i-r)*(s-n);return o/(e.length-Number(t))}var mi=(e,t,r)=>Se(N(e,t,r),!1);var fi=(e,t,r)=>Se(N(e,t,r),!0);var gr=(e,t,r)=>{let n=e[0],o=k.init(r).update({root:n});return f(n,t,o)??null};var z={int:{int:!0},pos:{min:1,int:!0},index:{min:0,int:!0},nzero:{min:0,max:0,int:!0}},xn={int:{type:"integers"},obj:{type:"objects"}};function W(e,t){return m(!e,t),null}function ke(e,t){let r=`${t} expression must resolve to object`;return m(!e,r),null}function X(e,t){let r=`${t} expression must resolve to string`;return m(!e,r),null}function I(e,t,r){let n=r?.int?"integer":"number",o=r?.min??-1/0,i=r?.max??1/0,s;return o===0&&i===0?s=`${t} expression must resolve to non-zero ${n}`:o===0&&i===1/0?s=`${t} expression must resolve to non-negative ${n}`:o!==-1/0&&i!==1/0?s=`${t} expression must resolve to ${n} in range [${o}, ${i}]`:o>0?s=`${t} expression must resolve to positive ${n}`:s=`${t} expression must resolve to ${n}`,m(!e,s),null}function j(e,t,r){let n="array";!y(r?.size)&&r?.size>=0&&(n=r.size===0?"non-zero array":`array(${r.size})`),r?.type&&(n=`array of ${r.type}`);let o=`${t} expression must resolve to ${n}`;return m(!e,o),null}var Ce=(e,t,r)=>{let n=r.failOnError,o=r,i=e.length,s=f(o?.local?.groupId,t.n,o);return!D(s)||s<1?I(n,"$firstN 'n'",z.pos):N(i<=s?e:e.slice(0,s),t.input,r)};var xr=(e,t,r)=>{let n=e[e.length-1],o=k.init(r).update({root:n});return f(n,t,o)??null};var _e=(e,t,r)=>{let n=r,o=e.length,i=f(n?.local?.groupId,t.n,n),s=r.failOnError;return!D(i)||i<1?I(s,"$lastN 'n'",z.pos):N(o<=i?e:e.slice(o-i),t.input,r)};var li=(e,t,r)=>{let n=N(e,t,r).filter(o=>!y(o));return n.length?n.reduce((o,i)=>M(o,i)>=0?o:i):null};var Me=(e,t,r)=>{let n=r,o=e.length,i=f(n?.local?.groupId,t.n,n);if(!D(i)||i<1)return I(r.failOnError,"$maxN 'n'",z.pos);let s=N(e,t.input,r).filter(a=>!y(a));return s.sort((a,c)=>-1*M(a,c)),o<=i?s:s.slice(0,i)};var le=(e,t,r)=>{m(h(t)&&v(t,"input","p")&&l(t.p),"$percentile expects object { input, p }");let n=N(e,t.input,r).filter(x).sort(),o=N(t.p,"$$CURRENT",r),i=t.method||"approximate";for(let s of o)if(!x(s)||s<0||s>1)return W(r.failOnError,"$percentile 'p' must resolve to array of numbers between [0.0, 1.0]");return o.map(s=>{let a=s*(n.length-1)+1,c=Math.floor(a),p=a===c?n[a-1]:n[c-1]+a%1*(n[c]-n[c-1]);switch(i){case"exact":return p;case"approximate":{let u=Yt(n,p);return u/n.length>=s?n[Math.max(u-1,0)]:n[u]}}})};var hr=(e,t,r)=>le(e,{...t,p:[.5]},r)?.pop();var jr=(e,t,r)=>{let n={};for(let o of e)if(!y(o))for(let i of Object.keys(o))o[i]!==void 0&&(n[i]=o[i]);return n};var yi=(e,t,r)=>{let n=N(e,t,r).filter(o=>!y(o));return n.length?n.reduce((o,i)=>M(o,i)<=0?o:i):null};var De=(e,t,r)=>{let n=r,o=e.length,i=f(n?.local?.groupId,t.n,n);if(!D(i)||i<1)return I(r.failOnError,"$minN 'n'",z.pos);let s=N(e,t.input,r).filter(a=>!y(a));return s.sort(M),o<=i?s:s.slice(0,i)};var Oi=(e,t,r)=>we(N(e,t,r).filter(x),!1);var Ai=(e,t,r)=>we(N(e,t,r).filter(x),!0);var bi=(e,t,r)=>x(t)?e.length*t:N(e,t,r).filter(x).reduce((o,i)=>o+i,0);var $r=(e,t,r)=>{let n=r,{n:o,sortBy:i}=f(n?.local?.groupId,t,n),s=it(S(e),i,r).take(o).collect();return N(s,t.output,n)};var di=(e,t,r)=>$r(e,{...t,n:1},r);var Vr={};xt(Vr,{$abs:()=>gi,$acos:()=>Da,$acosh:()=>Pa,$add:()=>xi,$allElementsTrue:()=>oa,$and:()=>$n,$anyElementTrue:()=>ia,$arrayElemAt:()=>Pi,$arrayToObject:()=>Ri,$asin:()=>Ra,$asinh:()=>Ua,$atan:()=>Fa,$atan2:()=>La,$atanh:()=>Ba,$bitAnd:()=>rs,$bitNot:()=>ns,$bitOr:()=>os,$bitXor:()=>is,$ceil:()=>hi,$cmp:()=>Tn,$concat:()=>ma,$concatArrays:()=>Ui,$cond:()=>ls,$convert:()=>va,$cos:()=>za,$cosh:()=>Wa,$dateAdd:()=>de,$dateDiff:()=>hs,$dateFromParts:()=>Es,$dateFromString:()=>ws,$dateSubtract:()=>Ss,$dateToParts:()=>ks,$dateToString:()=>_s,$dateTrunc:()=>Ps,$dayOfMonth:()=>Rs,$dayOfWeek:()=>Us,$dayOfYear:()=>Fs,$degreesToRadians:()=>Ya,$divide:()=>ji,$eq:()=>Fn,$exp:()=>$i,$filter:()=>Fi,$first:()=>Li,$firstN:()=>Bi,$floor:()=>Ei,$function:()=>Mr,$getField:()=>Js,$gt:()=>Ln,$gte:()=>Bn,$hour:()=>Ls,$ifNull:()=>_r,$in:()=>zi,$indexOfArray:()=>Wi,$indexOfBytes:()=>fa,$isArray:()=>qi,$isNumber:()=>Na,$isoDayOfWeek:()=>Bs,$isoWeek:()=>zs,$isoWeekYear:()=>Ws,$last:()=>Yi,$lastN:()=>Vi,$let:()=>Ja,$literal:()=>Hs,$ln:()=>Ii,$log:()=>Ti,$log10:()=>vi,$lt:()=>zn,$lte:()=>Wn,$ltrim:()=>ya,$map:()=>Ki,$maxN:()=>Qi,$median:()=>Xs,$mergeObjects:()=>Ur,$millisecond:()=>qs,$minN:()=>Gi,$minute:()=>Ys,$mod:()=>Ni,$month:()=>Vs,$multiply:()=>wi,$ne:()=>qn,$not:()=>En,$objectToArray:()=>ea,$or:()=>In,$percentile:()=>na,$pow:()=>Si,$radiansToDegrees:()=>Ka,$rand:()=>Zs,$range:()=>Hi,$reduce:()=>Xi,$regexFind:()=>Oa,$regexFindAll:()=>Aa,$regexMatch:()=>ba,$replaceAll:()=>da,$replaceOne:()=>ga,$reverseArray:()=>Ji,$round:()=>ki,$rtrim:()=>xa,$sampleRate:()=>ta,$second:()=>Ks,$setDifference:()=>sa,$setEquals:()=>aa,$setField:()=>Fr,$setIntersection:()=>ca,$setIsSubset:()=>pa,$setUnion:()=>ua,$sigmoid:()=>Ci,$sin:()=>Qa,$sinh:()=>Ga,$size:()=>Zi,$slice:()=>Tr,$sortArray:()=>ts,$split:()=>ha,$sqrt:()=>_i,$strLenBytes:()=>$a,$strLenCP:()=>Ea,$strcasecmp:()=>ja,$substr:()=>Ia,$substrBytes:()=>Br,$substrCP:()=>Ta,$subtract:()=>Mi,$switch:()=>ys,$tan:()=>Ha,$tanh:()=>Xa,$toBool:()=>zr,$toDate:()=>Wr,$toDecimal:()=>wa,$toDouble:()=>xe,$toHashedIndexKey:()=>Ma,$toInt:()=>qr,$toLong:()=>Yr,$toLower:()=>ka,$toString:()=>Ht,$toUpper:()=>Ca,$trim:()=>_a,$trunc:()=>Di,$type:()=>Sa,$unsetField:()=>ra,$week:()=>Qs,$year:()=>Gs,$zip:()=>es});var gi=(e,t,r)=>{let n=f(e,t,r);return y(n)?null:typeof n!="number"?I(r.failOnError,"$abs"):Math.abs(n)};var hn="$add expression must resolve to array of numbers.",xi=(e,t,r)=>{let n=f(e,t,r),o=r.failOnError,i=!1,s=0;if(!l(n))return W(o,hn);for(let a of n){if(y(a))return null;if(typeof a=="number")s+=a;else if(U(a)){if(i)return W(o,"$add must only have one date");i=!0,s+=+a}else return W(o,hn)}return i?new Date(s):s};var hi=(e,t,r)=>{let n=f(e,t,r);return y(n)?null:typeof n!="number"?I(r.failOnError,"$ceil"):Math.ceil(n)};var ji=(e,t,r)=>{m(l(t),"$divide expects array(2)");let n=f(e,t,r),o=r.failOnError,i=!0;for(let s of n){if(y(s))return null;i&&=x(s)}return i?n[1]===0?W(o,"$divide cannot divide by zero"):n[0]/n[1]:j(o,"$divide",{size:2,type:"number"})};var $i=(e,t,r)=>{let n=f(e,t,r);return y(n)?null:typeof n!="number"?I(r.failOnError,"$exp"):Math.exp(n)};var Ei=(e,t,r)=>{let n=f(e,t,r);return y(n)?null:typeof n!="number"?I(r.failOnError,"$floor"):Math.floor(n)};var Ii=(e,t,r)=>{let n=f(e,t,r);return y(n)?null:typeof n!="number"?I(r.failOnError,"$ln"):Math.log(n)};var Ti=(e,t,r)=>{let n=f(e,t,r);if(l(n)&&n.length==2){let o=!0;for(let i of n){if(y(i))return null;o&&=typeof i=="number"}if(o)return Math.log10(n[0])/Math.log10(n[1])}return j(r.failOnError,"$log",{size:2,type:"number"})};var vi=(e,t,r)=>{let n=f(e,t,r);return y(n)?null:typeof n=="number"?Math.log10(n):I(r.failOnError,"$log10")};var Ni=(e,t,r)=>{let n=f(e,t,r),o=!l(n)||n.length!=2;return o||=!n.every(i=>typeof i=="number"),o?j(r.failOnError,"$mod",{size:2,type:"number"}):n[0]%n[1]};var wi=(e,t,r)=>{m(l(t),"$multiply expects array");let n=f(e,t,r),o=r.failOnError;if(n.some(y))return null;let i=1;for(let s of n){if(!x(s))return j(o,"$multiply",{type:"number"});i*=s}return i};var Si=(e,t,r)=>{m(l(t)&&t.length===2,"$pow expects array(2)");let n=f(e,t,r),o=r.failOnError,i=!0;for(let s of n){if(y(s))return null;i&&=x(s)}return i?(n[0]===0&&n[1]<0&&W(o,"$pow cannot raise 0 to a negative exponent"),Math.pow(n[0],n[1])):j(o,"$pow",{size:2,type:"number"})};function Pe(e,t,r){let{name:n,roundOff:o,failOnError:i}=r;if(y(e))return null;if(Number.isNaN(e)||Math.abs(e)===1/0)return e;if(!x(e))return I(i,`${n} arg1 <number>`);if(!D(t)||t<-20||t>100)return I(i,`${n} arg2 <precision>`,{min:-20,max:100,int:!0});let s=Math.abs(e)===e?1:-1;e=Math.abs(e);let a=Math.trunc(e),c=parseFloat((e-a).toFixed(Math.abs(t)+1));if(t===0){let p=Math.trunc(10*c);o&&((a&1)===1&&p>=5||p>5)&&a++}else if(t>0){let p=Math.pow(10,t),u=Math.trunc(c*p),O=Math.trunc(c*p*10)%10;o&&O>5&&(u+=1),a=(a*p+u)/p}else if(t<0){let p=Math.pow(10,-1*t),u=a%p;if(a=Math.max(0,a-u),o&&s===-1){for(;u>10;)u-=u/10;a>0&&u>=5&&(a+=p)}}return a*s}var ki=(e,t,r)=>{m(l(t),"$round expects array(2)");let[n,o]=f(e,t,r);return Pe(n,o??0,{name:"$round",roundOff:!0,failOnError:r.failOnError})};var jn=1e10,Ci=(e,t,r)=>{if(y(t))return null;let n=f(e,t,r),{input:o,onNull:i}=h(n)?n:{input:n};if(y(o))return x(i)?i:null;if(x(o)){let s=1/(1+Math.exp(-o));return Math.round(s*jn)/jn}return I(r.failOnError,"$sigmoid")};var _i=(e,t,r)=>{let n=f(e,t,r),o=!r.failOnError;return y(n)?null:typeof n!="number"||n<0?(m(o,"$sqrt expression must resolve to non-negative number."),null):Math.sqrt(n)};var Mi=(e,t,r)=>{m(l(t),"$subtract expects array(2)");let n=f(e,t,r);if(n.some(y))return null;let o=r.failOnError,[i,s]=n;return U(i)&&x(s)?new Date(+i-Math.round(s)):U(i)&&U(s)?+i-+s:n.every(a=>typeof a=="number")?i-s:x(i)&&U(s)?W(o,"$subtract cannot subtract date from number"):j(o,"$subtract",{size:2,type:"number|date"})};var Di=(e,t,r)=>{m(l(t),"$trunc expects array(2)");let[n,o]=f(e,t,r);return Pe(n,o??0,{name:"$trunc",roundOff:!1,failOnError:r.failOnError})};var Ir="$arrayElemAt",Pi=(e,t,r)=>{m(l(t)&&t.length===2,`${Ir} expects array(2)`);let n=f(e,t,r);if(n.some(y))return null;let o=r.failOnError,[i,s]=n;if(!l(i))return j(o,`${Ir} arg1 <array>`);if(!D(s))return I(o,`${Ir} arg2 <index>`,z.int);if(s<0&&Math.abs(s)<=i.length)return i[(s+i.length)%i.length];if(s>=0&&s<i.length)return i[s]};var Re={generic:{type:"key-value pairs"},array:{type:"[k,v]"},object:{type:"{k,v}"}},Ri=(e,t,r)=>{let n=r.failOnError,o=f(e,t,r);if(y(o))return null;if(!l(o))return j(n,"$arrayToObject",Re.generic);let i=0,s={};for(let a of o)if(l(a)){let c=ct(a);if(i||(i=1),i!==1)return j(n,"$arrayToObject",Re.object);let[p,u]=c;s[p]=u}else if(h(a)&&v(a,"k","v")){if(i||(i=2),i!==2)return j(n,"$arrayToObject",Re.array);let{k:c,v:p}=a;s[c]=p}else return j(n,"$arrayToObject",Re.generic);return s};var Ui=(e,t,r)=>{let n=f(e,t,r),o=r.failOnError;if(y(n))return null;if(!l(n))return j(o,"$concatArrays");let i=0;for(let c of n){if(y(c))return null;if(!l(c))return j(o,"$concatArrays");i+=c.length}let s=new Array(i),a=0;for(let c of n)for(let p of c)s[a++]=p;return s};var Fi=(e,t,r)=>{m(h(t)&&v(t,"input","cond"),"$filter expects object { input, as, cond, limit }");let n=f(e,t.input,r),o=r.failOnError;if(y(n))return null;if(!l(n))return j(o,"$filter 'input'");let i=t.limit??Math.max(n.length,1);if(!D(i)||i<1)return I(o,"$filter 'limit'",{min:1,int:!0});if(n.length===0)return[];let s=k.init(r),a=t?.as||"this",c={variables:{}},p=[];for(let u=0,O=0;u<n.length&&O<i;u++){c.variables[a]=n[u];let A=f(e,t.cond,s.update(c));et(A,r.useStrictMode)&&(p.push(n[u]),O++)}return p};var Li=(e,t,r)=>{if(l(e))return gr(e,t,r);let n=f(e,t,r);return y(n)?null:l(n)?ct(n)[0]:j(r.failOnError,"$first")};var Bi=(e,t,r)=>{if(m(h(t)&&v(t,"input","n"),"$firstN expects object { input, n }"),l(e))return Ce(e,t,r);let{input:n,n:o}=f(e,t,r);return y(n)?null:l(n)?Ce(n,{n:o,input:"$$this"},r):j(r.failOnError,"$firstN 'input'")};var zi=(e,t,r)=>{m(l(t)&&t.length===2,"$in expects array(2)");let n=f(e,t,r),[o,i]=n;if(!l(i))return W(r.failOnError,"$in arg2 <array>");for(let s of i)if(G(s,o))return!0;return!1};var Ue="$indexOfArray",Wi=(e,t,r)=>{m(l(t)&&t.length>1&&t.length<5,`${Ue} expects array(4)`);let n=f(e,t,r),o=r.failOnError,i=n[0];if(y(i))return null;if(!l(i))return j(o,`${Ue} arg1 <array>`);let s=n[1],a=n[2]??0,c=n[3]??i.length;return!D(a)||a<0?I(o,`${Ue} arg3 <start>`,z.pos):!D(c)||c<0?I(o,`${Ue} arg4 <end>`,z.pos):a>c?-1:(a>0||c<i.length?i.slice(a,c):i).findIndex(u=>G(u,s))+a};var qi=(e,t,r)=>{let n=t;return l(t)&&(m(t.length===1,"$isArray expects array(1)"),n=t[0]),l(f(e,n,r))};var Yi=(e,t,r)=>{if(l(e))return xr(e,t,r);let n=f(e,t,r);return y(n)?null:!l(n)||n.length===0?j(r.failOnError,"$last",{size:0}):ct(n)[n.length-1]};var Vi=(e,t,r)=>{if(m(h(t)&&v(t,"input","n"),"$lastN expects object { input, n }"),l(e))return _e(e,t,r);let{input:n,n:o}=f(e,t,r);return y(n)?null:l(n)?_e(n,{n:o,input:"$$this"},r):j(r.failOnError,"$lastN 'input'")};var Ki=(e,t,r)=>{m(h(t)&&v(t,"input","in"),"$map expects object { input, as, in }");let n=f(e,t.input,r),o=r.failOnError;if(y(n))return null;if(!l(n))return j(o,"$map 'input'");if(!y(t.as)&&!$(t.as))return X(o,"$map 'as'");let i=k.init(r),s=t.as||"this",a={variables:{}};return n.map(c=>(a.variables[s]=c,f(e,t.in,i.update(a))))};var Qi=(e,t,r)=>{if(m(h(t)&&v(t,"input","n"),"$maxN expects object { input, n }"),l(e))return Me(e,t,r);let{input:n,n:o}=f(e,t,r);return y(n)?null:l(n)?Me(n,{n:o,input:"$$this"},r):j(r.failOnError,"$maxN 'input'")};var Gi=(e,t,r)=>{if(m(h(t)&&v(t,"input","n"),"$minN expects object { input, n }"),l(e))return De(e,t,r);let{input:n,n:o}=f(e,t,r);return y(n)?null:l(n)?De(n,{n:o,input:"$$this"},r):j(r.failOnError,"$minN 'input'")};var Hi=(e,t,r)=>{m(l(t)&&t.length>1&&t.length<4,"$range expects array(3)");let[n,o,i]=f(e,t,r),s=r.failOnError,a=i??1;if(!D(n))return I(s,"$range arg1 <start>",z.int);if(!D(o))return I(s,"$range arg2 <end>",z.int);if(!D(a)||a===0)return I(s,"$range arg3 <step>",z.nzero);let c=new Array,p=n;for(;p<o&&a>0||p>o&&a<0;)c.push(p),p+=a;return c};function Xi(e,t,r){m(h(t)&&v(t,"input","initialValue","in"),"$reduce expects object { input, initialValue, in }");let n=f(e,t.input,r),o=f(e,t.initialValue,r),i=t.in;if(y(n))return null;if(!l(n))return j(r.failOnError,"$reduce 'input'");let s=k.init(r),a={variables:{value:null}},c=o;for(let p=0;p<n.length;p++)a.variables.value=c,c=f(n[p],i,s.update(a));return c}var Ji=(e,t,r)=>{let n=f(e,t,r);return y(n)?null:l(n)?n.slice().reverse():j(r.failOnError,"$reverseArray")};var Zi=(e,t,r)=>{let n=f(e,t,r);return y(n)?null:l(n)?n.length:I(r.failOnError,"$size")};var Tr=(e,t,r)=>{m(l(t)&&t.length>1&&t.length<4,"$slice expects array(3)");let n=r.failOnError,o=f(e,t,r),i=o[0],s=o[1],a=o[2];if(!l(i))return j(n,"$slice arg1 <array>");if(!D(s))return I(n,"$slice arg2 <n>",z.int);if(!y(a)&&!D(a))return I(n,"$slice arg3 <n>",z.int);if(y(a))s<0?s=Math.max(0,i.length+s):(a=s,s=0);else{if(s<0&&(s=Math.max(0,i.length+s)),a<1)return I(n,"$slice arg3 <n>",z.pos);a+=s}return i.slice(s,a)};var ts=(e,t,r)=>{m(h(t)&&"input"in t&&"sortBy"in t,"$sortArray expects object { input, sortBy }");let{input:n,sortBy:o}=f(e,t,r);if(y(n))return null;if(!l(n))return j(r.failOnError,"$sortArray 'input'");if(h(o))return it(S(n),o,r).collect();let i=n.slice().sort(M);return o===-1&&i.reverse(),i};var es=(e,t,r)=>{m(h(t)&&v(t,"inputs"),"$zip received invalid arguments");let n=f(e,t.inputs,r),o=f(e,t.defaults,r)??[],i=t.useLongestLength??!1,s=r.failOnError;if(y(n))return null;if(!l(n))return j(s,"$zip 'inputs'");let a=0;for(let u of n){if(y(u))return null;l(u)||a++}if(a)return j(s,"$zip elements of 'inputs'");kt(i)||W(s,"$zip 'useLongestLength' must be boolean"),l(o)&&o.length>0&&m(i&&o.length===n.length,"$zip 'useLongestLength' must be set to true to use 'defaults'");let c=0;for(let u of n)c=i?Math.max(c,u.length):Math.min(c||u.length,u.length);let p=[];for(let u=0;u<c;u++){let O=n.map((A,b)=>y(A[u])?o[b]??null:A[u]);p.push(O)}return p};function ne(e,t,r,n,o){m(l(t),`${n} expects array as argument`);let i=f(e,t,r),s=!0;for(let a of i){if(y(a))return null;s&&=D(a)}return s?o(i):W(r.failOnError,`${n} array elements must resolve to integers`)}var rs=(e,t,r)=>ne(e,t,r,"$bitAnd",n=>n.reduce((o,i)=>o&i,-1));var ns=(e,t,r)=>{let n=f(e,t,r);return y(n)?null:D(n)?~n:I(r.failOnError,"$bitNot",z.int)};var os=(e,t,r)=>ne(e,t,r,"$bitOr",n=>n.reduce((o,i)=>o|i,0));var is=(e,t,r)=>ne(e,t,r,"$bitXor",n=>n.reduce((o,i)=>o^i,0));var ye={};xt(ye,{$and:()=>$n,$not:()=>En,$or:()=>In});var $n=(e,t,r)=>{m(l(t),"$and expects array");let n=r.useStrictMode;return t.every(o=>et(f(e,o,r),n))};var En=(e,t,r)=>{let n=at(t);return n.length===0?!1:n.length>1?j(r.failOnError,"$not",{size:1}):!f(e,n[0],r)};var In=(e,t,r)=>{m(l(t),"$or expects array of expressions");let n=r.useStrictMode;for(let o of t)if(et(f(e,o,r),n))return!0;return!1};var Ae={};xt(Ae,{$cmp:()=>Tn,$eq:()=>Fn,$gt:()=>Ln,$gte:()=>Bn,$lt:()=>zn,$lte:()=>Wn,$ne:()=>qn});var Tn=(e,t,r)=>{m(l(t)&&t.length===2,"$cmp expects array(2)");let[n,o]=f(e,t,r);return M(n,o)};function vr(e,t,r){return e.take(t)}function Nr(e,t,r){let n=f(null,t,r);m(l(n),"$documents expression must resolve to an array.");let o=S(n);return r.processingMode&3?o.map(s=>nt(s)):o}var ss=S([]);function Fe(e,t){if(!e)return{};let r=e[0]?.$documents;return r?{documents:Nr(ss,r,t).collect(),pipeline:e.slice(1)}:{pipeline:e}}function wr(e,t,r=!0){let n={exclusions:[],inclusions:[],positional:0},o=Object.keys(e);m(o.length,"Invalid empty sub-projection");let i=t?.idKey,s=!1;for(let a of o){if(a.startsWith("$"))return m(!r&&o.length===1,`FieldPath field names may not start with '$', given '${a}'.`),n;a.endsWith(".$")&&n.positional++;let c=e[a];if(c===!1||x(c)&&c===0)a===i?s=!0:n.exclusions.push(a);else if(!h(c))n.inclusions.push(a);else{let p=wr(c,t,!1);if(!p.inclusions.length&&!p.exclusions.length)n.inclusions.includes(a)||n.inclusions.push(a);else{for(let u of p.exclusions)n.exclusions.push(`${a}.${u}`);for(let u of p.inclusions)n.inclusions.push(`${a}.${u}`)}n.positional+=p.positional}m(!(n.exclusions.length&&n.inclusions.length),"Cannot do exclusion and inclusion in projection."),m(n.positional<=1,"Cannot specify more than one positional projection.")}if(s&&n.exclusions.push(i),r){let a=new Tt;for(let c of n.exclusions)m(a.add(c),`Path collision at ${c}.`);for(let c of n.inclusions)m(a.add(c),`Path collision at ${c}.`);n.exclusions.sort(),n.inclusions.sort()}return n}function Et(e,t,r){$(t)&&m(r.collectionResolver,`${e} requires 'collectionResolver' option to resolve named collection`);let n=$(t)?r.collectionResolver(t):t;return m(l(n),`${e} could not resolve input collection`),n}var Sr="$project";function Kt(e,t,r){if(Lt(t))return e;let n=wr(t,r),o=as(t,k.init(r),n);return e.map(o)}function as(e,t,r){let n=t.idKey,{exclusions:o,inclusions:i}=r,s={},a={preserveMissing:!0};for(let A of o)s[A]=(b,d)=>{Wt(b,A,{descendArray:!0})};for(let A of i){let b=R(e,A)??e[A];if(A.endsWith(".$")&&b===1){let d=t?.local?.condition??{};m(d,`${Sr}: positional operator '.$' requires array condition.`);let g=A.slice(0,-2);s[g]=us(g,d,t);continue}if(l(b))s[A]=(d,g)=>{t.update({root:g});let E=b.map(T=>f(g,T,t)??null);yt(d,A,E)};else if(x(b)||b===!0)s[A]=(d,g)=>{t.update({root:g});let E=It(g,A,a);Nn(d,E)};else if(!h(b))s[A]=(d,g)=>{t.update({root:g});let E=f(g,b,t);yt(d,A,E)};else{let d=Object.keys(b);m(d.length===1&&pt(d[0]),"Not a valid operator");let g=d[0],E=b[g],T=t.context.getOperator("projection",g);!T||g==="$slice"&&!at(E).every(x)?s[A]=(C,F)=>{t.update({root:F});let st=f(F,b,t);yt(C,A,st)}:s[A]=(C,F)=>{t.update({root:F});let st=T(F,E,A,t);yt(C,A,st)}}}let c=o.length===1&&o.includes(n),p=!o.includes(n),u=!i.length,O=u&&c||u&&o.length&&!c;return A=>{let b={};O&&Object.assign(b,A);for(let d in s)s[d](b,A);return u||ve(b),p&&!v(b,n)&&v(A,n)&&(b[n]=R(A,n)),b}}var vn=(e,t,r,n)=>{let o=R(e,t);l(o)||(o=R(o,r)),m(l(o),`${Sr}: field '${t}' must resolve to array`);let i=[];for(let s=0;s<o.length;s++)n({[r]:[o[s]]})&&i.push(s);return i},cs=e=>(t=>!e(t)),ps={$and:1,$or:1,$nor:1};function us(e,t,r){let n=Object.entries(t).slice(),o={$and:[],$or:[]};for(let c=0;c<n.length;c++){let[p,u,O]=n[c];if(p===e||p.startsWith(e+".")){let A=qt(u),b=Object.keys(A)[0],d=A[b],g=r.context.getOperator("query",b),E=p.substring(p.lastIndexOf(".")+1),T=g(E,d,r);!O||O==="$and"?o.$and.push([p,T,E]):O==="$nor"?o.$and.push([p,cs(T),E]):O==="$or"&&o.$or.push([p,T,E])}else if(pt(p)){m(!!ps[p],`${Sr}: '${p}' is not allowed in this context`);for(let A of u)for(let b of Object.keys(A))n.push([b,A[b],p])}}let i=e.lastIndexOf("."),s=e.substring(0,i)||e,a=e.substring(i+1);return(c,p)=>{let u=[];for(let[b,d,g]of o.$and)u.push(vn(p,b,g,d));if(o.$or.length){let b=[];for(let[d,g,E]of o.$or)b.push(...vn(p,d,E,g));u.push(jt(b))}let O=Bt(u).sort()[0],A=R(p,e)[O];s!=a&&!h(A)&&(A={[a]:A}),yt(c,s,[A])}}function Nn(e,t){if(e===ee||y(e))return t;if(y(t))return e;let r=e,n=t;for(let o of Object.keys(t))r[o]=Nn(r[o],n[o]);return r}function kr(e,t,r){return m(t>=0,"$skip value must be a non-negative integer"),e.drop(t)}var wn={$sort:it,$skip:kr,$limit:vr},Le=class{#t;#e;#r;#o;#s={};#n=null;#i=[];constructor(t,r,n,o){this.#t=t,this.#e=r,this.#r=n,this.#o=o}fetch(){if(this.#n)return this.#n;this.#n=S(this.#t).filter(this.#e);let t=this.#o.processingMode;t&1&&this.#n.map(r=>nt(r));for(let r of Object.keys(wn))if(v(this.#s,r)){let n=wn[r];this.#n=n(this.#n,this.#s[r],this.#o)}return Object.keys(this.#r).length&&(this.#n=Kt(this.#n,this.#r,this.#o)),t&2&&this.#n.map(r=>nt(r)),this.#n}fetchAll(){let t=S(Array.from(this.#i));return this.#i.length=0,vt(t,this.fetch())}all(){return this.fetchAll().collect()}skip(t){return this.#s.$skip=t,this}limit(t){return this.#s.$limit=t,this}sort(t){return this.#s.$sort=t,this}collation(t){return this.#o={...this.#o,collation:t},this}next(){if(this.#i.length>0)return this.#i.pop();let t=this.fetch().next();if(!t.done)return t.value}hasNext(){if(this.#i.length>0)return!0;let t=this.fetch().next();return t.done?!1:(this.#i.push(t.value),!0)}[Symbol.iterator](){return this.fetchAll()}};var ms=new Set(["$and","$or","$nor","$expr","$jsonSchema"]),J=class{#t;#e;#r;constructor(t,r){this.#e=nt(t),this.#r=k.init(r).update({condition:t}),this.#t=[],this.compile()}compile(){m(h(this.#e),`query criteria must be an object: ${JSON.stringify(this.#e)}`);let t={};for(let r of Object.keys(this.#e)){let n=this.#e[r];if(r==="$where")m(this.#r.scriptEnabled,"$where operator requires 'scriptEnabled' option to be true."),Object.assign(t,{field:r,expr:n});else if(ms.has(r))this.processOperator(r,r,n);else{m(!pt(r),`unknown top level operator: ${r}`);let o=qt(n);for(let i of Object.keys(o))this.processOperator(r,i,o[i])}t.field&&this.processOperator(t.field,t.field,t.expr)}}processOperator(t,r,n){let o=this.#r.context.getOperator("query",r);m(!!o,`unknown query operator ${r}`),this.#t.push(o(t,n,this.#r))}test(t){return this.#t.every(r=>r(t))}find(t,r){return new Le(t,n=>this.test(n),r||{},this.#r)}};function Cn(e,t){let r=i=>i,n=!0;for(let i of Object.keys(e))if(n&&=pt(i)&&i!=="$and"&&i!=="$or"&&i!=="$nor",!n)break;n&&(e={field:e},r=i=>({field:i}));let o=new J(e,t);return i=>o.test(r(i))}function B(e,t,r,n){let o=e.split("."),i=Math.max(1,o.length-1),s=k.init(r).update({depth:i}),a={unwrapArray:!0,pathArray:o};return n===Ve&&(t=Cn(t,r)),c=>{let p=R(c,e,a);return n(p,t,s)}}function bt(e,t,r,n){m(l(t)&&t.length===2,`${n.name} expects array(2)`);let[o,i]=f(e,t,r);return n(o,i,r)}function Oe(e,t,r){if(G(e,t)||y(e)&&y(t))return!0;if(l(e)){let n=r?.local?.depth??1;return e.some(o=>G(o,t))||ct(e,n).some(o=>G(o,t))}return!1}function Be(e,t,r){return!Oe(e,t,r)}function Cr(e,t,r){return y(e)?t.some(n=>n===null):Bt([at(e),t]).length>0}function _n(e,t,r){return!Cr(e,t,r)}function ze(e,t,r){return Ke(e,t,(n,o)=>M(n,o)<0)}function We(e,t,r){return Ke(e,t,(n,o)=>M(n,o)<=0)}function qe(e,t,r){return Ke(e,t,(n,o)=>M(n,o)>0)}function Ye(e,t,r){return Ke(e,t,(n,o)=>M(n,o)>=0)}function Mn(e,t,r){return at(e).some((n=>t.length===2&&n%t[0]===t[1]))}function Dn(e,t,r){let n=at(e),o=i=>$(i)&&et(t.exec(i),r?.useStrictMode);return n.some(o)||ct(n,1).some(o)}function Pn(e,t,r){if(!l(e)||!l(t)||!e.length||!t.length)return!1;let n=!0;for(let o of t){if(!n)break;if(h(o)&&Object.keys(o)[0]==="$elemMatch"){let i=o.$elemMatch,s=Cn(i,r);n=Ve(e,s,r)}else ot(o)?n=e.some(i=>$(i)&&o.test(i)):n=e.some(i=>G(o,i))}return n}function Rn(e,t,r){return Array.isArray(e)&&e.length===t}function Ve(e,t,r){if(l(e)&&!Lt(e)){for(let n=0,o=e.length;n<o;n++)if(t(e[n]))return!0}return!1}var Sn=e=>e===null,fs={array:l,boolean:kt,bool:kt,date:U,number:x,int:x,long:x,double:x,decimal:x,null:Sn,object:h,regexp:ot,regex:ot,string:$,undefined:y,1:x,2:$,3:h,4:l,6:y,8:kt,9:U,10:Sn,11:ot,16:x,18:x,19:x};function kn(e,t,r){let n=fs[t];return n?n(e):!1}function Un(e,t,r){return l(t)?t.findIndex(n=>kn(e,n,r))>=0:kn(e,t,r)}function Ke(e,t,r){for(let n of at(e))if(mt(n)===mt(t)&&r(n,t))return!0;return!1}var Fn=(e,t,r)=>bt(e,t,r,Oe);var Ln=(e,t,r)=>bt(e,t,r,qe);var Bn=(e,t,r)=>bt(e,t,r,Ye);var zn=(e,t,r)=>bt(e,t,r,ze);var Wn=(e,t,r)=>bt(e,t,r,We);var qn=(e,t,r)=>bt(e,t,r,Be);var Yn="$cond expects array(3) or object with 'if-then-else' expressions",ls=(e,t,r)=>{let n,o,i;l(t)?(m(t.length===3,Yn),n=t[0],o=t[1],i=t[2]):(m(h(t),Yn),n=t.if,o=t.then,i=t.else);let s=et(f(e,n,r),r.useStrictMode);return f(e,s?o:i,r)};var _r=(e,t,r)=>{m(l(t),"$ifNull expects an array");let n;for(let o of t)if(n=f(e,o,r),!y(n))return n;return n};var ys=(e,t,r)=>{m(h(t),"$switch received invalid arguments");for(let{case:n,then:o}of t.branches)if(et(f(e,n,r),r.useStrictMode))return f(e,o,r);return f(e,t.default,r)};var Mr=(e,t,r)=>{m(r.scriptEnabled,"$function requires 'scriptEnabled' option to be true");let n=f(e,t,r);return n.body.apply(null,n.args)};var be=["year","quarter","month","week","day","hour","minute","second","millisecond"],Os={mon:1,tue:2,wed:3,thu:4,fri:5,sat:6,sun:7},As=-1e9,oe=7,Dr=e=>(e&3)==0&&(e%100!=0||e%400==0);var bs=[365,366],ds=[[0,31,59,90,120,151,181,212,243,273,304,334],[0,31,60,91,121,152,182,213,244,274,305,335]],Qt=e=>ds[+Dr(e.getUTCFullYear())][e.getUTCMonth()]+e.getUTCDate(),Qe=(e,t)=>{let r=e.getUTCDay()||7,n=t.toLowerCase().substring(0,3);return(r-Os[n]+oe)%oe},Vn=e=>(e+Math.floor(e/4)-Math.floor(e/100)+Math.floor(e/400))%7,Kn=e=>52+ +(Vn(e)==4||Vn(e-1)==3);function Gt(e){let t=e.getUTCDay()||7,r=Math.floor((10+Qt(e)-t)/7);return r<1?Kn(e.getUTCFullYear()-1):r>Kn(e.getUTCFullYear())?1:r}function He(e){let t=Gt(e);return e.getUTCDay()>0&&e.getUTCDate()==1&&e.getUTCMonth()==0?0:e.getUTCDay()==0?t+1:t}function Xe(e){return e.getUTCFullYear()-+(e.getUTCMonth()===0&&e.getUTCDate()==1&&e.getUTCDay()<1)}var Ct=60,gt={week:6048e5,day:864e5,hour:36e5,minute:6e4,second:1e3,millisecond:1},Je="%Y-%m-%dT%H:%M:%S.%LZ",Pr=[["year",0,9999],["month",1,12],["day",1,31],["hour",0,23],["minute",0,59],["second",0,59],["millisecond",0,999]],Rr={jan:1,feb:2,mar:3,apr:4,may:5,jun:6,jul:7,aug:8,sep:9,oct:10,nov:11,dec:12},ie={"%b":{name:"abbr_month",padding:3,re:/(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)/i},"%B":{name:"full_month",padding:0,re:/(January|February|March|April|May|June|July|August|September|October|November|December)/i},"%Y":{name:"year",padding:4,re:/([0-9]{4})/},"%G":{name:"year",padding:4,re:/([0-9]{4})/},"%m":{name:"month",padding:2,re:/(0[1-9]|1[012])/},"%d":{name:"day",padding:2,re:/(0[1-9]|[12][0-9]|3[01])/},"%j":{name:"day_of_year",padding:3,re:/(0[0-9][1-9]|[12][0-9]{2}|3[0-5][0-9]|36[0-6])/},"%H":{name:"hour",padding:2,re:/([01][0-9]|2[0-3])/},"%M":{name:"minute",padding:2,re:/([0-5][0-9])/},"%S":{name:"second",padding:2,re:/([0-5][0-9]|60)/},"%L":{name:"millisecond",padding:3,re:/([0-9]{3})/},"%w":{name:"day_of_week",padding:1,re:/([0-6])/},"%u":{name:"day_of_week_iso",padding:1,re:/([1-7])/},"%U":{name:"week_of_year",padding:2,re:/([1-4][0-9]?|5[0-3]?)/},"%V":{name:"week_of_year_iso",padding:2,re:/([1-4][0-9]?|5[0-3]?)/},"%z":{name:"timezone",padding:2,re:/(([+-][01][0-9]|2[0-3]):?([0-5][0-9])?)/},"%Z":{name:"minute_offset",padding:3,re:/([+-][0-9]{3})/},"%%":{name:"percent_literal",padding:1,re:/%%/}},Ze=/(%[bBYGmdjHMSLwuUVzZ%])/g,Hn=/%[bBYGmdjHMSLwuUVzZ%]/,gs=/^[a-zA-Z_]+\/[a-zA-Z_]+$/;function ft(e,t){if(e===void 0)return 0;if(gs.test(e)){let i=new Date(t.toLocaleString("en-US",{timeZone:"UTC"})),s=new Date(t.toLocaleString("en-US",{timeZone:e}));return Math.round((s.getTime()-i.getTime())/6e4)}let r=ie["%z"].re.exec(e)??[];m(!!r,`timezone '${e}' is invalid or not supported.`);let n=parseInt(r[2])||0,o=parseInt(r[3])||0;return(Math.abs(n*Ct)+o)*(n<0?-1:1)}function Xn(e){return(e<0?"-":"+")+Ge(Math.abs(Math.floor(e/Ct)),2)+Ge(Math.abs(e)%Ct,2)}function dt(e,t){e.setUTCMinutes(e.getUTCMinutes()+t)}function q(e,t,r){if(U(e))return e;let n=f(e,t,r);if(U(n))return new Date(n);if(x(n))return new Date(n*1e3);m(!!n?.date,`cannot convert ${JSON.stringify(t)} to date`);let o=U(n.date)?new Date(n.date):new Date(n.date*1e3);return n.timezone&&dt(o,ft(n.timezone,o)),o}function Ge(e,t){return new Array(Math.max(t-String(e).length+1,0)).join("0")+e.toString()}var Qn=e=>{let t=e-As;return Math.trunc(t/4)-Math.trunc(t/100)+Math.trunc(t/400)};function xs(e,t){return Math.trunc(Qn(t-1)-Qn(e-1)+(t-e)*bs[0])}var se=(e,t)=>t.getUTCFullYear()-e.getUTCFullYear(),tr=(e,t)=>t.getUTCMonth()-e.getUTCMonth()+se(e,t)*12,er=(e,t)=>{let r=Math.trunc(e.getUTCMonth()/3);return Math.trunc(t.getUTCMonth()/3)-r+se(e,t)*4},ae=(e,t)=>Qt(t)-Qt(e)+xs(e.getUTCFullYear(),t.getUTCFullYear()),rr=(e,t,r)=>{let n=(r||"sun").substring(0,3);return Math.trunc((ae(e,t)+Qe(e,n)-Qe(t,n))/oe)},Jn=(e,t)=>t.getUTCHours()-e.getUTCHours()+ae(e,t)*24,Gn=(e,t)=>{let r=e.getUTCMonth()+t,n=Math.floor(r/12);if(r<0){let o=r%12+12;e.setUTCFullYear(e.getUTCFullYear()+n,o,e.getUTCDate())}else e.setUTCFullYear(e.getUTCFullYear()+n,r%12,e.getUTCDate())},ce=(e,t,r,n)=>{let o=new Date(e);switch(t){case"year":o.setUTCFullYear(o.getUTCFullYear()+r);break;case"quarter":Gn(o,3*r);break;case"month":Gn(o,r);break;default:o.setTime(o.getTime()+gt[t]*r)}return o};var de=(e,t,r)=>{let n=f(e,t,r);return ce(n.startDate,n.unit,n.amount,n.timezone)};var hs=(e,t,r)=>{let{startDate:n,endDate:o,unit:i,timezone:s,startOfWeek:a}=f(e,t,r),c=new Date(n),p=new Date(o);switch(dt(c,ft(s,c)),dt(p,ft(s,p)),i){case"year":return se(c,p);case"quarter":return er(c,p);case"month":return tr(c,p);case"week":return rr(c,p,a);case"day":return ae(c,p);case"hour":return Jn(c,p);case"minute":return c.setUTCSeconds(0),c.setUTCMilliseconds(0),p.setUTCSeconds(0),p.setUTCMilliseconds(0),Math.round((p.getTime()-c.getTime())/gt[i]);default:return Math.round((p.getTime()-c.getTime())/gt[i])}};var js=[31,28,31,30,31,30,31,31,30,31,30,31],$s=e=>e.month==2&&Dr(e.year)?29:js[e.month-1],Es=(e,t,r)=>{let n=f(e,t,r),o=ft(n.timezone,new Date);for(let i=Pr.length-1,s=0;i>=0;i--){let a=Pr[i],c=a[0],p=a[1],u=a[2],O=(n[c]||0)+s;s=0;let A=u+1;if(c=="hour"&&(O+=Math.floor(o/Ct)*-1),c=="minute"&&(O+=o%Ct*-1),O<p){let b=p-O;s=-1*Math.ceil(b/A),O=A-b%A}else O>u&&(O+=p,s=Math.trunc(O/A),O%=A);n[c]=O}return n.day=Math.min(n.day,$s(n)),new Date(Date.UTC(n.year,n.month-1,n.day,n.hour,n.minute,n.second,n.millisecond))};function Is(e){return e==="Z"?0:e>="A"&&e<"N"?e.charCodeAt(0)-64:77-e.charCodeAt(0)}var Ts=e=>e.replace(/^\//,"").replace(/\/$/,"").replace(/\/i/,""),vs=["^",".","-","*","?","$"];function Ns(e){for(let t of vs)e=e.replace(t,`\\${t}`);return e}function ws(e,t,r){let n=f(e,t,r),o=n.format||Je,i=n.onNull||null,s=n.dateString;if(y(s))return i;let a=o.split(Hn);a.reverse();let c=o.match(Ze),p={},u="";for(let d=0,g=c.length;d<g;d++){let E=c[d],T=ie[E];if(h(T)){let w=T.re.exec(s),C=a.pop()||"";w!==null?(p[T.name]=/^\d+$/.exec(w[0])?parseInt(w[0]):w[0],s=s.substring(w.index+w[0].length+1),u+=Ns(C)+Ts(T.re.toString())):p[T.name]=null}}if(y(p.month)){let d=(p.full_month?.slice(0,3)??p.abbr_month??"").toLowerCase();Rr[d]&&(p.month=Rr[d])}if(y(p.year)||y(p.month)||y(p.day)||!new RegExp("^"+u+"[A-Z]?$").test(n.dateString))return n.onError;let O=n.dateString.match(/([A-Z])$/);m(!(O&&n.timezone),`$dateFromString: you cannot pass in a date/time string with time zone information ('${O&&O[0]}') together with a timezone argument`);let A=O?Is(O[0])*Ct:ft(n.timezone,new Date),b=new Date(Date.UTC(p.year,p.month-1,p.day,0,0,0));return y(p.hour)||b.setUTCHours(p.hour),y(p.minute)||b.setUTCMinutes(p.minute),y(p.second)||b.setUTCSeconds(p.second),y(p.millisecond)||b.setUTCMilliseconds(p.millisecond),dt(b,-A),b}var Ss=(e,t,r)=>{let n=f(e,t,r);return ce(n.startDate,n.unit,-n.amount,n.timezone)};var ks=(e,t,r)=>{let n=f(e,t,r),o=new Date(n.date);dt(o,ft(n.timezone,o));let i={hour:o.getUTCHours(),minute:o.getUTCMinutes(),second:o.getUTCSeconds(),millisecond:o.getUTCMilliseconds()};return n.iso8601==!0?Object.assign(i,{isoWeekYear:Xe(o),isoWeek:Gt(o),isoDayOfWeek:o.getUTCDay()||7}):Object.assign(i,{year:o.getUTCFullYear(),month:o.getUTCMonth()+1,day:o.getUTCDate()})};var Cs={"%Y":e=>e.getUTCFullYear(),"%G":e=>e.getUTCFullYear(),"%m":e=>e.getUTCMonth()+1,"%d":e=>e.getUTCDate(),"%H":