|
|
@@ -10,11 +10,12 @@ var DocumentSource = require('./DocumentSource');
|
|
|
* @constructor
|
|
|
* @param [ctx] {ExpressionContext}
|
|
|
**/
|
|
|
-var ProjectDocumentSource = module.exports = function ProjectDocumentSource(ctx){
|
|
|
- if (arguments.length > 1) throw new Error("up to one arg expected");
|
|
|
+var ProjectDocumentSource = module.exports = function ProjectDocumentSource(ctx, exprObj){
|
|
|
+ if (arguments.length > 2) throw new Error("up to two args expected");
|
|
|
base.call(this, ctx);
|
|
|
- this.OE = new ObjectExpression();
|
|
|
+ this.OE = new ObjectExpression(exprObj);
|
|
|
this._raw = undefined;
|
|
|
+ this._variables = undefined;
|
|
|
}, klass = ProjectDocumentSource, base = require('./DocumentSource'), proto = klass.prototype = Object.create(base.prototype, {constructor:{value:klass}});
|
|
|
|
|
|
// DEPENDENCIES
|
|
|
@@ -69,11 +70,11 @@ proto.getNext = function getNext(callback) {
|
|
|
};
|
|
|
|
|
|
/**
|
|
|
- * Returns the object that was used to construct the ProjectDocumentSource
|
|
|
- * @return {object} the object that was used to construct the ProjectDocumentSource
|
|
|
+ * Optimizes the internal ObjectExpression
|
|
|
+ * @return
|
|
|
**/
|
|
|
-proto.getRaw = function getRaw() {
|
|
|
- return this._raw;
|
|
|
+proto.optimize = function optimize() {
|
|
|
+ this.OE = this.OE.optimize();
|
|
|
};
|
|
|
|
|
|
proto.serialize = function serialize(explain) {
|
|
|
@@ -82,38 +83,13 @@ proto.serialize = function serialize(explain) {
|
|
|
return out;
|
|
|
};
|
|
|
|
|
|
-/**
|
|
|
- * Optimizes the internal ObjectExpression
|
|
|
- * @return
|
|
|
- **/
|
|
|
-proto.optimize = function optimize() {
|
|
|
- this.OE.optimize();
|
|
|
-};
|
|
|
-
|
|
|
-proto.toJSON = function toJSON(){
|
|
|
- var obj = {};
|
|
|
- this.sourceToJson(obj);
|
|
|
- return obj;
|
|
|
-};
|
|
|
-
|
|
|
-/**
|
|
|
- * Places a $project key inside the builder object with value of this.OE
|
|
|
- * @method sourceToJson
|
|
|
- * @param {builder} An object (was ported from BsonBuilder)
|
|
|
- * @return
|
|
|
- **/
|
|
|
-proto.sourceToJson = function sourceToJson(builder, explain) {
|
|
|
- var insides = this.OE.toJSON(true);
|
|
|
- builder[this.getSourceName()] = insides;
|
|
|
-};
|
|
|
-
|
|
|
/**
|
|
|
* Builds a new ProjectDocumentSource from an object
|
|
|
* @method createFromJson
|
|
|
* @return {ProjectDocmentSource} a ProjectDocumentSource instance
|
|
|
**/
|
|
|
-klass.createFromJson = function(jsonElement, expCtx) {
|
|
|
- if (!(jsonElement instanceof Object) || jsonElement.constructor !== Object) throw new Error('Error 15969. Specification must be an object but was ' + typeof jsonElement);
|
|
|
+klass.createFromJson = function(elem, expCtx) {
|
|
|
+ if (!(elem instanceof Object) || elem.constructor !== Object) throw new Error('Error 15969. Specification must be an object but was ' + typeof elem);
|
|
|
|
|
|
var objectContext = new Expression.ObjectCtx({
|
|
|
isDocumentOk: true,
|
|
|
@@ -121,17 +97,22 @@ klass.createFromJson = function(jsonElement, expCtx) {
|
|
|
isInclusionOk: true
|
|
|
});
|
|
|
|
|
|
- var project = new ProjectDocumentSource(expCtx),
|
|
|
- idGenerator = new VariablesIdGenerator(),
|
|
|
- vps = new VariablesParseState(idGenerator);
|
|
|
+ var idGenerator = new VariablesIdGenerator(),
|
|
|
+ vps = new VariablesParseState(idGenerator),
|
|
|
+ parsed = Expression.parseObject(elem, objectContext, vps),
|
|
|
+ exprObj = parsed;
|
|
|
|
|
|
- project._raw = jsonElement;
|
|
|
- var parsed = Expression.parseObject(jsonElement, objectContext, vps);
|
|
|
- var exprObj = parsed;
|
|
|
if (!exprObj instanceof ObjectExpression) throw new Error("16402, parseObject() returned wrong type of Expression");
|
|
|
- if (!exprObj.getFieldCount()) throw new Error("16403, $projection requires at least one output field");
|
|
|
- project.OE = exprObj;
|
|
|
+ //if (!exprObj.getFieldCount() ) throw new Error("uassert 16403: $project requires at least one output field");
|
|
|
+
|
|
|
+ var project = new ProjectDocumentSource(expCtx, exprObj);
|
|
|
project._variables = new Variables(idGenerator.getIdCount());
|
|
|
+
|
|
|
+ var projectObj = elem
|
|
|
+ project.OE = exprObj;
|
|
|
+
|
|
|
+ project._raw = elem;
|
|
|
+
|
|
|
return project;
|
|
|
};
|
|
|
|
|
|
@@ -145,3 +126,17 @@ proto.getDependencies = function getDependencies(deps) {
|
|
|
this.OE.addDependencies(deps, path);
|
|
|
return base.GetDepsReturn.EXHAUSTIVE;
|
|
|
};
|
|
|
+
|
|
|
+/**
|
|
|
+ * Returns the object that was used to construct the ProjectDocumentSource
|
|
|
+ * @return {object} the object that was used to construct the ProjectDocumentSource
|
|
|
+ **/
|
|
|
+proto.getRaw = function getRaw() {
|
|
|
+ return this._raw;
|
|
|
+};
|
|
|
+
|
|
|
+proto.toJSON = function toJSON(){
|
|
|
+ var obj = {};
|
|
|
+ this.sourceToJson(obj);
|
|
|
+ return obj;
|
|
|
+};
|