assignRawDocsToIdStructure.js 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. 'use strict';
  2. const leanPopulateMap = require('./leanPopulateMap');
  3. const modelSymbol = require('../symbols').modelSymbol;
  4. const utils = require('../../utils');
  5. module.exports = assignRawDocsToIdStructure;
  6. /*!
  7. * Assign `vals` returned by mongo query to the `rawIds`
  8. * structure returned from utils.getVals() honoring
  9. * query sort order if specified by user.
  10. *
  11. * This can be optimized.
  12. *
  13. * Rules:
  14. *
  15. * if the value of the path is not an array, use findOne rules, else find.
  16. * for findOne the results are assigned directly to doc path (including null results).
  17. * for find, if user specified sort order, results are assigned directly
  18. * else documents are put back in original order of array if found in results
  19. *
  20. * @param {Array} rawIds
  21. * @param {Array} vals
  22. * @param {Boolean} sort
  23. * @api private
  24. */
  25. function assignRawDocsToIdStructure(rawIds, resultDocs, resultOrder, options, recursed) {
  26. // honor user specified sort order
  27. const newOrder = [];
  28. const sorting = options.sort && rawIds.length > 1;
  29. const nullIfNotFound = options.$nullIfNotFound;
  30. let doc;
  31. let sid;
  32. let id;
  33. for (let i = 0; i < rawIds.length; ++i) {
  34. id = rawIds[i];
  35. if (Array.isArray(id)) {
  36. // handle [ [id0, id2], [id3] ]
  37. assignRawDocsToIdStructure(id, resultDocs, resultOrder, options, true);
  38. newOrder.push(id);
  39. continue;
  40. }
  41. if (id === null && !sorting) {
  42. // keep nulls for findOne unless sorting, which always
  43. // removes them (backward compat)
  44. newOrder.push(id);
  45. continue;
  46. }
  47. sid = String(id);
  48. doc = resultDocs[sid];
  49. // If user wants separate copies of same doc, use this option
  50. if (options.clone && doc != null) {
  51. if (options.lean) {
  52. const _model = leanPopulateMap.get(doc);
  53. doc = utils.clone(doc);
  54. leanPopulateMap.set(doc, _model);
  55. } else {
  56. doc = doc.constructor.hydrate(doc._doc);
  57. }
  58. }
  59. if (recursed) {
  60. if (doc) {
  61. if (sorting) {
  62. newOrder[resultOrder[sid]] = doc;
  63. } else {
  64. newOrder.push(doc);
  65. }
  66. } else if (id != null && id[modelSymbol] != null) {
  67. newOrder.push(id);
  68. } else {
  69. newOrder.push(options.retainNullValues || nullIfNotFound ? null : id);
  70. }
  71. } else {
  72. // apply findOne behavior - if document in results, assign, else assign null
  73. newOrder[i] = doc || null;
  74. }
  75. }
  76. rawIds.length = 0;
  77. if (newOrder.length) {
  78. // reassign the documents based on corrected order
  79. // forEach skips over sparse entries in arrays so we
  80. // can safely use this to our advantage dealing with sorted
  81. // result sets too.
  82. newOrder.forEach(function(doc, i) {
  83. rawIds[i] = doc;
  84. });
  85. }
  86. }