| 4821 | */ |
| 4822 | |
| 4823 | function _execPopulateQuery(mod, match, select) { |
| 4824 | // `null` match means `mod`'s documents have no ids to query, possible if a large populate |
| 4825 | // was split into a query per document (gh-5890). Skip executing a query: `_assign()` still |
| 4826 | // sets the documents' populated paths to the appropriate default value. |
| 4827 | if (match == null) { |
| 4828 | return Promise.resolve({ docs: [], deferredPopulates: [] }); |
| 4829 | } |
| 4830 | let subPopulate = clone(mod.options.populate); |
| 4831 | const queryOptions = {}; |
| 4832 | if (mod.options.skip !== undefined) { |
| 4833 | queryOptions.skip = mod.options.skip; |
| 4834 | } |
| 4835 | if (mod.options.limit !== undefined) { |
| 4836 | queryOptions.limit = mod.options.limit; |
| 4837 | } |
| 4838 | if (mod.options.perDocumentLimit !== undefined) { |
| 4839 | queryOptions.perDocumentLimit = mod.options.perDocumentLimit; |
| 4840 | } |
| 4841 | Object.assign(queryOptions, mod.options.options); |
| 4842 | // Inherit the parent query's read settings unless populate options set them, see `setInheritedReadOptions()` |
| 4843 | if (queryOptions.readPreference == null && mod.options._inheritedReadPreference != null) { |
| 4844 | queryOptions.readPreference = mod.options._inheritedReadPreference; |
| 4845 | } |
| 4846 | if (queryOptions.readConcern == null && mod.options._inheritedReadConcern != null) { |
| 4847 | queryOptions.readConcern = mod.options._inheritedReadConcern; |
| 4848 | } |
| 4849 | |
| 4850 | if (mod.count) { |
| 4851 | delete queryOptions.skip; |
| 4852 | // Sort doesn't affect the count, so skip the ref model's `defaultSort` |
| 4853 | queryOptions.sort ??= {}; |
| 4854 | } |
| 4855 | |
| 4856 | if (queryOptions.perDocumentLimit != null) { |
| 4857 | queryOptions.limit = queryOptions.perDocumentLimit; |
| 4858 | delete queryOptions.perDocumentLimit; |
| 4859 | } else if (queryOptions.limit != null) { |
| 4860 | queryOptions.limit = queryOptions.limit * mod.ids.length; |
| 4861 | } |
| 4862 | |
| 4863 | // When there's a per-document match function, defer any populate (including from hooks) |
| 4864 | // until after sift filtering, otherwise sift compares ObjectIds against populated docs. |
| 4865 | if (Array.isArray(mod.match)) { |
| 4866 | queryOptions._deferPopulate = true; |
| 4867 | } |
| 4868 | |
| 4869 | const query = mod.model.find(match, select, queryOptions); |
| 4870 | // If we're doing virtual populate and projection is inclusive and foreign |
| 4871 | // field is not selected, automatically select it because mongoose needs it. |
| 4872 | // If projection is exclusive and client explicitly unselected the foreign |
| 4873 | // field, that's the client's fault. |
| 4874 | for (const foreignField of mod.foreignField) { |
| 4875 | if (foreignField !== '_id' && |
| 4876 | query.selectedInclusively() && |
| 4877 | !isPathSelectedInclusive(query._fields, foreignField)) { |
| 4878 | query.select(foreignField); |
| 4879 | } |
| 4880 | } |