Allow hiding schema fields when calling toJSON
See original GitHub issueThis 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.
user = new Schema
username:
type: String
password:
type: String
expose: false
or something like
User.findOne({username: 'joe'}, function(err, doc) {
user = doc.toJSON(hide: 'password')
});
Issue Analytics
- State:
- Created 11 years ago
- Comments:53 (9 by maintainers)
Top Results From Across the Web
Hide properties of Mongoose objects in Node.JS JSON ...
The solution is to define a custom . toJSON() method on the Mongoose schema and delete the properties which you don't want to...
Read more >How to protect the password field in Mongoose/MongoDB so it ...
I'm using for hiding password field in my REST JSON response UserSchema.methods.toJSON = function() { var obj = this.toObject(); //or var obj =...
Read more >Mongoose v6.8.2: Schemas
Mongoose assigns each of your schemas an id virtual getter by default which returns the document's _id field cast to a string, or...
Read more >Understanding JSON Schema
There are a number of online JSON Schema tools that allow you to run your own JSON schemas against example documents.
Read more >OpenAPI Specification - Version 2.0 - Swagger
Models are described using the Schema Object which is a subset of JSON Schema ... This is global to all APIs but can...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Has anyone solved this yet? Otherwise I’ll write that functionality when I have time. How about:
or maybe
There should probably be an option to ignore this, like:
doc.toJSON({showAll: true});
ordoc.toJSON({ignoreOptions: true});
ordoc.toRawJSON();