objectid.js 794 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. /**
  2. * ObjectId type constructor
  3. *
  4. * #### Example
  5. *
  6. * const id = new mongoose.Types.ObjectId;
  7. *
  8. * @constructor ObjectId
  9. */
  10. 'use strict';
  11. const ObjectId = require('../driver').get().ObjectId;
  12. const objectIdSymbol = require('../helpers/symbols').objectIdSymbol;
  13. /*!
  14. * Getter for convenience with populate, see gh-6115
  15. */
  16. Object.defineProperty(ObjectId.prototype, '_id', {
  17. enumerable: false,
  18. configurable: true,
  19. get: function() {
  20. return this;
  21. }
  22. });
  23. /*!
  24. * Convenience `valueOf()` to allow comparing ObjectIds using double equals re: gh-7299
  25. */
  26. if (!ObjectId.prototype.hasOwnProperty('valueOf')) {
  27. ObjectId.prototype.valueOf = function objectIdValueOf() {
  28. return this.toString();
  29. };
  30. }
  31. ObjectId.prototype[objectIdSymbol] = true;
  32. module.exports = ObjectId;