Skip to content

Commit

Permalink
test(populate): repro #9913
Browse files Browse the repository at this point in the history
  • Loading branch information
vkarpov15 committed Feb 16, 2021
1 parent 18e5db1 commit 881ee62
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions test/model.populate.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9909,6 +9909,46 @@ describe('model: populate:', function() {
});
});

it('sets not-found values to null for paths that are not in the schema (gh-9913)', function() {
const Books = db.model('books', new Schema({ name: String, tags: [{ type: 'ObjectId', ref: 'tags' }] }));
const Tags = db.model('tags', new Schema({ authors: [{ author: 'ObjectId' }] }));
const Authors = db.model('authors', new Schema({ name: String }));

return co(function*() {
const anAuthor = new Authors({ name: 'Author1' });
yield anAuthor.save();

const aTag = new Tags({ authors: [{ author: anAuthor.id }, { author: new mongoose.Types.ObjectId() }] });
yield aTag.save();

const aBook = new Books({ name: 'Book1', tags: [aTag.id] });
yield aBook.save();

const aggregateOptions = [
{ $match: {
name: { $in: [aBook.name] }
} },
{ $lookup: {
from: 'tags',
localField: 'tags',
foreignField: '_id',
as: 'tags'
} }
];
const books = yield Books.aggregate(aggregateOptions).exec();

const populateOptions = [{
path: 'tags.authors.author',
model: 'authors',
select: '_id name'
}];

const populatedBooks = yield Books.populate(books, populateOptions);
assert.strictEqual(populatedBooks[0].tags[0].authors[0].author.name, 'Author1');
assert.strictEqual(populatedBooks[0].tags[0].authors[1].author, null);
});
});

it('handles perDocumentLimit where multiple documents reference the same populated doc (gh-9906)', function() {
const postSchema = new Schema({
title: String,
Expand Down

0 comments on commit 881ee62

Please sign in to comment.