Document.js 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. "use strict";
  2. var Document = module.exports = (function(){
  3. // CONSTRUCTOR
  4. /**
  5. * Represents a `Document` (i.e., an `Object`) in `mongo` but in `munge` this is only a set of static helpers since we treat all `Object`s like `Document`s.
  6. *
  7. * @class Document
  8. * @namespace mungedb.aggregate.pipeline
  9. * @module mungedb-aggregate
  10. * @constructor
  11. **/
  12. var klass = function Document(){
  13. if(this.constructor == Document) throw new Error("Never create instances! Use static helpers only.");
  14. }, base = Object, proto = klass.prototype = Object.create(base.prototype, {constructor:{value:klass}});
  15. // DEPENDENCIES
  16. var Value = require("./Value");
  17. // STATIC MEMBERS
  18. /**
  19. * Shared "_id"
  20. * @static
  21. * @property ID_PROPERTY_NAME
  22. **/
  23. klass.ID_PROPERTY_NAME = "_id";
  24. /**
  25. * Compare two documents.
  26. *
  27. * BSON document field order is significant, so this just goes through the fields in order.
  28. * The comparison is done in roughly the same way as strings are compared, but comparing one field at a time instead of one character at a time.
  29. *
  30. * @static
  31. * @method compare
  32. * @param rL left document
  33. * @param rR right document
  34. * @returns an integer less than zero, zero, or an integer greater than zero, depending on whether rL < rR, rL == rR, or rL > rR
  35. **/
  36. klass.compare = function compare(l, r){ //TODO: might be able to replace this with a straight compare of docs using JSON.stringify()
  37. var lPropNames = Object.getOwnPropertyNames(l),
  38. lPropNamesLength = lPropNames.length,
  39. rPropNames = Object.getOwnPropertyNames(r),
  40. rPropNamesLength = rPropNames.length;
  41. for(var i = 0; true; ++i) {
  42. if (i >= lPropNamesLength) {
  43. if (i >= rPropNamesLength) return 0; // documents are the same length
  44. return -1; // left document is shorter
  45. }
  46. if (i >= rPropNamesLength) return 1; // right document is shorter
  47. var nameCmp = Value.compare(lPropNames[i], rPropNames[i]);
  48. if (nameCmp !== 0) return nameCmp; // field names are unequal
  49. var valueCmp = Value.compare(l[lPropNames[i]], r[rPropNames[i]]);
  50. if (valueCmp) return valueCmp; // fields are unequal
  51. }
  52. /* NOTREACHED */
  53. throw new Error("This should never happen"); //verify(false)
  54. // return 0;
  55. };
  56. /**
  57. * Clone a document
  58. * @static
  59. * @method clone
  60. * @param document
  61. **/
  62. klass.clone = function(document){
  63. var obj = {};
  64. for(var key in document){
  65. if(document.hasOwnProperty(key)){
  66. var withObjVal = document[key];
  67. if(withObjVal.constructor === Object){
  68. obj[key] = Document.clone(withObjVal);
  69. }else{
  70. obj[key] = withObjVal;
  71. }
  72. }
  73. }
  74. return obj;
  75. };
  76. // proto.addField = function addField(){ throw new Error("Instead of `Document#addField(key,val)` you should just use `obj[key] = val`"); }
  77. // proto.setField = function addField(){ throw new Error("Instead of `Document#setField(key,val)` you should just use `obj[key] = val`"); }
  78. // proto.getField = function getField(){ throw new Error("Instead of `Document#getField(key)` you should just use `var val = obj[key];`"); }
  79. return klass;
  80. })();