//用户
var mongoose = require("mongoose"),
setting = require("./setting"); //配置连接数据库方法
var connect = function(){
//mongoose.connect('mongodb://localhost/test');
mongoose.connect("mongodb://" + setting.host +"/"+ setting.db);
// db = mongoose.createConnection("localhost","test");
//db.on("error",console.error.bind(console,'连接错误:'))
} //连接放在外面就可以多次操作,不然只能操作一次
connect();
//关闭数据库连接
//mongoose.connection.close() // var Schema = mongoose.Schema;
// var userSchema = new Schema({}); //这种写法有点类似 object = Object();返回的也是对象
//usersSchema = mongoose.Schema;
var usersSchema = new mongoose.Schema({
name: String,
username:String,
pwd:String,
weibo:String,
email:String
}); var usersModel = mongoose.model('User', usersSchema); function User(user){
this.name = user.name,
this.username = user.username,
this.pwd = user.pwd,
this.weibo = user.weibo,
this.email = user.email
}; module.exports = User; //储存用户数据
User.prototype.save = function(callback){
//要存入的用户文档
var user = {
name:this.name,
username:this.username,
pwd:this.pwd,
weibo:this.weibo,
email:this.email
};
//连接数据库
//connect();
//var newUser = usersModel(user);
var newUser = new usersModel(user);
newUser.save(function(err, user){
if(err){
return callback && callback(err);
}
callback && callback(null,user);
})
}; //获取1条用户数据
User.get = function(query,callback){
//连接数据库
// connect();
usersModel.findOne(query, function (err, user) {
if (err) {
return callback && callback(err);
}
callback && callback(null,user);//成功,返回数据
});
}; //获取所有的用户数据
User.getAll = function(callback){
//连接数据库
// connect();
usersModel.find(function (err, user) {
if (err) {
return callback && callback(err);
}
callback && callback(null,user);//成功,返回数据
});
}; //获取id数据
User.findById = function(_id,callback){
//连接数据库
// connect();
usersModel.findById(_id,function (err, user) {
if (err) {
return callback && callback(err);
}
callback && callback(null,user);//成功,返回数据
});
}; //删除用户数据 只有状态码{result:{ok:1,n:0},connection:{……},options{……}}
User.remove = function(query,callback){
//连接数据库
// connect();
usersModel.remove(query, function (err, user) {
if (err) {
return callback && callback(err);
}
callback && callback(null,user);//成功,返回数据
});
}; //获取用户数据 只有状态码{ok:1,nModified:1,n:1}
User.update = function(query,opt,callback){
//连接数据库
// connect();
usersModel.update(query,opt,function (err, user) {
if (err) {
return callback && callback(err);
}
callback && callback(null,user);//成功,返回数据
});
}; var cat = {
name: "yoyo",
username:"cwj",
pwd:"",
weibo:"pingfan",
email:"[email protected]"
}; var Cat = new User(cat);
//保存
//Cat.save(function(err,user){console.log(user);})
//User.findById("557683e7297334ac29068af6",function(err,user){console.log(user);}); //查询
//User.get({name: "yoyo22"},function(err,user){console.log(user)});
//User.getAll(function(err,user){console.log(user)})
//mongoose.connection.close(); //删除一条
//User.remove({name: "yoyo11"},function(err,msg){console.log(msg)});
//删除所有
//User.remove(null,function(err,msg){console.log(msg)});
//User.remove({},function(err,msg){console.log(msg)}); //更新一条数据
//User.update({name: "yoyo"},{username:"cwj",pwd:"234556",weibo:"kskk"},function(err,msg){console.log(msg)});
05-11 20:53