Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Select entire document after using select: false #11474

Closed
NathanAP opened this issue Mar 3, 2022 · 10 comments
Closed

Select entire document after using select: false #11474

NathanAP opened this issue Mar 3, 2022 · 10 comments
Labels
enhancement This issue is a user-facing general improvement that doesn't fix a bug or add a new feature
Milestone

Comments

@NathanAP
Copy link

NathanAP commented Mar 3, 2022

Do you want to request a feature or report a bug?

Question / feature

What is the current behavior?

Currently, if we use select: false in a schema, we need to do Model.findOne({ _id }).select("+field_a") to get its value.

What is the expected behavior?

We could have an option to select the entire document, ignoring the select: false, it would not require us to do something like Model.findOne({ _id }).select("+field_b +field_c +field_d +field_e ... +field_n"). I feel this is necessary for some situations, but I couldn't figure a proper way to achieve this. I couldn't find anywhere an answer to this and I did not get an answer in stack overflow. Any idea is welcome!

What are the versions of Node.js, Mongoose and MongoDB you are using? Note that "latest" is not a version.

node v16.14.0
mongoose v6.2.4
mongodb v5.0.6

@Uzlopak
Copy link
Collaborator

Uzlopak commented Mar 4, 2022

If you use select false, then you basically say: hey I don't want to expose this attribute by a simple findOne or find by accident because of reasons like security.

So why are you using select false?

@IslandRhythms
Copy link
Collaborator

ditto ^

@IslandRhythms IslandRhythms added the help This issue can likely be resolved in GitHub issues. No bug fixes, features, or docs necessary label Mar 4, 2022
@NathanAP
Copy link
Author

NathanAP commented Mar 4, 2022

Thats totally the case, I'm using it to hide attributes that I won't use in the response!

Lets say I'm not interested in send a list of created orders by users to my endpoint response, but I need to use the list for something else (comparison, searching, etc). To archieve that, I'll need to use .select("+orders") every time I seek for an user to do it. If I have more "hidden" attributes, I'll need to add a +anotherAttribute for every other attribute I need. A simple option that brings the entire document could be more simple while coding.

Its not really a big deal, but I missed this option the week with a model I have.

@indraraj26
Copy link

In this case you can drop the field with select("-dropfield") instead of select:false on schema level.

@Uzlopak
Copy link
Collaborator

Uzlopak commented Mar 5, 2022

I would actually prefer to always select with an object and not with a string.

Anyhow
If you want you can even create a second model with the same schema, but which has no select:false set. Like userPublicModel and userInternalModel. In one you have a schema where you hide e.g. password and one were you expose everything.

@NathanAP
Copy link
Author

NathanAP commented Mar 7, 2022

Nice! I don't want to cause trouble or make this a big deal, just wondering if there was a good option to do it without needing to add a string everywhere. I'll think about another object or a const string with a method to get all fields available. Thanks.

@NathanAP NathanAP closed this as completed Mar 7, 2022
@sami-lab
Copy link

Actually I also need this feature for selecting All fields for Admin side so instead of writing all select:false fields manually Is there any better way you found?

@NathanAP
Copy link
Author

No ):

I ended up adding everything in a single string when I needed...

@1finedev
Copy link

a little unfortunate that mongoose does not have this.

Every api surely has an admin side which needs to fetch all fields in the db. having to chain .select(insert 20 fields here) is just a pain

@vkarpov15 vkarpov15 reopened this Sep 19, 2024
@vkarpov15 vkarpov15 added this to the 8.8 milestone Sep 19, 2024
@vkarpov15 vkarpov15 added enhancement This issue is a user-facing general improvement that doesn't fix a bug or add a new feature and removed help This issue can likely be resolved in GitHub issues. No bug fixes, features, or docs necessary labels Sep 19, 2024
vkarpov15 added a commit that referenced this issue Oct 27, 2024
vkarpov15 added a commit that referenced this issue Oct 28, 2024
feat(query): add schemaLevelProjections option to query to disable schema-level select: false
@NathanAP
Copy link
Author

Thanks guys for this feature <3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement This issue is a user-facing general improvement that doesn't fix a bug or add a new feature
Projects
None yet
Development

No branches or pull requests

7 participants