Source: node_modules/bson/lib/bson/db_ref.js

  1. 'use strict';
  2. /**
  3. * A class representation of the BSON DBRef type.
  4. *
  5. * @class
  6. * @param {string} collection the collection name.
  7. * @param {ObjectID} oid the reference ObjectID.
  8. * @param {string} [db] optional db name, if omitted the reference is local to the current db.
  9. * @return {DBRef}
  10. */
  11. function DBRef(collection, oid, db, fields) {
  12. if (!(this instanceof DBRef)) return new DBRef(collection, oid, db, fields);
  13. // check if namespace has been provided
  14. var parts = collection.split('.');
  15. if (parts.length === 2) {
  16. db = parts.shift();
  17. collection = parts.shift();
  18. }
  19. this._bsontype = 'DBRef';
  20. this.collection = collection;
  21. this.oid = oid;
  22. this.db = db;
  23. this.fields = fields || {};
  24. }
  25. /**
  26. * @ignore
  27. * @api private
  28. */
  29. DBRef.prototype.toJSON = function() {
  30. var o = {
  31. $ref: this.collection,
  32. $id: this.oid
  33. };
  34. if (this.db != null) o.$db = this.db;
  35. o = Object.assign(o, this.fields);
  36. return o;
  37. };
  38. module.exports = DBRef;
  39. module.exports.DBRef = DBRef;