Skip to content

Commit

Permalink
fix(populate): avoid duplicate ids in dynref queries
Browse files Browse the repository at this point in the history
Fix #5054
  • Loading branch information
vkarpov15 committed Mar 18, 2017
1 parent dae6192 commit 249653b
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions lib/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -3107,7 +3107,7 @@ function populate(model, docs, options, callback) {

assignVals({
originalModel: model,
rawIds: mod.ids,
rawIds: mod.allIds,
localField: mod.localField,
foreignField: mod.foreignField,
rawDocs: rawDocs,
Expand Down Expand Up @@ -3351,6 +3351,8 @@ function getModelsMapForPopulate(model, docs, options) {
var k = modelNames.length;
while (k--) {
modelName = modelNames[k];
var _doc = Array.isArray(doc) && refPath ? doc[k] : doc;
var _ret = Array.isArray(ret) && refPath ? ret[k] : ret;
Model = originalOptions.model && originalOptions.model.modelName ?
originalOptions.model :
model.db.model(modelName);
Expand All @@ -3374,8 +3376,9 @@ function getModelsMapForPopulate(model, docs, options) {
available[modelName] = {
Model: Model,
options: currentOptions,
docs: [doc],
ids: [ret],
docs: [_doc],
ids: [_ret],
allIds: [ret],
// Assume only 1 localField + foreignField
localField: localField,
foreignField: foreignField,
Expand All @@ -3384,8 +3387,9 @@ function getModelsMapForPopulate(model, docs, options) {
};
map.push(available[modelName]);
} else {
available[modelName].docs.push(doc);
available[modelName].ids.push(ret);
available[modelName].docs.push(_doc);
available[modelName].ids.push(_ret);
available[modelName].allIds.push(ret);
}
}
}
Expand Down

0 comments on commit 249653b

Please sign in to comment.