Tuesday, 10 December 2019

Mongoose FindOne extremely slow!

Hello all,I have realized that the following code related to the registration of an user takes around 4 seconds, is that normal? Even during testing where I have only one user it takes that long.javascript if (password === repeatedPassword) { try { const existingUser = await User.findOne({ 'local.email': email }); if (existingUser) { if (existingUser.local.registrationConfirmed) { return res.status(400).send({ type: errorTypes.REGISTER_ERROR_VERIFIED_USER_EXISTS_ALREADY, payload: req.t(`auth:${errorTypes.REGISTER_ERROR_VERIFIED_USER_EXISTS_ALREADY}`) }); } return res.status(400).send({ type: errorTypes.REGISTER_ERROR_NON_VERIFIED_USER_EXISTS_ALREADY, payload: req.t(`auth:${errorTypes.REGISTER_ERROR_NON_VERIFIED_USER_EXISTS_ALREADY}`) }); }My schema is as follows:```javascriptconst mongoose = require('mongoose');const userSchema = mongoose.Schema({ local: { type: { email: { type: String, unique: true, required: true }, name: { type: String, required: true }, password: { type: String, required: true }, resetPasswordToken: String, resetPasswordExpires: Date, verificationToken: String, verificationExpires: { type: Date, default: () => new Date(+new Date() + 7 * 24 * 60 * 60 * 1000) }, registrationConfirmed: { type: Boolean, default: false } }, required: false }, google: { id: String, name: String, email: String }, accountType: String });userSchema.index( { 'local.verificationExpires': 1 }, { name: 'Remove unverified users', expireAfterSeconds: 3600, partialFilterExpression: { 'local.registrationConfirmed': false } } );module.exports = mongoose.model('User', userSchema); ```​Could the index I create be the problem? Is there a way to improve things? or is this expected?Thank you in advance and regards.

Submitted December 11, 2019 at 06:55AM by dejavits

No comments:

Post a Comment