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
FindOne returns default value of the nonexistent filed. #4362
Comments
That's just how defaults work, when there isn't a schema.pre('save', function(next) {
if (this.isNew) {
this.createdAt = Date.now();
}
next();
}); Also, I wouldn't recommend separating the fields in your projection |
Stumbled upon the same problem. |
Not necessarily, mongoose has a lot of places where the document doesn't represent the exact data stored in the db: getters, virtuals, etc. Either way, we assert on this behavior and changing it would break a lot of people's code, so we won't change it unless there's a lot of demand to change it. Here's a workaround: createdAt: {
type: Date,
default: function() {
if (this.isNew) { return new Date(); }
return undefined;
}
} This will make it so that |
First: A workaround to overcome this issue:
Model.findOne({id: 'blabla'}).select('email password createdAt').lean().exec()
issue:
I use an
createdAt
field -which has an expires (TTL24h
) and a default (Date.now
) value- in myUsers
model to store the creating date info of the new created users.If the user activates the account the system removes the
createdAt
field.If the user doesn't activate the account the system remove the account from
Users
collection after 24 hours.In my authentication operations, I check the existing of
createdAt
field firstly.If the user is not activated I show a message like
Account is not activated.
When I try
FindOne
method for purpose of the above authentication procedure, I getDate.now
value although there is no acreatedAt
field in the document because of the user has activated his account.createdAt
field in myUser
model:createdAt: { type: Date, expires: '24h', default: Date.now },
My method:
Users.findOne({email: username}, 'email, password, createdAt', function(err, user) {
By using the above method
createdAt
value returns at every time.ps: i searched the issue but i couldn't anything.
The text was updated successfully, but these errors were encountered: