Automattic / Automattic/mongoose
Plus path projections underneath nested objects with select: false
- Dominant language
- JavaScript
- Stars
- 27.5k
- Forks
- 4k
- Avg merge
- 2d 7h
- Merged PRs (30d)
- 35
Description
Hi! I would like to report which I think may be a bug, or a feature not yet implemented.
I have an object in the schema with a `{ select: false }` opt and I want to select only one field of this object and let everything left hidden.
In other words, I am not able to use the dot notation and the `+` like so:
`.select("+authentification.login")`
to achieve that, the workaround found:
`.select("+authentification -authentification.password");`
but in my case I would have to omit more than one field.
thank you 😃
**standalone strict**
```javascript
const mongoose = require("mongoose");
mongoose.connect("mongodb://localhost:27017/test");
const Test = mongoose.model("Test", {
name: String,
authentification: {
type: {
login: String,
password: String,
},
select: false,
},
});
const data = {
name: "name",
authentification: {
login: "login",
password: "password",
},
};
async function main() {
await Test.create(data);
const userWrong = await Test.findOne({}).select("+authentification.password");
console.log(userWrong);
// workaround to make this work
const userCorrect = await Test.findOne({}).select(
"+authentification -authentification.password",
);
console.log(userCorrect);
}
main();
```
"mongoose": "5.2.18",
Contributor guide
Research direction
No source file or test is named. Start by running the standalone JavaScript reproduction against Mongoose 5.2.18 and trace the query projection handling for the nested select:false path; done means selecting +authentification.login returns that field without requiring the whole object, with a regression test covering the behavior.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- javascript, mongodb
- Domain
- databases
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100