MCPcopy Create free account
hub / github.com/Automattic/mongoose / _execPopulateQuery

Function _execPopulateQuery

lib/model.js:4811–4939  ·  view source on GitHub ↗
(mod, match, select)

Source from the content-addressed store, hash-verified

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

Callers

nothing calls this directly

Calls 4

populateMethod · 0.80
setMethod · 0.80
findMethod · 0.65
cloneFunction · 0.50

Tested by

no test coverage detected