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.
Set your schema as
test: {
type: String,
default: 'hello world',
select: false
}
Check the SchemaType#select in the document.
test
property persisted in database and just don't want it selected when querying:You can use select
in a pre find hook:
UserSchema.pre('find', function (next) {
this.select({ test: false });
next();
});
In a query hook (as opposed to a save hook for example), this
refers to your query object. In a save hook it refers to the current document being saved.
This hook will only execute for find
queries, not findById
or findOne
queries.
OR
(see Hank Chiu's answer)
set the select flag to false in the schema :
test: {
type: String,
default: 'hello world',
select: false,
}
test
property persisted in database :Remove the test
property from schema and add a test
virtual :
schema.virtual('test').get(function () {
return 'hello world';
});
user.test
will return hello world
.
Add a getter
your test
definition :
test: {
type: String,
default: 'hello world',
get: function () {
return 'hello guys';
}
}
user.test
will return hello guys
but its true value will be persisted in database.
Old erroneous answer :
You can use select
which takes an object of Model properties as keys and booleans as values :
User
.find({})
.select({
test: false;
})
.exec(function (err, users) {
// users won't have the test property
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With