Skip to content

Commit af7ae41

Browse files
committed
Merge branch 'master' into vkarpov15/gh-13424
2 parents ac302c4 + 360b417 commit af7ae41

File tree

3 files changed

+54
-2
lines changed

3 files changed

+54
-2
lines changed

docs/compatibility.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ Below are the [semver](http://semver.org/) ranges representing which versions of
1818

1919
| MongoDB Server | Mongoose |
2020
| :------------: | :-------------------------------------: |
21-
| `7.x` | `^7.4.0 | ^8.0.0` |
21+
| `7.x` | `^7.4.0 \| ^8.0.0` |
2222
| `6.x` | `^6.5.0 \| ^7.0.0 \| ^8.0.0` |
2323
| `5.x` | `^6.0.0 \| ^7.0.0 \| ^8.0.0` |
2424
| `4.4.x` | `^5.10.0 \| ^6.0.0 \| ^7.0.0 \| ^8.0.0` |

lib/helpers/populate/assignVals.js

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

145145
const parts = _path.split('.');
146146
let cur = docs[i];
147-
const curPath = parts[0];
148147
for (let j = 0; j < parts.length - 1; ++j) {
149148
// If we get to an array with a dotted path, like `arr.foo`, don't set
150149
// `foo` on the array.
@@ -160,6 +159,7 @@ module.exports = function assignVals(o) {
160159
// If nothing to set, avoid creating an unnecessary array. Otherwise
161160
// we'll end up with a single doc in the array with only defaults.
162161
// See gh-8342, gh-8455
162+
const curPath = parts.slice(0, j + 1).join('.');
163163
const schematype = originalSchema._getSchema(curPath);
164164
if (valueToSet == null && schematype != null && schematype.$isMongooseArray) {
165165
break;

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)