I have the following schema.
var UserSchema = new mongoose.Schema({
username: {
type: String,
unique: true,
required: true
},
password: {
type: String,
required: true
},
test: {
type: String,
default: 'hello world'
}
});
UserSchema.pre('save', function(callback) {
var user = this;
this.test = undefined; // here unset test field which prevent to add in db
});
module.exports = mongoose.model('User', UserSchema);
But when I find data for example
User.find(function(err, users) {
if (err)
res.send(err);
res.json(users);
});
it always returns
[
{
_id: "56fa6c15a9383e7c0f2e4477",
username: "abca",
password: "$2a$05$GkssfHjoZnX8na/QMe79LOwutun1bv2o76gTQsIThnjOTW.sobD/2",
__v: 0,
test: "hello world"
}
]
How can I modify or add any special param to get data without test
field and without any change in query, let's say
User.find({}, '-test', function (err, users){
});
also, I have set the default value in model: test: "hello world"
but I do not want this value to appear in the response.
I have also set this.test = undefined;
and this should mean that it is preventing this default value to be added into database, however I am still getting this in response.