How to define model in MongoDb
Here, we are going to make a model in Express using MongoDb
//First of all, require a mongoose module
var mongoose = require("mongoose");
// then create schema instance using mongoose
var schema = mongoose.Schema;
// finally create a schema using schema instance and in defining model, we defines the fields and their type for our model/collections.
var userSchema = new schema({
name:'String',
email:'String',
age:'String'
});
// And at last, export the model by giving it a suitable name, for using it outside this file/module (If we are using collections UERERS, then provide a name like USER)
module.exports = mongoose.model("User",userSchema);
//First of all, require a mongoose module
var mongoose = require("mongoose");
// then create schema instance using mongoose
var schema = mongoose.Schema;
// finally create a schema using schema instance and in defining model, we defines the fields and their type for our model/collections.
var userSchema = new schema({
name:'String',
email:'String',
age:'String'
});
// And at last, export the model by giving it a suitable name, for using it outside this file/module (If we are using collections UERERS, then provide a name like USER)
module.exports = mongoose.model("User",userSchema);
Comments
Post a Comment