Skip to content

Commit 07dba1b

Browse files
authored
Merge pull request #14099 from csy1204/fix/gh-14098
fix(populate): fix curPath to update appropriately
2 parents f2bbddc + 92f63cf commit 07dba1b

File tree

2 files changed

+54
-1
lines changed

2 files changed

+54
-1
lines changed

lib/helpers/populate/assignVals.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ module.exports = function assignVals(o) {
144144

145145
const parts = _path.split('.');
146146
let cur = docs[i];
147-
const curPath = parts[0];
147+
let curPath = parts[0];
148148
for (let j = 0; j < parts.length - 1; ++j) {
149149
// If we get to an array with a dotted path, like `arr.foo`, don't set
150150
// `foo` on the array.
@@ -167,6 +167,7 @@ module.exports = function assignVals(o) {
167167
cur[parts[j]] = {};
168168
}
169169
cur = cur[parts[j]];
170+
curPath += parts[j + 1] ? `.${parts[j + 1]}` : '';
170171
// If the property in MongoDB is a primitive, we won't be able to populate
171172
// the nested path, so skip it. See gh-7545
172173
if (typeof cur !== 'object') {

test/model.populate.test.js

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8259,6 +8259,58 @@ describe('model: populate:', function() {
82598259
assert.deepEqual(populatedRides[1].files, []);
82608260
});
82618261

8262+
it('doesnt insert empty document when lean populating a path within an underneath non-existent document array (gh-14098)', async function() {
8263+
const userSchema = new mongoose.Schema({
8264+
fullName: String,
8265+
company: String
8266+
});
8267+
const User = db.model('User', userSchema);
8268+
8269+
const fileSchema = new mongoose.Schema({
8270+
_id: String,
8271+
uploaderId: {
8272+
type: mongoose.ObjectId,
8273+
ref: 'User'
8274+
}
8275+
}, { toObject: { virtuals: true }, toJSON: { virtuals: true } });
8276+
fileSchema.virtual('uploadedBy', {
8277+
ref: 'User',
8278+
localField: 'uploaderId',
8279+
foreignField: '_id',
8280+
justOne: true
8281+
});
8282+
8283+
const contentSchema = new mongoose.Schema({
8284+
memo: String,
8285+
files: { type: [fileSchema], default: [] }
8286+
}, { toObject: { virtuals: true }, toJSON: { virtuals: true }, _id: false });
8287+
8288+
const postSchema = new mongoose.Schema({
8289+
title: String,
8290+
content: { type: contentSchema }
8291+
}, { toObject: { virtuals: true }, toJSON: { virtuals: true } });
8292+
const Post = db.model('Test1', postSchema);
8293+
8294+
const user = await User.create({ fullName: 'John Doe', company: 'GitHub' });
8295+
await Post.create([
8296+
{ title: 'London-Paris' },
8297+
{
8298+
title: 'Berlin-Moscow',
8299+
content: {
8300+
memo: 'Not Easy',
8301+
files: [{ _id: '123', uploaderId: user._id }]
8302+
}
8303+
}
8304+
]);
8305+
await Post.updateMany({}, { $unset: { 'content.files': 1 } });
8306+
const populatedRides = await Post.find({}).populate({
8307+
path: 'content.files.uploadedBy',
8308+
justOne: true
8309+
}).lean();
8310+
assert.equal(populatedRides[0].content.files, undefined);
8311+
assert.equal(populatedRides[1].content.files, undefined);
8312+
});
8313+
82628314
it('sets empty array if populating undefined path (gh-8455)', async function() {
82638315
const TestSchema = new Schema({
82648316
thingIds: [mongoose.ObjectId]

0 commit comments

Comments
 (0)