Преглед изворни кода

#2067: Added getDependencies to

Jared Hall пре 13 година
родитељ
комит
fd666e6c3f

+ 1 - 1
lib/pipeline/PipelineD.js

@@ -39,7 +39,7 @@ var PipelineD = module.exports = (function(){
         var projection = {};
         var deps = [];
         var status = DocumentSource.GetDepsReturn.SEE_NEXT;
-        for (var i=0; i < sources.length && status == DocumentSource.GetDepsReturn.SEE_NEXT; i++) {
+        for (var i=0; i < sources.length && status != DocumentSource.GetDepsReturn.EXHAUSTIVE; i++) {
             status = sources[i].getDependencies(deps);
         }
         if (status == DocumentSource.GetDepsReturn.EXHAUSTIVE) {

+ 11 - 1
lib/pipeline/documentSources/GroupDocumentSource.js

@@ -183,7 +183,17 @@ var GroupDocumentSource = module.exports = (function(){
 		return this.currentDocument;
 	};
 
-
+	proto.getDependencies = function getDependencies(deps) {
+		var self = this;
+		// add _id
+		this.idExpression.addDependencies(deps);
+		// add the rest
+		this.fieldNames.forEach(function(field, i) {
+			self.expressions[i].addDependencies(deps);
+		});
+
+		return DocumentSource.GetDepsReturn.EXHAUSTIVE;
+	};
 	
 
 	proto.addAccumulator = function addAccumulator(fieldName, accumulatorFactory, expression){

+ 44 - 0
test/lib/pipeline/PipelineD.js

@@ -99,6 +99,50 @@ module.exports = {
 					cs = PipelineD.prepareCursorSource(p, [1,2,3,4,5]);
 				
 				assert.deepEqual(cs._projection, {"_id":0,"testDep":1});
+			},
+
+			"should get projection's deps": function(){
+				var cmdObj = [
+					{$match:{
+						x:{$exists:true},
+						y:{$exists:false}
+					}},
+					{$project:{
+						a:"$a.b.c",
+						b:"$d",
+						c:"$e.f.g"
+					}},
+					{$group:{
+						_id:"$a",
+						x:{$push:"b"}
+					}}
+				];
+				var p = Pipeline.parseCommand(cmdObj),
+					cs = PipelineD.prepareCursorSource(p, [1,2,3,4,5]);
+					assert.equal(JSON.stringify(cs._projection), JSON.stringify({ 'a.b.c': 1, d: 1, 'e.f.g': 1, _id: 1 }));
+			},
+
+			"should get group's deps": function(){
+				var cmdObj = [
+					{$match:{
+						x:{$exists:true},
+						y:{$exists:false}
+					}},
+					{$group:{
+						_id:"$a",
+						x:{$push:"$b"},
+						y:{$addToSet:"$x.y.z"},
+						z:{$sum:"$x.y.z.w"}
+					}},
+					{$project:{
+						a:"$a.b.c",
+						b:"$d",
+						c:"$e.f.g"
+					}}
+				];
+				var p = Pipeline.parseCommand(cmdObj),
+					cs = PipelineD.prepareCursorSource(p, [1,2,3,4,5]);
+					assert.equal(JSON.stringify(cs._projection), JSON.stringify({ _id: 0, a: 1, b: 1, 'x.y.z': 1 }));
 			}
 		}
 	}