Automattic / Automattic/mongoose
Selecting parent field causes an automatic child field selection even if it has a select false option.
- Dominant language
- JavaScript
- Stars
- 27.5k
- Forks
- 4k
- Avg merge
- 2d 5h
- Merged PRs (30d)
- 32
Description
**Do you want to request a *feature* or report a *bug*?**
Possibly a bug (Or an expected behavior?)
**What is the current behavior?**
**If the current behavior is a bug, please provide the steps to reproduce.**
```js
const mongoose = require('mongoose');
mongoose.connect('...');
const Example = mongoose.model('Example', {
parent: {
hidden: { type: String, select: false },
other: { type: String }
}
});
const runSample = async () => {
const example = new Example({
parent: {
hidden: 'Should be hidden?',
other: 'Just a value',
}
});
await example.save();
const ex1 = await Example.findOne({ _id: example._id }).exec();
console.log(ex1); // Logs { _id: '...', parent: { other: 'Just a value' } } without parent.hidden
const ex2 = await Example.findOne({ _id: example._id }).select('parent').exec();
console.log(ex2); // Logs { _id: '...', parent: { hidden: 'Should be hidden?', other: 'Just a value' } } with parent.hidden
};
runSample().then(() => {
console.log('Done');
}, err => {
console.error(err);
});
```
Current behavior includes a hidden child field `parent.hidden` even if `{ select: false }` options is set in Schema when a parent field is selected.
But without any `.select()` clause, the result doesn't include `parent.hidden` field.
**What is the expected behavior?**
```js
const ex2 = await Example.findOne({ _id: example._id }).select('parent').exec();
console.log(ex2); // { _id: '...', parent: { other: 'Just a value' } } also without parent.hidden
```
Assuming **no select clause implies everything is selected but hidden fields**, I think it makes sense that if `parent` field is selected, the result shouldn't include `parent.hidden` field as well.
**Please mention your node.js, mongoose and MongoDB version.**
Mongose: 5.2.5
MongoDB: 3.6
Node.js: 8.x
Contributor guide
Research direction
No source files or tests are named. Start by running the provided Mongoose reproduction with the stated versions, compare the result of the query with and without .select('parent'), and trace the projection handling that differs. Done means selecting the parent does not include its select:false child field.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- javascript, mongodb, node.js
- Domain
- databases
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 38/100