安裝
npm install mongod
npm install mongoose
# 鏈接數(shù)據(jù)庫
>```
var mongoose=require('mongoose');
mongoose.connect('mongodb://localhost/dbname');
定義數(shù)據(jù)模型
var BlogPost = new Schema({
author : ObjectId,
title : String,
body : String,
date : Date
});
# 增刪改查 demo
>```
//insert
var mongoose = require('mongoose');
var db = mongoose.createConnection('mongodb://192.168.127.132:27017/caideyang_db');
var monSchema = new mongoose.Schema({
name:{type:String,default:'Caideyang'},
age:{type:Number},
sex:{type:String}
});
var monModel = db.model('user',monSchema);
var content = {name:'zzd',age:16,sex:'A'};
var monInsert = new monModel(content);
monInsert.save(function(err){
if (err) {
console.log(err);
}else{
console.log('Insert Successed !');
}
db.close();
});
//find
var mongoose = require('mongoose');//引入模塊
var db = mongoose.createConnection('mongodb://192.168.127.132:27017/caideyang_db');//創(chuàng)建連接,選擇數(shù)據(jù)
var monSchema = new mongoose.Schema({ //設(shè)置一個基礎(chǔ)的數(shù)據(jù)類型
name:{type:String,default:'Caideyang'},
age:{type:Number},
sex:{type:String}
});
var monModel = db.model('user',monSchema); //創(chuàng)建一個數(shù)據(jù)庫集合(表)user,系統(tǒng)默認(rèn)會加s
//var content = {name:'zdd',age:28,sex:'girl'}; //插入的內(nèi)容
var content = {name:'zcc'};//定義查詢條件
var field = {name:1,age:1,sex:1};//定義查詢的字段
//var monInsert = new monModel(content);//設(shè)置內(nèi)容
monModel.find(content,field,function(err,result){//數(shù)據(jù)插入操作
if (err) {
console.log(err);
}else{
console.log(result);
}
db.close();
});
//update
var mongoose = require('mongoose');//引入模塊
var db = mongoose.createConnection('mongodb://192.168.127.132:27017/caideyang_db');//創(chuàng)建連接,選擇數(shù)據(jù)
var monSchema = new mongoose.Schema({ //設(shè)置一個基礎(chǔ)的數(shù)據(jù)類型
name:{type:String,default:'Caideyang'},
age:{type:Number},
sex:{type:String}
});
var monModel = db.model('user',monSchema); //創(chuàng)建一個數(shù)據(jù)庫集合(表)user,系統(tǒng)默認(rèn)會加s
//var content = {name:'zsc',age:10,sex:'girl'}; //插入的內(nèi)容
var content = {name:'snc'};//定義查詢條件
var gengxindata = {$set:{name:'程萬清',age:26}}; //修改后的內(nèi)容
//var field = {name:1,age:1,sex:1};//定義查詢的字段
//var monInsert = new monModel(content);//設(shè)置內(nèi)容
monModel.update(content,gengxindata,function(err,result){//數(shù)據(jù)更新操作
if (err) {
console.log(err);
}else{
console.log(result);
}
db.close();
});
//delete
var mongoose = require('mongoose');//引入模塊
var db = mongoose.createConnection('mongodb://192.168.127.132:27017/caideyang_db');//創(chuàng)建連接花嘶,選擇數(shù)據(jù)
var monSchema = new mongoose.Schema({ //設(shè)置一個基礎(chǔ)的數(shù)據(jù)類型
name:{type:String,default:'Caideyang'},
age:{type:Number},
sex:{type:String}
});
var monModel = db.model('user',monSchema); //創(chuàng)建一個數(shù)據(jù)庫集合(表)user,系統(tǒng)默認(rèn)會加s
//var content = {name:'daa',age:10,sex:'girl'}; //插入的內(nèi)容
var content = {name:'cac'};//定義刪除條件
//var field = {name:1,age:1,sex:1};//定義查詢的字段
//var monInsert = new monModel(content);//設(shè)置內(nèi)容
monModel.remove(content,function(err,result){//數(shù)據(jù)刪除操作
if (err) {
console.log(err);
}else{
console.log(result);
}
db.close();
});