Skip to content

Commit

Permalink
Merge branch 'Automattic:master' into patch-1
Browse files Browse the repository at this point in the history
  • Loading branch information
alex-statsig committed Aug 11, 2024
2 parents 8d6422b + f98b463 commit 35e90da
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -4472,7 +4472,7 @@ function _assign(model, vals, mod, assignmentOpts) {
rawOrder[key].push(i);
} else if (isVirtual ||
rawDocs[key].constructor !== val.constructor ||
String(rawDocs[key]._doc._id) !== String(val._doc._id)) {
(rawDocs[key] instanceof Document ? String(rawDocs[key]._doc._id) : String(rawDocs[key]._id)) !== (val instanceof Document ? String(val._doc._id) : String(val._id))) {
// May need to store multiple docs with the same id if there's multiple models
// if we have discriminators or a ref function. But avoid converting to an array
// if we have multiple queries on the same model because of `perDocumentLimit` re: gh-9906
Expand Down
48 changes: 48 additions & 0 deletions test/model.populate.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11048,4 +11048,52 @@ describe('model: populate:', function() {
assert.equal(pet1.owner.name, 'Alice');
assert.equal(pet2.owner.name, 'Alice');
});

it('makes sure that populate works correctly with duplicate foreignField with lean(); (gh-14794)', async function() {
const authorSchema = new mongoose.Schema({
group: String,
name: String
});

const postSchema = new mongoose.Schema({
authorGroup: String,
title: String,
content: String
});

const Author = db.model('Author', authorSchema);
const Post = db.model('Post', postSchema);

await Author.create({ group: 'AUTH1', name: 'John Doe' });
await Author.create({ group: 'AUTH2', name: 'Jane Smith' });
await Author.create({ group: 'AUTH2', name: 'Will Jons' });

await Post.create({
authorGroup: 'AUTH1',
title: 'First Post',
content: 'Content 1'
});

await Post.create({
authorGroup: 'AUTH2',
title: 'Second Post',
content: 'Content 2'
});

const posts = await Post.find()
.populate({
path: 'authorGroup',
model: 'Author',
select: { _id: 1, name: 1 },
foreignField: 'group'
})
.select({ _id: 1, authorGroup: 1, title: 1 })
.lean();

for (const post of posts) {
assert.ok(post.authorGroup._id instanceof mongoose.Types.ObjectId);
assert.ok(typeof post.authorGroup.name === 'string');
}
assert.equal(posts.length, 2);
});
});

0 comments on commit 35e90da

Please sign in to comment.