Example created collection in mongodb with nodejs use mongoose

Creating a Collection

Example:  create a collection in MongoDB use model method

 

var mongoose = require('mongoose');

var db = mongoose.connection;

var Schema = mongoose.Schema;

db.on('error', console.error);

db.once('open', function () {

    console.log("db connect");

    var customers = new Schema({

        name: {type: String, required: true},

        address: {type: String, required: true}

    });

    db.model('customers', customers);

    console.log("Created collection customers");

});

mongoose.connect('mongodb://192.168.1.122:27017/codewr');

 

Run:

 

C:\ Nodejs>node mongooseCreatedCollection.js

Created collection customers