It is an object modeling tool for nodejs that connects MongoDB.Mongoose take the models and maps them to MongoDB database.
install required dependencies
npm install mongoose
var mongoose = require('mongoose');
/*without callback */
mongoose.connect('localhost','mydatabase');
/* with callback */ create a connection object and bind two callabcks.
var db = mongoose.connection;
db.on('open',function callback(){
//connection success
});
db.on('error',function(msg){
//connection error
});
Schema Creation :
var empSchema = mongoose.Schema({
name: String,
designation : String,
location: String,
Skills: String
});
Model Creation :
var Employee = mongoose.model('employee',empSchema);
Saving the model :
employee.save(function(err,emp){
if(err){
//error handling
}else{
//success
}
});
Reading the model :
var Employee = mongoose.model('employee',empSchema);
Employee.find(function(err,emp){
if(err){
//error handling
}else{
//success
}
});
Reading specific model :
Employee.find({name:'SrinivasNidadavolu'},function(err,emp){
if(err){
//error handling
}else{
//success
}
});
output:
[{name:'SrinivasNidadavolu',designation:'software engineer',location:'building number 14,Raheja Mindpsace, Hyderabad',Skills:'Java,C++,AWS,AZURE,Containers,cloud specialist'}]
install required dependencies
npm install mongoose
var mongoose = require('mongoose');
/*without callback */
mongoose.connect('localhost','mydatabase');
/* with callback */ create a connection object and bind two callabcks.
var db = mongoose.connection;
db.on('open',function callback(){
//connection success
});
db.on('error',function(msg){
//connection error
});
Schema Creation :
var empSchema = mongoose.Schema({
name: String,
designation : String,
location: String,
Skills: String
});
Model Creation :
var Employee = mongoose.model('employee',empSchema);
Saving the model :
employee.save(function(err,emp){
if(err){
//error handling
}else{
//success
}
});
Reading the model :
var Employee = mongoose.model('employee',empSchema);
Employee.find(function(err,emp){
if(err){
//error handling
}else{
//success
}
});
Reading specific model :
Employee.find({name:'SrinivasNidadavolu'},function(err,emp){
if(err){
//error handling
}else{
//success
}
});
output:
[{name:'SrinivasNidadavolu',designation:'software engineer',location:'building number 14,Raheja Mindpsace, Hyderabad',Skills:'Java,C++,AWS,AZURE,Containers,cloud specialist'}]
0 coment�rios:
Post a Comment