Skip to content

Commit 364fc10

Browse files
committed
Add test for gh-14098
1 parent f94affd commit 364fc10

File tree

1 file changed

+58
-1
lines changed

1 file changed

+58
-1
lines changed

test/model.populate.test.js

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8254,11 +8254,68 @@ describe('model: populate:', function() {
82548254
path: 'companyId',
82558255
justOne: true
82568256
}
8257-
});
8257+
}).lean();
8258+
console.log(populatedRides)
82588259
assert.deepEqual(populatedRides[0].files, []);
82598260
assert.deepEqual(populatedRides[1].files, []);
82608261
});
82618262

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

0 commit comments

Comments
 (0)