OutDocumentSource.js 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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}});
  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. klass.isSplittableDocumentSource = true;
  41. //NeedsMongodDocumentSource implementation
  42. klass.needsMongodDocumentSource = true;
  43. proto.getDependencies = function(deps) {
  44. deps.needWholeDocument = true;
  45. return DocumentSource.GetDepsReturn.EXHAUSTIVE;
  46. };