Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions lib/helpers/projection/parseProjection.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,20 @@ module.exports = function parseProjection(v, retainMinusPaths) {
ret[field] = include;
}

removeConflictingProjections(ret);

return ret;
};

function removeConflictingProjections(projection) {
const keys = Object.keys(projection);
for (const key of keys) {
if (projection[key] === 0) {
for (const other of keys) {
if (other.startsWith(key + '.') && projection[other] === 0) {
delete projection[other];
}
}
}
}
}
24 changes: 24 additions & 0 deletions test/projection.pathCollision.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
'use strict';

const mongoose = require('../');
const { Schema, model } = mongoose;
const assert = require('assert');

describe('Fix projection collision', function() {
it('removes children when parent is excluded', async function() {
const BarSchema = new Schema({
name: String,
subd: {
raw: { type: String, select: false },
clean: String
}
});

const Bar = model('Bar', BarSchema, 'bars');

const q = Bar.find({}).select('subd');
const proj = q._fields;

assert.deepStrictEqual(proj, { subd: 0 });
});
});
Loading