Node JS With Mongo DB Using Mongoose

What is Mongoose?.

  • Mongoose is one of the Node.js libraries that provides MongoDB object mapping, in a simple manner “Mongoose is a library of Node.js, it provides interaction with MongoDB using Object-Relation- Mapping”.
  • Mongoose provides a straight-forward, schema-based solution to model our application data. It includes built-in type casting, validation, query building, business logic hooks and more.

How To Install Mongoose Library?.

Note:- Make sure Node server is installed in your machine. Link to Download Node Server.

Once node server is installed into machine then type below command in your command prompt.

npm install mongoose

Why Mongoose?.

  • Easy to interact with database.
  • Supports Object Relation Mapping.
  • Will help us to handle exceptions easily.
  • No need to learn database queries.

Program To insert document into collection?

var mongoose = require(‘mongoose’);
var dbHost = ‘mongodb://localhost:27017/whatsmysuggestion’;
mongoose.connect(dbHost);var StudentSchema = mongoose.Schema({
name: String,
sid: {type: String, index: true},
address: String,
fee: Number
});

var Student = mongoose.model(‘Student’, StudentSchema,
“mongoose_demo”);

var db = mongoose.connection;

db.on(‘error’, console.error.bind(console,’connection error:’));

db.once(‘open’, function(){
console.log(“Connected to DB”);

var Student1 = new Student({
name:”ranga”,
sid: “wms1”,
address: “Bangalore”,
fee: 123
});

Student1.save(function(err){
if ( err ) throw err;
console.log(“Student Saved Successfully”);
});

var Student2 = new Student({
name:”Arif”,
sid: “wms2”,
address: “Bangalore”,
fee: 125
});

Student2.save(function(err){
if ( err ) throw err;
console.log(“Student Saved Successfully”);

});

var Student3 = new Student({
name:”Vara”,
sid: “wms3”,
address: “Bangalore”,
fee: 80
});

Student3.save(function(err){
if ( err ) throw err;
console.log(“Student Saved Successfully”);
});
});

Program To Update document in collection?

var mongoose = require(‘mongoose’);
var dbHost = ‘mongodb://localhost:27017/whatsmysuggestion’;
mongoose.connect(dbHost);var StudentSchema = mongoose.Schema({
name: String,
sid: {type: String, index: true},
address: String,
fee: Number
});

var Student = mongoose.model(‘Student’, StudentSchema, “mongoose_demo”);

var db = mongoose.connection;

db.on(‘error’, console.error.bind(console, ‘connection error:’));
db.once(‘open’, function(){
console.log(“Connected to DB”);

Student.update({sid : {$eq: ‘wms3’}}, {$set: {name: “sreedhar”}}, function(err, result){
console.log(“Updated successfully”);
console.log(result);
});

});

Program To Delete document from collection?

var mongoose = require(‘mongoose’);
var dbHost = ‘mongodb://localhost:27017/whatsmysuggestion’;
mongoose.connect(dbHost);var StudentSchema = mongoose.Schema({
name: String,
sid: {type: String, index: true},
address: String,
fee: Number
});

var Student = mongoose.model(‘Student’, StudentSchema, “mongoose_demo”);

var db = mongoose.connection;

db.on(‘error’, console.error.bind(console, ‘connection error:’));
db.once(‘open’, function(){
console.log(“Connected to DB”);

Student.remove({fee:{$eq: 125}}).exec();
console.log(“Record Deleted”);
});

Program To Fetch documents from collection?

var mongoose = require(‘mongoose’);
var dbHost = ‘mongodb://localhost:27017/whatsmysuggestion’;
mongoose.connect(dbHost);var StudentSchema = mongoose.Schema({
name: String,
sid: {type: String, index: true},
address: String,
fee: Number
});

var Student = mongoose.model(‘Student’, StudentSchema, “mongoose_demo”);

var db = mongoose.connection;

db.on(‘error’, console.error.bind(console, ‘connection error:’));
db.once(‘open’, function(){
console.log(“Connected to DB”);

Student.find({fee : {$gt:100}}, “name sid address fee”, function(err, result){
if ( err ) throw err;
console.log(“Find Operations: ” + result);
});
} );

Complete Video Tutorial In Telugu.