Skip to content

Commit

Permalink
fix(queryhelpers): avoid path collision error when projecting in disc…
Browse files Browse the repository at this point in the history
…riminator key with `.$`

Re: #9361
  • Loading branch information
vkarpov15 committed Aug 27, 2020
1 parent 8c8751f commit d48cfa1
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 8 deletions.
2 changes: 1 addition & 1 deletion lib/queryhelpers.js
Expand Up @@ -261,7 +261,7 @@ exports.applyPaths = function applyPaths(fields, schema) {
let cur = '';
for (let i = 0; i < pieces.length; ++i) {
cur += (cur.length === 0 ? '' : '.') + pieces[i];
const projection = get(fields, cur, false);
const projection = get(fields, cur, false) || get(fields, cur + '.$', false);
if (projection && typeof projection !== 'object') {
return;
}
Expand Down
18 changes: 11 additions & 7 deletions test/model.discriminator.test.js
Expand Up @@ -762,31 +762,35 @@ describe('model', function() {

it('with $meta projection (gh-5859)', function() {
const eventSchema = new Schema({ eventField: String }, { id: false });
eventSchema.index({ eventField: 'text' });
const Event = db.model('Test', eventSchema);

const trackSchema = new Schema({ trackField: String });
const Track = Event.discriminator('Track', trackSchema);

const trackedItem = new Track({
trackField: 'trackField',
eventField: 'eventField'
trackField: 'track',
eventField: 'event'
});

return trackedItem.save().
then(() => Event.init()).
then(function() {
return Event.find({}).select({ score: { $meta: 'textScore' } });
return Event.find({ $text: { $search: 'event' } }).
select({ score: { $meta: 'textScore' } });
}).
then(function(docs) {
assert.equal(docs.length, 1);
assert.equal(docs[0].trackField, 'trackField');
assert.equal(docs[0].trackField, 'track');
}).
then(function() {
return Track.find({}).select({ score: { $meta: 'textScore' } });
return Track.find({ $text: { $search: 'event' } }).
select({ score: { $meta: 'textScore' } });
}).
then(function(docs) {
assert.equal(docs.length, 1);
assert.equal(docs[0].trackField, 'trackField');
assert.equal(docs[0].eventField, 'eventField');
assert.equal(docs[0].trackField, 'track');
assert.equal(docs[0].eventField, 'event');
});
});

Expand Down

0 comments on commit d48cfa1

Please sign in to comment.