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);

Comments

Popular posts from this blog

What is MQTT protocol & its use

What the hack is Call, bind & apply

What is Provider in Angular Js?