diff --git a/lib/model.js b/lib/model.js index 377b6df40e2..2df50420e98 100644 --- a/lib/model.js +++ b/lib/model.js @@ -4120,10 +4120,11 @@ function populate(model, docs, options, callback) { assignmentOpts.excludeId = excludeIdReg.test(select) || (select && select._id === 0); if (ids.length === 0 || ids.every(utils.isNullOrUndefined)) { - // Ensure that we set populate virtuals with count option to 0 even - // if we don't actually execute a query. + // Ensure that we set populate virtuals to 0 or empty array even + // if we don't actually execute a query because they don't have + // a value by default. See gh-7731, gh-8230 --_remaining; - if (mod.count) { + if (mod.count || mod.isVirtual) { _assign(model, [], mod, assignmentOpts); } continue;