标签:
var mongoose =require(‘mongoose‘);
var db = mongoose.connect("mongodb://localhost:27001/test");//本地库test 数据库
var testSchem = new mongoose.Schema({
name:{type:String},
age:{type:Number,default:0},
time:{type:Date,default:Date.now},
email:{type:String,default:‘‘}
});
var TestModel =db.model(‘test1‘,testSchem);//test1集合
var TestEntity=new TestModel({
name:‘eason‘,
age:23,
email:‘1241@qq.com‘
});
console.log(TestEntity.name);
//保存
TestEntity.save(function(err){
if(err){
console.log(err)
return
}
else
{
console.log(‘save ok‘)
}
})
//更新
TestModel.update({name:‘eason‘},{$set:{name:‘chen‘}},function(err){
if(err){
console.log(‘update not ok‘)
return
}else{
console.log(‘success‘)
}
})
//删除
TestModel.remove({name:‘chen‘},function(err){
if(err){
console.log(‘remove not ok‘)
return
}else{
console.log(‘remove success‘)
}
})
//查询
TestModel.findOne({name:‘chen‘},function(err){
if(err){
console.log(‘no result‘)
return
}else{
console.log(‘has one‘)
}
})
标签:
原文地址:http://www.cnblogs.com/cylblogs/p/5175521.html