selectPopulatedFields.js 1018 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. 'use strict';
  2. /*!
  3. * ignore
  4. */
  5. module.exports = function selectPopulatedFields(query) {
  6. var opts = query._mongooseOptions;
  7. if (opts.populate != null) {
  8. var paths = Object.keys(opts.populate);
  9. var i;
  10. var userProvidedFields = query._userProvidedFields || {};
  11. if (query.selectedInclusively()) {
  12. for (i = 0; i < paths.length; ++i) {
  13. if (!isPathInFields(userProvidedFields, paths[i])) {
  14. query.select(paths[i]);
  15. }
  16. }
  17. } else if (query.selectedExclusively()) {
  18. for (i = 0; i < paths.length; ++i) {
  19. if (userProvidedFields[paths[i]] == null) {
  20. delete query._fields[paths[i]];
  21. }
  22. }
  23. }
  24. }
  25. };
  26. /*!
  27. * ignore
  28. */
  29. function isPathInFields(userProvidedFields, path) {
  30. var pieces = path.split('.');
  31. var len = pieces.length;
  32. var cur = pieces[0];
  33. for (var i = 1; i < len; ++i) {
  34. if (userProvidedFields[cur] != null) {
  35. return true;
  36. }
  37. cur += '.' + pieces[i];
  38. }
  39. return userProvidedFields[cur] != null;
  40. }