/**
* Created by lbc on 2016/11/16.
*/
var mongoose=require("mongoose");
var db=mongoose.connect('mongodb://localhost/test'); //连接数据库
var Schema=mongoose.Schema; //创建模型 db.connection.on("error",function(error){
console.log("数据库连接失败:"+error)
});
db.connection.on("open", function () {
console.log("------数据库连接成功!------");
}); var userSchema=new Schema({
name:String,
password:String
}); //定义了一个新的模型,但是此模型还未和users集合有关联
//扩展静态方法 extend
userSchema.static('findByName',function(name,callback){
return this.fund({name:name},callback);
}); var userModel=db.model('users',userSchema); /***************************************************************************************/
//保存-entity
var saveOp=function(collection){
collection.save(function(error,doc){
if(error){
console.log("error:"+error);
}else{
console.log(doc); // obj { __v: 0, name: 'lgb', password: 'abc123', _id: 5832bcdf78b9e9227cb0319e }
}
});
};
//新增-model
var insertOp=function(collection,model){
collection.create(model,function(error,doc){
if(error){
console.log("error:"+error);
}else{
console.log(doc); // obj { __v: 0, name: 'lgb', password: 'abc123', _id: 5832bcdf78b9e9227cb0319e }
}
});
};
//更新-model
var updateOp=function(conllection,fiter,update){
conllection.update(fiter,update,function(error,doc){
if(error){
console.log("error:"+error);
}else{
console.log(doc); // { ok: 1, nModified: 1, n: 1 }
}
});
};
//删除-model
var removeOp=function(conllection,fiter){
conllection.remove(fiter,function(error,docs){
if(error){
console.log('error:'+error);
}else{
console.log(docs); //{ result: { ok: 1, n: 1 }
}
});
};
//查询-model
var findOp=function(collection,filter,fields,options){
collection.find(filter,fields,options,function(error,docs){
if(error){
console.log("error:"+error);
}else{
console.log(docs); // []
}
});
};
//查询单条-model
var findOneOp=function(collection,filter,fields){
collection.findOne(filter,fields,function(error,docs){
if(error){
console.log("error:"+error);
}else{
console.log(docs); //obj
}
});
};
//查询单条-model-By _id
var findByIdOp=function(collection,_id,fields){
collection.findById(_id,fields,function(error,docs){
if(error){
console.log("error:"+error);
}else{
console.log(docs); //obj
}
});
};
/***************************************************************************************/ //saveOp(new userModel({name:'lbc123',password:'123'}));
//insertOp(userModel,{name:'lgb',password:'abc123',sex:true})
//findOp(userModel,{},null,{limit:50});
//updateOp(userModel,{name:'lbc123'},{$set:{password:'abc123'}});
//removeOp(userModel,{_id:'582d5eefc6fa58205c0a7161'});
//findOneOp(userModel,{name:'lbc123'},{name:1});
//findByIdOp(userModel,'5832a0f5b84f0a1fd0ef5770'); exports.user=userModel; //与users集合关联 映射 ORM //filter 条件语法
/*1. $gt(>),$lt(<),$lte(<=),$gte(>=)操作符:针对Number类型的查询具体超强的排除性。
2. $ne(!=)操作符:相当于不等于、不包含,查询时可根据单个或多个属性进行结果排除。
3. $in操作符:和$ne操作符用法相同,但意义相反。
4. $or操作符:可查询多个条件,只要满足其中一个就可返回结果值。
5. $exists操作符:主要用于判断某些属性是否存在。
游标:
{limit:10} 限制数量
{skip:10} 跳过数量 数量中少于n的话,则不会返回任何结果。
{sort:{age:-1}} 结果排序 1是升序 asc,-1是降序 desc
*/

MongooseHelper的更多相关文章

随机推荐

  1. 通过style控制圆形imageView显示

    1. 2.drawable--style <?xml version="1.0" encoding="utf-8"?> <layer-list ...

  2. jquery validate minlength rule is not working

    Question: I have a form with a password field. The password has to be at least 8 characters long. &l ...

  3. shiro session计算timeout

    /** * Determines if this session is expired. * * @return true if the specified session has expired, ...

  4. October 31st Week 45th Monday 2016

    While there is life there is hope. 一息若存,希望不灭. Go on living even if there is no hope. Knowing is not ...

  5. SQL SERVER中如何在声明游标的语句中,用变量做表名

    -- 因为定义游标所用的表名是变量,所以采用EXEC(定义语句) 的方式来声明游标set @StrSql='DECLARE Ba_Cursor CURSOR FOR (SELECT a.PhoneId ...

  6. Vue.js与MVVM

    MVVM 是Model-View-ViewModel 的缩写,它是一种基于前端开发的架构模式,其核心是提供对View 和 ViewModel 的双向数据绑定,这使得ViewModel 的状态改变可以自 ...

  7. centos7.0 下安装jdk1.8

    centos7.0这里安装jdk1.8采用yum安装方式,非常简单. 1.查看yum库中jdk的版本 [root@localhost ~]# yum search java|grep jdk 2.选择 ...

  8. 缓存淘汰算法--LRU算法

    1. LRU1.1. 原理 LRU(Least recently used,最近最少使用)算法根据数据的历史访问记录来进行淘汰数据,其核心思想是"如果数据最近被访问过,那么将来被访问的几率也 ...

  9. mysql忘记root密码怎么办?

    有时候忘记mysql的root密码了,怎么办? 这个时候,我们可以修改my.cnf,添加以不检查权限的方式启动,再修改root,最后重启mysql数据库. (1)service mysql stop ...

  10. Windows8 UI MessageBox In DevExpress

    // custom messagebox using System; using System.Drawing; using System.Windows.Forms; using DevExpres ...