|
@@ -18,9 +18,12 @@ var ProjectDocumentSource = module.exports = function ProjectDocumentSource(ctx)
|
|
|
}, klass = ProjectDocumentSource, base = require('./DocumentSource'), proto = klass.prototype = Object.create(base.prototype, {constructor:{value:klass}});
|
|
}, klass = ProjectDocumentSource, base = require('./DocumentSource'), proto = klass.prototype = Object.create(base.prototype, {constructor:{value:klass}});
|
|
|
|
|
|
|
|
// DEPENDENCIES
|
|
// DEPENDENCIES
|
|
|
-var Expression = require('../expressions/Expression');
|
|
|
|
|
-var ObjectExpression = require('../expressions/ObjectExpression');
|
|
|
|
|
-var Value = require('../Value');
|
|
|
|
|
|
|
+var Expression = require('../expressions/Expression'),
|
|
|
|
|
+ ObjectExpression = require('../expressions/ObjectExpression'),
|
|
|
|
|
+ Value = require('../Value'),
|
|
|
|
|
+ Variables = require('../expressions/Variables'),
|
|
|
|
|
+ VariablesIdGenerator = require('../expressions/VariablesIdGenerator'),
|
|
|
|
|
+ VariablesParseState = require('../expressions/VariablesParseState');
|
|
|
|
|
|
|
|
klass.projectName = "$project";
|
|
klass.projectName = "$project";
|
|
|
|
|
|
|
@@ -39,7 +42,7 @@ proto.getNext = function getNext(callback) {
|
|
|
|
|
|
|
|
this.source.getNext(function(err, input) {
|
|
this.source.getNext(function(err, input) {
|
|
|
if (err)
|
|
if (err)
|
|
|
- return callback(null, err);
|
|
|
|
|
|
|
+ return callback(null, err);
|
|
|
|
|
|
|
|
if (input === DocumentSource.EOF)
|
|
if (input === DocumentSource.EOF)
|
|
|
return callback(null, DocumentSource.EOF);
|
|
return callback(null, DocumentSource.EOF);
|
|
@@ -47,13 +50,15 @@ proto.getNext = function getNext(callback) {
|
|
|
/* create the result document */
|
|
/* create the result document */
|
|
|
var out = {};
|
|
var out = {};
|
|
|
|
|
|
|
|
- /*
|
|
|
|
|
- * Use the ExpressionObject to create the base result.
|
|
|
|
|
- *
|
|
|
|
|
- * If we're excluding fields at the top level, leave out the _id if
|
|
|
|
|
- * it is found, because we took care of it above.
|
|
|
|
|
- */
|
|
|
|
|
- self.OE.addToDocument(out, input); // TODO: 'Variables' stuffs
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * Use the ExpressionObject to create the base result.
|
|
|
|
|
+ *
|
|
|
|
|
+ * If we're excluding fields at the top level, leave out the _id if
|
|
|
|
|
+ * it is found, because we took care of it above.
|
|
|
|
|
+ **/
|
|
|
|
|
+ self._variables.setRoot(input);
|
|
|
|
|
+ self.OE.addToDocument(out, input, self._variables);
|
|
|
|
|
+ self._variables.clearRoot();
|
|
|
|
|
|
|
|
return callback(null, out);
|
|
return callback(null, out);
|
|
|
});
|
|
});
|
|
@@ -105,18 +110,24 @@ proto.sourceToJson = function sourceToJson(builder, explain) {
|
|
|
**/
|
|
**/
|
|
|
klass.createFromJson = function(jsonElement, expCtx) {
|
|
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);
|
|
if (!(jsonElement instanceof Object) || jsonElement.constructor !== Object) throw new Error('Error 15969. Specification must be an object but was ' + typeof jsonElement);
|
|
|
|
|
+
|
|
|
var objectContext = new Expression.ObjectCtx({
|
|
var objectContext = new Expression.ObjectCtx({
|
|
|
isDocumentOk: true,
|
|
isDocumentOk: true,
|
|
|
isTopLevel: true,
|
|
isTopLevel: true,
|
|
|
isInclusionOk: true
|
|
isInclusionOk: true
|
|
|
});
|
|
});
|
|
|
- var project = new ProjectDocumentSource(expCtx);
|
|
|
|
|
|
|
+
|
|
|
|
|
+ var project = new ProjectDocumentSource(expCtx),
|
|
|
|
|
+ idGenerator = new VariablesIdGenerator(),
|
|
|
|
|
+ vps = new VariablesParseState(idGenerator);
|
|
|
|
|
+
|
|
|
project._raw = jsonElement;
|
|
project._raw = jsonElement;
|
|
|
- var parsed = Expression.parseObject(jsonElement, objectContext);
|
|
|
|
|
|
|
+ var parsed = Expression.parseObject(jsonElement, objectContext, vps);
|
|
|
var exprObj = parsed;
|
|
var exprObj = parsed;
|
|
|
if (!exprObj instanceof ObjectExpression) throw new Error("16402, parseObject() returned wrong type of Expression");
|
|
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");
|
|
if (!exprObj.getFieldCount()) throw new Error("16403, $projection requires at least one output field");
|
|
|
project.OE = exprObj;
|
|
project.OE = exprObj;
|
|
|
|
|
+ project._variables = new Variables(idGenerator.getIdCount());
|
|
|
return project;
|
|
return project;
|
|
|
};
|
|
};
|
|
|
|
|
|