!function(r,n){"use strict";function t(r){for(var n=0;n<g.length;n++)if(g[n](r))return!0;return!1}function e(r){if(t(r))return h.isRegExp(r)?{$regex:r}:{$eq:r};if(h.isObject(r)){var n=h.keys(r),e=0===h.intersection(s(U),n).length;if(e)return{$eq:r};if(h.contains(n,"$regex")){var i=r.$regex,u=r.$options||"",o="";h.isString(i)&&(o+=i.ignoreCase||u.indexOf("i")>=0?"i":"",o+=i.multiline||u.indexOf("m")>=0?"m":"",o+=i.global||u.indexOf("g")>=0?"g":"",i=new RegExp(i,o)),r.$regex=i,delete r.$options}}return r}function i(r,n){return h.result(r,n)}function u(r,t){if(!t)return n;for(var e,o=t.split("."),s=r,a=0;a<o.length;a++){if(e=null===o[a].match(/^\d+$/),e&&h.isArray(s)){var c=[];h.each(s,function(r){c.push(u(r,o[a]))}),s=c}else s=i(s,o[a]);if(s===n)break}return s}function o(r,n){return new Array(Math.max(n-String(r).length+1,0)).join("0")+r}function s(r){return h.keys(Y[r])}function a(r,n){var t={keys:[],groups:[]};if(h.each(r,function(r){var e=n(r),i=-1;if(h.isObject(e)){for(var u=0;u<t.keys.length;u++)if(h.isEqual(e,t.keys[u])){i=u;break}}else i=h.indexOf(t.keys,e);i>-1?t.groups[i].push(r):(t.keys.push(e),t.groups.push([r]))}),t.keys.length!==t.groups.length)throw new Error("assert groupBy(): keys.length !== groups.length");return t}function c(r,t,e){if(h.contains(s(C),t))return O[t](r,e);if(h.isObject(e)){var i={};for(var u in e)if(e.hasOwnProperty(u)&&(i[u]=c(r,u,e[u]),h.contains(s(C),u))){if(i=i[u],h.keys(e).length>1)throw new Error("Invalid $group expression '"+JSON.stringify(e)+"'");break}return i}return n}function f(r,n,e){if(h.contains(s(P),e))return Q[e](r,n);if(h.isString(n)&&n.length>0&&"$"===n[0])return u(r,n.slice(1));var i;if(t(n))return n;if(h.isArray(n))i=h.map(n,function(n){return f(r,n,null)});else if(h.isObject(n)){i={};for(var o in n)if(n.hasOwnProperty(o)&&(i[o]=f(r,n[o],o),h.contains(s(P),o))){if(i=i[o],h.keys(n).length>1)throw new Error("Invalid aggregation expression '"+JSON.stringify(n)+"'");break}}return i}var l,h,p={};p.VERSION="0.6.1",null!=r&&(l=r.Mingo),p.noConflict=function(){return r.Mingo=l,p};var y="undefined"!=typeof module&&"undefined"!=typeof require;y?("undefined"!=typeof module&&(module.exports=p),h=require("underscore")):(r.Mingo=p,h=r._);var g=[h.isString,h.isBoolean,h.isNumber,h.isDate,h.isNull,h.isRegExp],v={key:"_id"};if(p.setup=function(r){h.extend(v,r||{})},p.Query=function(r,n){return this instanceof p.Query?(this._criteria=r,this._projection=n,this._compiled=[],void this._compile()):new p.Query(r,n)},p.Query.prototype={_compile:function(){if(!h.isEmpty(this._criteria)){if(h.isArray(this._criteria)||h.isFunction(this._criteria)||!h.isObject(this._criteria))throw new Error("Invalid type for criteria");for(var r in this._criteria)if(this._criteria.hasOwnProperty(r)){var n=this._criteria[r];if(h.contains(["$and","$or","$nor","$where"],r))this._processOperator(r,r,n);else{n=e(n);for(var t in n)n.hasOwnProperty(t)&&this._processOperator(r,t,n[t])}}}},_processOperator:function(r,n,t){if(!h.contains(s(U),n))throw new Error("Invalid query operator '"+n+"' detected");this._compiled.push(w[n](r,t))},test:function(r){for(var n=0;n<this._compiled.length;n++)if(!this._compiled[n].test(r))return!1;return!0},find:function(r,n){return new p.Cursor(r,this,n)},remove:function(r){for(var n=[],t=0;t<r.length;t++)this.test(r[t])||n.push(r[t]);return n}},y){var $=require("stream").Transform,d=require("util");p.Query.prototype.stream=function(r){return new p.Stream(this,r)},p.Stream=function(r,n){return this instanceof p.Stream?(n=n||{},h.extend(n,{objectMode:!0}),$.call(this,n),void(this._query=r)):new p.Stream(r,n)},d.inherits(p.Stream,$),p.Stream.prototype._transform=function(r,n,t){if(h.isObject(r)&&this._query.test(r))if(h.isEmpty(this._query._projection))this.push(r);else{var e=new p.Cursor([r],this._query);e.hasNext()&&this.push(e.next())}t()}}p.Cursor=function(r,n,t){return this instanceof p.Cursor?(this._query=n,this._collection=r,this._projection=t||n._projection,this._operators={},this._result=!1,void(this._position=0)):new p.Cursor(r,n,t)},p.Cursor.prototype={_fetch:function(){var r=this;if(this._result!==!1)return this._result;if(h.isObject(this._projection)&&h.extend(this._operators,{$project:this._projection}),!h.isArray(this._collection))throw new Error("Input collection is not of valid type. Must be an Array.");this._result=h.filter(this._collection,this._query.test,this._query);var n=[];if(h.each(["$sort","$skip","$limit","$project"],function(t){h.has(r._operators,t)&&n.push(h.pick(r._operators,t))}),n.length>0){var t=new p.Aggregator(n);this._result=t.run(this._result,this._query)}return this._result},all:function(){return this._fetch()},first:function(){return this.count()>0?this._fetch()[0]:null},last:function(){return this.count()>0?this._fetch()[this.count()-1]:null},count:function(){return this._fetch().length},skip:function(r){return h.extend(this._operators,{$skip:r}),this},limit:function(r){return h.extend(this._operators,{$limit:r}),this},sort:function(r){return h.extend(this._operators,{$sort:r}),this},next:function(){return this.hasNext()?this._fetch()[this._position++]:null},hasNext:function(){return this.count()>this._position},max:function(r){return O.$max(this._fetch(),r)},min:function(r){return O.$min(this._fetch(),r)},map:function(r){return h.map(this._fetch(),r)},forEach:function(r){h.each(this._fetch(),r)}},p.Aggregator=function(r){return this instanceof p.Aggregator?void(this._operators=r):new p.Aggregator(r)},p.Aggregator.prototype={run:function(r,n){if(!h.isEmpty(this._operators))for(var t=0;t<this._operators.length;t++){var e=this._operators[t],i=h.keys(e);if(1!=i.length||!h.contains(s(R),i[0]))throw new Error("Invalid aggregation operator '"+i+"'");i=i[0],r=n instanceof p.Query?m[i].call(n,r,e[i]):m[i](r,e[i])}return r}},p.find=function(r,n,t){return new p.Query(n).find(r,t)},p.remove=function(r,n){return new p.Query(n).remove(r)},p.aggregate=function(r,n){if(!h.isArray(n))throw new Error("Aggregation pipeline must be an array");return new p.Aggregator(n).run(r)},p.addOperators=function(r,n){var t=n({resolve:u,computeValue:f,ops:s,key:function(){return v.key}});if(!h.contains([P,C,R,T,U],r))throw new Error("Could not identify type '"+r+"'");var e=s(r);h.each(h.keys(t),function(n){if(!/^\$\w+$/.test(n))throw new Error("Invalid operator name '"+n+"'");if(h.contains(e,n))throw new Error("Operator "+n+" is already defined for "+r+" operators")});var i={};switch(r){case U:h.each(h.keys(t),function(r){i[r]=function(n,t){return function(e,i){return{test:function(o){var s=u(o,e),a=n.call(t,e,s,i);if(h.isBoolean(a))return a;if(a instanceof p.Query)return a.test(o);throw new Error("Invalid return type for '"+r+"'. Must return a Boolean or Mingo.Query")}}}}(t[r],t)});break;case T:h.each(h.keys(t),function(r){i[r]=function(r,n){return function(t,e,i){var o=u(t,i);return r.call(n,i,o,e)}}(t[r],t)});break;default:h.each(h.keys(t),function(r){i[r]=function(r,n){return function(){var t=Array.prototype.slice.call(arguments);return r.apply(n,t)}}(t[r],t)})}h.extend(Y[r],i)},p.CollectionMixin={query:function(r,n){return p.find(this.toJSON(),r,n)},aggregate:function(r){var n=[this.toJSON(),r];return p.aggregate.apply(null,n)}};var m={$group:function(r,n){var t=n[v.key],e=a(r,function(r){return f(r,t,t)}),i=[];return n=h.omit(n,v.key),h.each(e.keys,function(r,t){var u={};h.isUndefined(r)||(u[v.key]=r);for(var o in n)n.hasOwnProperty(o)&&(u[o]=c(e.groups[t],o,n[o]));i.push(u)}),i},$match:function(r,n){return new p.Query(n).find(r).all()},$project:function(r,t){if(h.isEmpty(t))return r;var e=[],i=h.keys(t),u=!1;if(h.contains(i,v.key)){var o=t[v.key];(0===o||o===!1)&&(i=h.without(i,v.key),h.isEmpty(i)&&(u=!0))}else i.push(v.key);for(var a=0;a<r.length;a++){var c=r[a],l={},p=!1,y=!1,g=[];u&&g.push(v.key),h.each(i,function(r){var e,i=t[r];if(r!==v.key&&0===i&&(y=!0),r===v.key&&h.isEmpty(i))e=c[r];else if(h.isString(i))e=f(c,i,r);else if(1===i||i===!0)e=h.result(c,r);else if(h.isObject(i)){var u=h.keys(i);if(u=u.length>1?!1:u[0],u!==!1&&h.contains(s(T),u)){var o=k[u](c,i[u],r);h.isUndefined(o)||(e=o),"$slice"==u&&(p=!0)}else e=f(c,i,r)}else g.push(r);e!==n&&(l[r]=h.isObject(e)?h.clone(e):e)}),(p||y||u)&&(l=h.defaults(l,h.omit(c,g))),e.push(l)}return e},$limit:function(r,n){return h.first(r,n)},$skip:function(r,n){return h.rest(r,n)},$unwind:function(r,n){for(var t=[],e=n.substr(1),u=0;u<r.length;u++){var o=r[u],s=i(o,e);if(!h.isArray(s))throw new Error("Target field '"+e+"' is not of type Array.");h.each(s,function(r){var n=h.clone(o);n[e]=r,t.push(n)})}return t},$sort:function(r,n){if(!h.isEmpty(n)&&h.isObject(n)){var t=h.keys(n);t.reverse().forEach(function(t){var e=[],i=h.groupBy(r,function(r){var n=u(r,t);return e.push(n),n});e=h.sortBy(h.uniq(e),function(r){return r}),-1===n[t]&&e.reverse(),r=[],h.each(e,function(n){Array.prototype.push.apply(r,i[n])})})}return r}},w={},_={$and:function(r,n){if(!h.isArray(n))throw new Error("Invalid expression for $and criteria");var t=[];return h.each(n,function(r){t.push(new p.Query(r))}),{test:function(r){for(var n=0;n<t.length;n++)if(!t[n].test(r))return!1;return!0}}},$or:function(r,n){if(!h.isArray(n))throw new Error("Invalid expression for $or criteria");var t=[];return h.each(n,function(r){t.push(new p.Query(r))}),{test:function(r){for(var n=0;n<t.length;n++)if(t[n].test(r))return!0;return!1}}},$nor:function(r,n){if(!h.isArray(n))throw new Error("Invalid expression for $nor criteria");var t=this.$or("$or",n);return{test:function(r){return!t.test(r)}}},$not:function(r,n){var t={};t[r]=e(n);var i=new p.Query(t);return{test:function(r){return!i.test(r)}}},$where:function(r,n){return h.isFunction(n)||(n=new Function("return "+n+";")),{test:function(r){return n.call(r)===!0}}}};h.extend(w,_);var E={$eq:function(r,t){return r=h.flatten(h.isArray(r)?r:[r]),r=h.find(r,function(r){return h.isEqual(r,t)}),r!==n},$ne:function(r,n){return!this.$eq(r,n)},$in:function(r,n){return r=h.isArray(r)?r:[r],h.intersection(r,n).length>0},$nin:function(r,n){return h.isUndefined(r)||!this.$in(r,n)},$lt:function(r,t){return r=h.isArray(r)?r:[r],r=h.find(r,function(r){return t>r}),r!==n},$lte:function(r,t){return r=h.isArray(r)?r:[r],r=h.find(r,function(r){return t>=r}),r!==n},$gt:function(r,t){return r=h.isArray(r)?r:[r],r=h.find(r,function(r){return r>t}),r!==n},$gte:function(r,t){return r=h.isArray(r)?r:[r],r=h.find(r,function(r){return r>=t}),r!==n},$mod:function(r,t){return r=h.isArray(r)?r:[r],r=h.find(r,function(r){return h.isNumber(r)&&h.isArray(t)&&2===t.length&&r%t[0]===t[1]}),r!==n},$regex:function(r,t){return r=h.isArray(r)?r:[r],r=h.find(r,function(r){return h.isString(r)&&h.isRegExp(t)&&!!r.match(t)}),r!==n},$exists:function(r,n){return n===!1&&h.isUndefined(r)||n===!0&&!h.isUndefined(r)},$all:function(r,n){var t=this,e=!1;if(h.isArray(r)&&h.isArray(n))for(var i=0;i<n.length;i++){if(!h.isObject(n[i])||!h.contains(h.keys(n[i]),"$elemMatch"))return h.intersection(n,r).length===n.length;e=e||t.$elemMatch(r,n[i].$elemMatch)}return e},$size:function(r,n){return h.isArray(r)&&h.isNumber(n)&&r.length===n},$elemMatch:function(r,n){if(h.isArray(r)&&!h.isEmpty(r))for(var t=new p.Query(n),e=0;e<r.length;e++)if(t.test(r[e]))return!0;return!1},$type:function(r,n){switch(n){case 1:return h.isNumeric(r)&&-1!==(r+"").indexOf(".");case 2:case 5:return h.isString(r);case 3:return h.isObject(r);case 4:return h.isArray(r);case 8:return h.isBoolean(r);case 9:return h.isDate(r);case 10:return h.isNull(r);case 11:return h.isRegExp(r);case 16:return h.isNumeric(r)&&2147483647>=r&&-1===(r+"").indexOf(".");case 18:return h.isNumeric(r)&&r>2147483647&&0x8000000000000000>=r&&-1===(r+"").indexOf(".");default:return!1}}};h.each(h.keys(E),function(r){w[r]=function(r,n){return function(t,e){return{test:function(i){var o=u(i,t);return r.call(n,o,e)}}}}(E[r],E)});var k={$:function(){throw new Error("$ not implemented")},$elemMatch:function(r,t,e){var i=u(r,e),o=new p.Query(t);if(h.isUndefined(i)||!h.isArray(i))return n;for(var s=0;s<i.length;s++)if(o.test(i[s]))return[i[s]];return n},$slice:function(r,n,t){var e=u(r,t);if(!h.isArray(e))return e;if(h.isArray(n)){var i=n[0]<0?e.length+n[0]:n,o=i+n[1];n=[i,o]}else{if(!h.isNumber(n))throw new Error("Invalid type for $slice operator");n=0>n?[n]:[0,n]}return Array.prototype.slice.apply(e,n)}},O={$addToSet:function(r,n){var t=h.map(r,function(r){return f(r,n,null)});return h.uniq(t)},$sum:function(r,n){return h.isArray(r)?h.isNumber(n)?r.length*n:h.reduce(r,function(r,t){return r+f(t,n,null)},0):0},$max:function(r,n){var t=h.max(r,function(r){return f(r,n,null)});return f(t,n,null)},$min:function(r,n){var t=h.min(r,function(r){return f(r,n,null)});return f(t,n,null)},$avg:function(r,n){return this.$sum(r,n)/(r.length||1)},$push:function(r,n){return h.map(r,function(r){return f(r,n,null)})},$first:function(r,t){return r.length>0?f(r[0],t):n},$last:function(r,t){return r.length>0?f(r[r.length-1],t):n}},A={$add:function(r,n){var t=f(r,n,null);return h.reduce(t,function(r,n){return r+n},0)},$subtract:function(r,n){var t=f(r,n,null);return t[0]-t[1]},$divide:function(r,n){var t=f(r,n,null);return t[0]/t[1]},$multiply:function(r,n){var t=f(r,n,null);return h.reduce(t,function(r,n){return r*n},1)},$mod:function(r,n){var t=f(r,n,null);return t[0]%t[1]}},x={$concat:function(r,t){var e=f(r,t,null);return h.contains(e,null)||h.contains(e,n)?null:e.join("")},$strcasecmp:function(r,n){var t=f(r,n,null);return t[0]=h.isEmpty(t[0])?"":t[0].toUpperCase(),t[1]=h.isEmpty(t[1])?"":t[1].toUpperCase(),t[0]>t[1]?1:t[0]<t[1]?-1:0},$substr:function(r,n){var t=f(r,n,null);return h.isString(t[0])?t[1]<0?"":t[2]<0?t[0].substr(t[1]):t[0].substr(t[1],t[2]):""},$toLower:function(r,n){var t=f(r,n,null);return h.isEmpty(t)?"":t.toLowerCase()},$toUpper:function(r,n){var t=f(r,n,null);return h.isEmpty(t)?"":t.toUpperCase()}},b={$dayOfYear:function(r,t){var e=f(r,t,null);if(h.isDate(e)){var i=new Date(e.getFullYear(),0,0),u=e-i,o=864e5;return Math.round(u/o)}return n},$dayOfMonth:function(r,t){var e=f(r,t,null);return h.isDate(e)?e.getDate():n},$dayOfWeek:function(r,t){var e=f(r,t,null);return h.isDate(e)?e.getDay()+1:n},$year:function(r,t){var e=f(r,t,null);return h.isDate(e)?e.getFullYear():n},$month:function(r,t){var e=f(r,t,null);return h.isDate(e)?e.getMonth()+1:n},$week:function(r,n){var t=f(r,n,null);t=new Date(+t),t.setHours(0,0,0),t.setDate(t.getDate()+4-(t.getDay()||7));var e=new Date(t.getFullYear(),0,1);return Math.floor(((t-e)/864e5+1)/7)},$hour:function(r,t){var e=f(r,t,null);return h.isDate(e)?e.getHours():n},$minute:function(r,t){var e=f(r,t,null);return h.isDate(e)?e.getMinutes():n},$second:function(r,t){var e=f(r,t,null);return h.isDate(e)?e.getSeconds():n},$millisecond:function(r,t){var e=f(r,t,null);return h.isDate(e)?e.getMilliseconds():n},$dateToString:function(r,n){for(var t=n.format,e=f(r,n.date,null),i=t.match(/(%%|%Y|%m|%d|%H|%M|%S|%L|%j|%w|%U)/g),u=0,s=i.length;s>u;u++){var a=B[i[u]],c=a;if(h.isArray(a)){var l=this[a[0]],p=a[1];c=o(l.call(this,r,e),p)}t=t.replace(i[u],c)}return t}},j={$setEquals:function(r,n){var t=f(r,n,null),e=h.uniq(t[0]),i=h.uniq(t[1]);return e.length!==i.length?!1:0==h.difference(e,i).length},$setIntersection:function(r,n){var t=f(r,n,null);return h.intersection(t[0],t[1])},$setDifference:function(r,n){var t=f(r,n,null);return h.difference(t[0],t[1])},$setUnion:function(r,n){var t=f(r,n,null);return h.union(t[0],t[1])},$setIsSubset:function(r,n){var t=f(r,n,null);return h.intersection(t[0],t[1]).length===t[0].length},$anyElementTrue:function(r,n){for(var t=f(r,n,null)[0],e=0;e<t.length;e++)if(t[e])return!0;return!1},$allElementsTrue:function(r,n){for(var t=f(r,n,null)[0],e=0;e<t.length;e++)if(!t[e])return!1;return!0}},q={$cond:function(r,n){var t,e,i;if(h.isArray(n)){if(3!=n.length)throw new Error("Invalid arguments for $cond operator");t=n[0],e=n[1],i=n[2]}else h.isObject(n)&&(t=n["if"],e=n.then,i=n["else"]);var u=f(r,t,null);return u?f(r,e,null):f(r,i,null)},$ifNull:function(r,t){if(!h.isArray(t)||2!=t.length)throw new Error("Invalid arguments for $ifNull operator");var e=f(r,t,null);return null===e[0]||e[0]===n?e[1]:e[0]}},S={$cmp:function(r,n){var t=f(r,n,null);return t[0]>t[1]?1:t[0]<t[1]?-1:0}};h.each(["$eq","$ne","$gt","$gte","$lt","$lte"],function(r){S[r]=function(n,t){var e=f(n,t,null);return E[r](e[0],e[1])}});var M={$size:function(r,t){var e=f(r,t,null);return h.isArray(e)?e.length:n}},N={$literal:function(r,n){return n}},I={$map:function(r,n){var t=f(r,n.input,null);if(!h.isArray(t))throw new Error("Input expression for $map must resolve to an array");var e=n.as,i=n["in"],u="$"+e,o=r[u];return h.map(t,function(n){r[u]=n;var t=f(r,i,null);return h.isUndefined(o)?delete r[u]:r[u]=o,t})},$let:function(r,n){var t=n.vars,e=n["in"],i={},u=h.keys(t);h.each(u,function(n){var e=f(r,t[n],null),u="$"+n;i[u]=r[u],r[u]=e});var o=f(r,e,null);return h.each(u,function(n){var t="$"+n;h.isUndefined(i[t])?delete r[t]:r[t]=i[t]}),o}},D={$and:function(r,n){var t=f(r,n,null);return h.every(t)},$or:function(r,n){var t=f(r,n,null);return h.some(t)},$not:function(r,n){return!f(r,n[0],null)}},Q=h.extend({},M,A,D,S,q,b,N,j,x,I),U=p.OP_QUERY="query",C=p.OP_GROUP="group",P=p.OP_AGGREGATE="aggregate",R=p.OP_PIPELINE="pipeline",T=p.OP_PROJECTION="projection",Y={aggregate:Q,group:O,pipeline:m,projection:k,query:w},B={"%Y":["$year",4],"%m":["$month",2],"%d":["$dayOfMonth",2],"%H":["$hour",2],"%M":["$minute",2],"%S":["$second",2],"%L":["$millisecond",3],"%j":["$dayOfYear",3],"%w":["$dayOfWeek",1],"%U":["$week",2],"%%":"%"}}(this);
//# sourceMappingURL=mingo.min.map