Skip to content

Commit

Permalink
fix(populate): avoid populating single nested subdocs underneath arra…
Browse files Browse the repository at this point in the history
…ys if there's no `ref`

Fix #11538
Re: #10856
  • Loading branch information
vkarpov15 committed Apr 16, 2022
1 parent fcd9100 commit b56cd4a
Showing 1 changed file with 13 additions and 10 deletions.
23 changes: 13 additions & 10 deletions lib/helpers/populate/getModelsMapForPopulate.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,17 +76,20 @@ module.exports = function getModelsMapForPopulate(model, docs, options) {
let schemaOptions = null;
let modelNamesInOrder = null;

if (schema != null && schema.instance === 'Embedded' && schema.options.ref) {
const data = {
localField: options.path + '._id',
foreignField: '_id',
justOne: true
};
const res = _getModelNames(doc, schema, modelNameFromQuery, model);
if (schema != null && schema.instance === 'Embedded') {
if (schema.options.ref) {
const data = {
localField: options.path + '._id',
foreignField: '_id',
justOne: true
};
const res = _getModelNames(doc, schema, modelNameFromQuery, model);

const unpopulatedValue = mpath.get(options.path, doc);
const id = mpath.get('_id', unpopulatedValue);
addModelNamesToMap(model, map, available, res.modelNames, options, data, id, doc, schemaOptions, unpopulatedValue);
const unpopulatedValue = mpath.get(options.path, doc);
const id = mpath.get('_id', unpopulatedValue);
addModelNamesToMap(model, map, available, res.modelNames, options, data, id, doc, schemaOptions, unpopulatedValue);
}
// No-op if no `ref` set. See gh-11538
continue;
}

Expand Down

0 comments on commit b56cd4a

Please sign in to comment.