Skip to content

Commit b6f6f37

Browse files
committed
test(populate): repro #8460
1 parent 98724be commit b6f6f37

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

test/model.populate.test.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9000,4 +9000,36 @@ describe('model: populate:', function() {
90009000
assert.equal(res.list[2].data.sourceId, 123);
90019001
});
90029002
});
9003+
9004+
it('excluding foreignField using minus path deselects foreignField (gh-8460)', function() {
9005+
const schema = Schema({ specialId: String });
9006+
9007+
schema.virtual('searchResult', {
9008+
ref: 'gh8460_Result',
9009+
localField: 'specialId',
9010+
foreignField: 'specialId',
9011+
options: { select: 'name -_id -specialId' },
9012+
justOne: true
9013+
});
9014+
const Model = db.model('gh8460_Model', schema);
9015+
const Result = db.model('gh8460_Result', Schema({
9016+
name: String,
9017+
specialId: String,
9018+
other: String
9019+
}));
9020+
9021+
return co(function*() {
9022+
yield Result.create({ name: 'foo', specialId: 'secret', other: 'test' });
9023+
yield Model.create({ specialId: 'secret' });
9024+
9025+
let doc = yield Model.findOne().populate('searchResult');
9026+
assert.strictEqual(doc.searchResult.specialId, void 0);
9027+
9028+
doc = yield Model.findOne().populate({
9029+
path: 'searchResult',
9030+
select: 'name -_id'
9031+
});
9032+
assert.strictEqual(doc.searchResult.specialId, 'secret');
9033+
});
9034+
});
90039035
});

0 commit comments

Comments
 (0)