geo_haystack_search.js 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. 'use strict';
  2. const Aspect = require('./operation').Aspect;
  3. const defineAspects = require('./operation').defineAspects;
  4. const OperationBase = require('./operation').OperationBase;
  5. const decorateCommand = require('../utils').decorateCommand;
  6. const decorateWithReadConcern = require('../utils').decorateWithReadConcern;
  7. const executeCommand = require('./db_ops').executeCommand;
  8. const handleCallback = require('../utils').handleCallback;
  9. const resolveReadPreference = require('../utils').resolveReadPreference;
  10. const toError = require('../utils').toError;
  11. /**
  12. * Execute a geo search using a geo haystack index on a collection.
  13. *
  14. * @class
  15. * @property {Collection} a Collection instance.
  16. * @property {number} x Point to search on the x axis, ensure the indexes are ordered in the same order.
  17. * @property {number} y Point to search on the y axis, ensure the indexes are ordered in the same order.
  18. * @property {object} [options] Optional settings. See Collection.prototype.geoHaystackSearch for a list of options.
  19. */
  20. class GeoHaystackSearchOperation extends OperationBase {
  21. /**
  22. * Construct a GeoHaystackSearch operation.
  23. *
  24. * @param {Collection} a Collection instance.
  25. * @param {number} x Point to search on the x axis, ensure the indexes are ordered in the same order.
  26. * @param {number} y Point to search on the y axis, ensure the indexes are ordered in the same order.
  27. * @param {object} [options] Optional settings. See Collection.prototype.geoHaystackSearch for a list of options.
  28. */
  29. constructor(collection, x, y, options) {
  30. super(options);
  31. this.collection = collection;
  32. this.x = x;
  33. this.y = y;
  34. }
  35. /**
  36. * Execute the operation.
  37. *
  38. * @param {Collection~resultCallback} [callback] The command result callback
  39. */
  40. execute(callback) {
  41. const coll = this.collection;
  42. const x = this.x;
  43. const y = this.y;
  44. let options = this.options;
  45. // Build command object
  46. let commandObject = {
  47. geoSearch: coll.collectionName,
  48. near: [x, y]
  49. };
  50. // Remove read preference from hash if it exists
  51. commandObject = decorateCommand(commandObject, options, ['readPreference', 'session']);
  52. options = Object.assign({}, options);
  53. // Ensure we have the right read preference inheritance
  54. options.readPreference = resolveReadPreference(coll, options);
  55. // Do we have a readConcern specified
  56. decorateWithReadConcern(commandObject, coll, options);
  57. // Execute the command
  58. executeCommand(coll.s.db, commandObject, options, (err, res) => {
  59. if (err) return handleCallback(callback, err);
  60. if (res.err || res.errmsg) handleCallback(callback, toError(res));
  61. // should we only be returning res.results here? Not sure if the user
  62. // should see the other return information
  63. handleCallback(callback, null, res);
  64. });
  65. }
  66. }
  67. defineAspects(GeoHaystackSearchOperation, Aspect.READ_OPERATION);
  68. module.exports = GeoHaystackSearchOperation;