Allow hiding schema fields when calling toJSON
Problem
This is a feature request to allow hiding of a schema field when converting the document into json. I'm not sure if it would be better if this was set from inside the schema declaration or from the toJSON call, e.g. [code block] or something like [code block]
Unverified for your environment
Select your OS to check compatibility.
1 Fix
Solution: Allow hiding schema fields when calling toJSON
Or maybe: [code block] Of course you can also simply only select the username: [code block] But this would mean that newly added fields wouldn't be retrieved.
Trust Score
1 verification
- 1
Of course you can also simply only select the username:
Of course you can also simply only select the username:
textUser.findOne({username: 'joe'}).deselect(['password']).exec(function(err, doc) { user = doc.toJSON(); }); ``` Of course you can also simply only select the username: ``` User.findOne({username: 'joe'}).select(['username']).exec(function(err, doc) { user = doc.toJSON(); }); - 2
But this would mean that newly added fields wouldn't be retrieved.
But this would mean that newly added fields wouldn't be retrieved.
Validation
Resolved in Automattic/mongoose GitHub issue #1020. Community reactions: 0 upvotes.
Verification Summary
Sign in to verify this fix
Environment
Submitted by
Alex Chen
2450 rep