| 
					
				 | 
			
			
				@@ -16,6 +16,8 @@ var Pipeline = module.exports = function Pipeline(theCtx){ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 	this.SYNC_MODE = false; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 }, klass = Pipeline, base = Object, proto = klass.prototype = Object.create(base.prototype, {constructor:{value:klass}}); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+var DepsTracker = require("./DepsTracker"); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 var DocumentSource = require("./documentSources/DocumentSource"), 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 	LimitDocumentSource = require('./documentSources/LimitDocumentSource'), 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 	MatchDocumentSource = require('./documentSources/MatchDocumentSource'), 
			 | 
		
	
	
		
			
				| 
					
				 | 
			
			
				@@ -237,39 +239,39 @@ klass.optimizations.sharded.moveFinalUnwindFromShardsToMerger = function moveFin 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  * @param shardPipe shard sources 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  * @param mergePipe merge sources 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  */ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-klass.optimizations.sharded.limitFieldsSentFromShardsToMerger = function limitFieldsSentFromShardsToMerger(shardPipe, mergePipe) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-	var mergeDeps = mergePipe.getDependencies(shardPipe.getInitialQuery()); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-	if (mergeDeps.needWholeDocument) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-		return; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-	} 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-	if (mergeDeps.fields == null) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-		mergeDeps.fields = {}; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-	} 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-	if (mergeDeps.fields.length == 0) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-		mergeDeps.fields["_id"] = 0; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-	} 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-	if (shardPipe.sources == null) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-		shardPipe.sources = {}; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-	} 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-	//NOTE: Deviation from Mongo: not setting mergeDeps.needTextScore because we aren't handling that (Document meta stuff) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				- 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-    // HEURISTIC: only apply optimization if none of the shard stages have an exhaustive list of 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-    // field dependencies. While this may not be 100% ideal in all cases, it is simple and 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-    // avoids the worst cases by ensuring that: 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-    // 1) Optimization IS applied when the shards wouldn't have known their exhaustive list of 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-    //    dependencies. This situation can happen when a $sort is before the first $project or 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-    //    $group. Without the optimization, the shards would have to reify and transmit full 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-    //    objects even though only a subset of fields are needed. 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-    // 2) Optimization IS NOT applied immediately following a $project or $group since it would 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-    //    add an unnecessary project (and therefore a deep-copy). 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-    for (var i = 0; i < shardPipe.sources.length; i++) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        if (shardPipe.sources.getDependencies() & DocumentSource.GetDepsReturn.EXHAUSTIVE_FIELDS) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            return; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-    } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				- 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-    // if we get here, add the project. 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-    shardPipe.sources.push(ProjectDocumentSource.createFromJson({"$project": mergeDeps.toProjection()[0]}, shardPipe.ctx)); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-}; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+// klass.optimizations.sharded.limitFieldsSentFromShardsToMerger = function limitFieldsSentFromShardsToMerger(shardPipe, mergePipe) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+// 	var mergeDeps = mergePipe.getDependencies(shardPipe.getInitialQuery()); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+// 	if (mergeDeps.needWholeDocument) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+// 		return; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+// 	} 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+// 	if (mergeDeps.fields == null) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+// 		mergeDeps.fields = {}; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+// 	} 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+// 	if (mergeDeps.fields.length == 0) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+// 		mergeDeps.fields["_id"] = 0; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+// 	} 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+// 	if (shardPipe.sources == null) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+// 		shardPipe.sources = {}; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+// 	} 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+// 	//NOTE: Deviation from Mongo: not setting mergeDeps.needTextScore because we aren't handling that (Document meta stuff) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+//     // HEURISTIC: only apply optimization if none of the shard stages have an exhaustive list of 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+//     // field dependencies. While this may not be 100% ideal in all cases, it is simple and 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+//     // avoids the worst cases by ensuring that: 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+//     // 1) Optimization IS applied when the shards wouldn't have known their exhaustive list of 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+//     //    dependencies. This situation can happen when a $sort is before the first $project or 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+//     //    $group. Without the optimization, the shards would have to reify and transmit full 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+//     //    objects even though only a subset of fields are needed. 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+//     // 2) Optimization IS NOT applied immediately following a $project or $group since it would 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+//     //    add an unnecessary project (and therefore a deep-copy). 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+//     for (var i = 0; i < shardPipe.sources.length; i++) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+//         if (shardPipe.sources.getDependencies() & DocumentSource.GetDepsReturn.EXHAUSTIVE_FIELDS) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+//             return; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+//     } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+//     // if we get here, add the project. 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+//     shardPipe.sources.push(ProjectDocumentSource.createFromJson({$project: mergeDeps.toProjection()[0]}, shardPipe.ctx)); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+// }; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 /** 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  * Create an `Array` of `DocumentSource`s from the given JSON pipeline 
			 | 
		
	
	
		
			
				| 
					
				 | 
			
			
				@@ -526,7 +528,7 @@ proto.getDependencies = function getDependencies (initialQuery) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     var deps, knowAllFields = false; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     if (this.sources == null || this.sources.length == 0) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-    	return {}; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    	return new DepsTracker(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     //NOTE: Deviation from Mongo -- We aren't using Meta and textscore 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     for (var i=0; i < this.sources.length() && !knowAllFields; i++) { 
			 |