How to use the models function from mongoose

Find comprehensive JavaScript mongoose.models code examples handpicked from public code repositorys.

84
85
86
87
88
89
90
91
92
93

## mongoose basics

> * your models extends from mongoose.Model
> * schemas are stored on global mongoose but models may not
> * mongoose.models.ModelName equals to mongoose.model('ModelName')
> * ModelName.schema equals to mongoose.modelSchemas.ModelName
> * static methods should be extended on mongoose.Model with a dynamic context
> * instance methods should be extended on mongoose.Model.prototype
> * custom model static methods are stored in MyModel.schema.statics 
fork icon7
star icon48
watch icon6

+ 11 other calls in file

89
90
91
92
93
94
95
96
97
        });
    }
};

// Export model
delete mongoose.models.SurveyResponse
delete mongoose.modelSchemas.SurveyResponse
var SurveyResponse = mongoose.model('SurveyResponse', SurveyResponseSchema);
module.exports = SurveyResponse;
fork icon29
star icon38
watch icon24

84
85
86
87
88
89
90
91
92
93
94
95


// Small utility function for checking if email is already exists
userSchema.path('email').validate(async function (value) {
  if (!this.isNew) return;


  const emailCount = await mongoose.models.User.countDocuments({
    email: value,
  });
  return !emailCount;
}, 'Email already exists. Please Login with your existing Credentials.');
fork icon0
star icon0
watch icon1

+ 7 other calls in file