OutDocumentSource.js 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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 = function() {
  23. throw new Error("not implemented yet");
  24. };
  25. proto.serialize = function(explain) {
  26. var doc = {},
  27. srcNm = this.getSourceName();
  28. doc[srcNm] = this._collectionName;
  29. return doc;
  30. };
  31. proto.getOutputNs = function() {
  32. return this._outputNs;
  33. };
  34. klass.createFromJson = function(jsonElement, ctx) {
  35. if (typeof jsonElement !== "string")
  36. throw new Error("code 16990; $out only supports a string argument, not " + typeof jsonElement);
  37. var out = new OutDocumentSource(null, ctx); // TODO: outputNs
  38. out._collectionName = jsonElement;
  39. return out;
  40. };
  41. // SplittableDocumentSource implementation.
  42. // Mongo doesn't fully implement SplittableDocumentSource on DocumentSourceOut.
  43. // It doesn't implement getShardSource or getMergeSource
  44. klass.isSplittableDocumentSource = true;
  45. proto.getShardSource = function getShardSource() {
  46. return null;
  47. };
  48. proto.getMergeSource = function getMergeSource() {
  49. return this;
  50. };
  51. //NeedsMongodDocumentSource implementation
  52. klass.needsMongodDocumentSource = true;
  53. proto.getDependencies = function(deps) {
  54. deps.needWholeDocument = true;
  55. return DocumentSource.GetDepsReturn.EXHAUSTIVE_ALL;
  56. };