mongoose
2015-10-08 15:47
465 查看
之前看过mongodb,现在需要用过nodejs来操作,继续看一下,mongoose。
教程:http://ourjs.com/detail/53ad24edb984bb4659000013
我是在http://www.cnblogs.com/wang-jing/p/4138654.html(node.js+express+mongodb)的基础上练习的
1、可以由model生成entity
2、可以在schema中定义方法,在entity中使用:为model和entity提供公共方法
schema:是数据库模型在程序中的表现;schema.type是mongoose的数据类型
Schema.Types.Mixed是Mongoose定义个混合类型,碰到了再说
Schema.Types.ObjectId主键
虚拟属性/虚拟域 碰到了再说
配置项:safe,strict,sharedKey(分布式),capped(上线操作),versionKey(版本锁),autoIndex(自动索引)
Documents:相当于entity
更新
新增:
entity.save(); model.create();
删除:
entity.remove(); model.remove();
子文档 碰到了再说
查询
model.find(); model.count()
验证:
required 非空验证
min/max 范围验证(边值验证)
enum/match 枚举验证/匹配验证
validate 自定义验证规则
中间件
串行
并行
使用范畴
例如:看看还有没有其他方法
教程:http://ourjs.com/detail/53ad24edb984bb4659000013
Schema、
Model、
Entity的关系请牢记,
Schema生成
Model,
Model创造
Entity,
Model和
Entity都可对数据库操作造成影响,但
Model比
Entity更具操作性。
我是在http://www.cnblogs.com/wang-jing/p/4138654.html(node.js+express+mongodb)的基础上练习的
1、可以由model生成entity
var personE = new Demo({title:'blog'}); console.log(personE.title);
2、可以在schema中定义方法,在entity中使用:为model和entity提供公共方法
demoSchema.methods.speak = function(){ console.log('my name is:'+this.title); } personE.speak();
schema:是数据库模型在程序中的表现;schema.type是mongoose的数据类型
Schema.Types.Mixed是Mongoose定义个混合类型,碰到了再说
Schema.Types.ObjectId主键
虚拟属性/虚拟域 碰到了再说
配置项:safe,strict,sharedKey(分布式),capped(上线操作),versionKey(版本锁),autoIndex(自动索引)
var ExampleSchema = new Schema(config,options); 或 var ExampleSchema = new Schema(config); ExampleSchema.set(option,value);
Documents:相当于entity
更新
PersonModel.findById(id,function(err,person){ person.name = 'MDragon'; //entity person.save(function(err){}); });
新增:
entity.save(); model.create();
删除:
entity.remove(); model.remove();
子文档 碰到了再说
查询
Person .find({ occupation: /host/ }) .where('name.last').equals('Ghost') .where('age').gt(17).lt(66) .where('likes').in(['vaporizing', 'talking']) .limit(10) .sort('-occupation') .select('name occupation') .exec(callback);
model.find(); model.count()
验证:
验证始终定义在SchemaType中 验证是一个内部中间件 验证是在一个Document被保存时默认启用的,除非你关闭验证 验证是异步递归的,如果你的SubDoc验证失败,Document也将无法保存 验证并不关心错误类型,而通过ValidationError这个对象可以访问
required 非空验证
min/max 范围验证(边值验证)
enum/match 枚举验证/匹配验证
validate 自定义验证规则
var PersonSchema = new Schema({ name:{ type:'String', required:true //姓名非空 }, age:{ type:'Nunmer', min:18, //年龄最小18 max:120 //年龄最大120 }, city:{ type:'String', enum:['北京','上海'] //只能是北京、上海人 }, other:{ type:'String', validate:[validator,err] //validator是一个验证函数,err是验证失败的错误信息 } });
err.errors //错误集合(对象) err.errors.color //错误属性(Schema的color属性) err.errors.color.message //错误属性信息 err.errors.path //错误属性路径 err.errors.type //错误类型 err.name //错误名称 err.message //错误消息
中间件
串行
var schema = new Schema(...); schema.pre('save',function(next){ //做点什么 next(); });
并行
var schema = new Schema(...); schema.pre('save',function(next,done){ //下一个要执行的中间件并行执行 next(); doAsync(done); });
使用范畴
复杂的验证 删除有主外关联的doc 异步默认 某个特定动作触发异步任务,例如触发自定义事件和通知
例如:看看还有没有其他方法
schema.pre('save',function(next){ var err = new Eerror('some err'); next(err); }); entity.save(function(err){ console.log(err.message); //some err });
相关文章推荐
- Algorithms—229.Majority Element II
- LSGO软件技术团队2015~2016学年第三周(0914~0920)总结
- Algorithms—287.Find the Duplicate Number
- django模型修改后更新报错表不存在的的解决办法
- Go语言学习笔记
- Algorithms—290.Word Pattern
- 谷歌推出加速移动网页项目AMP:打开网页速度更快
- Emacs中Golang的设置
- django filefield实现动态改变文件上传位置upload_to
- POJ 2195 Going Home
- Go error--cannot find package
- POJ 2762 Going from u to v or from v to u?(强连通+缩点+拓扑排序)
- genymotion device got no ip address 的解决方案
- Category Protocol Exte…
- OC中的类别Category-协议Protocol-…
- OC中的类别Category-协议Protocol-…
- Category Protocol Exte…
- Golang适合高并发场景的原因分析
- HOTPOWER.【专注游戏界面外包】/接游戏界面外包/logo外包/icon
- 你知道苹果员工一年能为公司创造多少收入吗?