0815 A

mongod 操作 (续)

var mongoose = require('mongoose');
var db = mongoose.createConnection('mongodb://192.168.112.129:27017/test');
var monSchema = new mongoose.monSchema({
name: { type:String,default:'nima'},
age:{ type:Number },
sex:{ type:String}
});
var monModel = db.model('user',monSchema);
var content = {name:"小李子",age:18,sex:'男'};
var monInsert = new monModel(content);
monInsert.sava(function(err){
if(err){
    console.log(err);
}
else{
    console.log('插入成功!');
}
db.close();
});

var tiaojian = {name:"张三"};
monModel.remove(tiaojian,function(err){
 if(err){
     console.log(err);
 }else{
     console.log("remove is ok");
 }
 db.close();
});

var content = { };
var field = { name:1,age:1,sex:1}
 monModel.find(content,field,function(err,result){
 if(err){
     console.log(err);
 }else{
     console.log(result);
 }
 db.close();
});

var tiaojian = {name:"张三"};
var gengxindata={$set:{name:'不死小强'}};
monModel.update(tiaojian,gengxindata,function(err,result){
 if(err){
     console.log(err);
 }else{
     console.log("update is ok");
 }
 db.close();
});

你可能感兴趣的:(0815 A)