removeDeselectedForeignField.js 743 B

12345678910111213141516171819202122232425262728293031
  1. 'use strict';
  2. const get = require('../get');
  3. const mpath = require('mpath');
  4. const parseProjection = require('../projection/parseProjection');
  5. /*!
  6. * ignore
  7. */
  8. module.exports = function removeDeselectedForeignField(foreignFields, options, docs) {
  9. const projection = parseProjection(get(options, 'select', null), true) ||
  10. parseProjection(get(options, 'options.select', null), true);
  11. if (projection == null) {
  12. return;
  13. }
  14. for (const foreignField of foreignFields) {
  15. if (!projection.hasOwnProperty('-' + foreignField)) {
  16. continue;
  17. }
  18. for (const val of docs) {
  19. if (val.$__ != null) {
  20. mpath.unset(foreignField, val._doc);
  21. } else {
  22. mpath.unset(foreignField, val);
  23. }
  24. }
  25. }
  26. };