OutDocumentSource.js 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. "use strict";
  2. var DocumentSource = require("./DocumentSource");
  3. /**
  4. * @class OutDocumentSource
  5. * @namespace mungedb-aggregate.pipeline.documentSources
  6. * @module mungedb-aggregate
  7. * @constructor
  8. * @param [ctx] {ExpressionContext}
  9. **/
  10. var OutDocumentSource = module.exports = function OutDocumentSource(outputNs, ctx){
  11. if (arguments.length > 2) throw new Error("up to two args expected");
  12. base.call(this, ctx);
  13. // defaults
  14. this._done = false;
  15. this._outputNs = outputNs;
  16. this._collectionName = "";
  17. }, klass = OutDocumentSource, base = DocumentSource, proto = klass.prototype = Object.create(base.prototype, {constructor:{value:klass}}); //jshint ignore:line
  18. klass.outName = "$out";
  19. proto.getSourceName = function() {
  20. return klass.outName;
  21. };
  22. proto.getNext = DocumentSource.GET_NEXT_PASS_THROUGH;
  23. proto.serialize = function(explain) {
  24. var doc = {},
  25. srcNm = this.getSourceName();
  26. doc[srcNm] = this._collectionName;
  27. return doc;
  28. };
  29. proto.getOutputNs = function() {
  30. return this._outputNs;
  31. };
  32. klass.createFromJson = function(jsonElement, ctx) {
  33. if (typeof jsonElement !== "string")
  34. throw new Error("code 16990; $out only supports a string argument, not " + typeof jsonElement);
  35. var out = new OutDocumentSource(null, ctx); // TODO: outputNs
  36. out._collectionName = jsonElement;
  37. return out;
  38. };
  39. // SplittableDocumentSource implementation.
  40. // Mongo doesn't fully implement SplittableDocumentSource on DocumentSourceOut.
  41. // It doesn't implement getShardSource or getMergeSource
  42. klass.isSplittableDocumentSource = true;
  43. proto.getShardSource = function getShardSource() {
  44. return null;
  45. };
  46. proto.getMergeSource = function getMergeSource() {
  47. return this;
  48. };
  49. //NeedsMongodDocumentSource implementation
  50. klass.needsMongodDocumentSource = true;
  51. proto.getDependencies = function(deps) {
  52. deps.needWholeDocument = true;
  53. return DocumentSource.GetDepsReturn.EXHAUSTIVE_ALL;
  54. };