[运维网] Mongoose
Mongoose是什么Mongoose是MongoDB的一个对象模型工具,可以工作于异步环境下。
定义一个模型很容易:
View Code
1 varComments = newSchema({
2 title: String,
3 body: String,
4 date: Date
5 });
6 varBlogPost = newSchema({
7 author: ObjectId,
8 title: String,
9 body: String,
10 date: Date,
11 comments: ,
12 meta: {
13 votes: Number,
14 favs: Number
15 }
16 });
17
18 mongoose.model('BlogPost', BlogPost);
安装
推荐通过NPM方式安装:
View Code
1 $ npm install mongoose
或者,你可以从Github仓库中获取代码,然后解压:
View Code
1 $ git clone git@github.com:LearnBoost/mongoose.git support/mongoose/
将模块添加至NodeJS可以找到的环境路径中require.paths.unshift('support/mongoose/lib');
然后就可以在应用中将mongoose模块包含进来
View Code
1 require('mongoose');
连接到MongoDB数据库
首先,我们需要定义一个连接。如果你的应用只使用一个数据库,可以使用mongoose.connect,如果需要创建附加数据库连接,使用mongoose.createConnection。
connect和createConnection都能连接mongodb数据库,支持以URI或参数(host,database,port)的形式。
View Code
var mongoose =require('mongoose');
mongoose.connect('mongodb://www.csser.com/csser.com_database');
连接一旦建立成功,该连接实例的open事件就被触发。如果你使用的是mongoose.connect方式,连接对象为mongoose.connection;否则,mongoose.createConnection返回的是Connection对象。
切记!Mongoose在与数据库真正建立连接之前便缓存了所有的命令,这就意味着你在定义模型、执行查询时不必非要确认与MongoDB数据库的连接是否已经建立。(一回@CSSer注:异步是MongoDB等与传统数据库的重大区别)
定义模型
模型是通过模式接口(Schema interface)定义的,如:
View Code
1 varSchema = mongoose.Schema, ObjectId = Schema.ObjectId;
2 varBlogPost = newSchema({
3 author: ObjectId,
4 title: String,
5 body: String,
6 date: Date
7 });
除了定义文档结构和你要存储的数据类型外,模式(Schema)还用于以下定义:
[*]
[*]Validators (异步和同步)
[*]Defaults - 默认值
[*]Getters
[*]Setters
[*]Indexes - 索引
[*]Middleware - 中间件
[*]Methods definition - 方法定义
[*]Statics definition - 静态定义
[*]Plugins - 插件
下面的代码向我们展示了这些功能的一部分:
View Code
1 varComment = newSchema({
2 name: {
3 type: String,
4 default: 'hahaha'
5 },
6 age: {
7 type: Number,
8 min: 18,
9 index: true
10 },
11 bio: {
12 type: String,
13 match: //
14 },
15 date: {
16 type: Date,
17 default: Date.now
18 }
19 });
20 // 定义setterComment.path('name').set(function(v){return v.capitalize();});
21 // 定义中间件Comment.pre('save',function(next){notify(this.get('email'));next();});
你可以查看几乎包含所有模型定义的 示例 。
访问模型
当通过mongoose.model('ModelName', mySchema)定义了一个模型之后,我们可以通过相同的函数来访问它:
View Code
1 var myModel = mongoose.model('ModelName');
接下来我们可以将模型的实例保存下来:
View Code
1 var instance = new myModel();
2 instance.my.key = 'csser';
3 instance.save(function(err) { //});
或者我们也可以从同样的的集合(collection)中找到文档(documents):
View Code
1 myModel.find({}, function(err, docs) {
2 // docs.forEach
3 });
也可以调用findOne, findById, update等等方法,更多的细节请阅读 API文档 。
嵌入文档
还记得在第一个示例的代码片段中,我们在模式中定义了一个键(key):
View Code
comments:
这里的Comments是我们已经创建的模式(Schema),这就是说创建嵌入文档跟下面的代码看起来一样简单:
View Code
1 // 重新获得模型varBlogPost= mongoose.model('BlogPost');
2 // 创建一篇博客日志var post =newBlogPost();// 创建一个评论
3 post.comments.push({
4 title: 'My comment for csser.com'
5 });
6
7 post.save(function(err) {
8 if (!err) console.log('Success!');
9 });
用同样的方式删除模式:
View Code
1 BlogPost.findById(myId, function(err, post) {
2 if (!err) {
3 post.comments.remove();
4 post.save(function(err) {
5 // do something
6 });
7 }
8 });
嵌入文档拥有与模型相同的功能,包括Defaults、validators、middleware等。当发生错误时,它会冒泡到save()错误回调函数,这里错误处理是一个单元。
Mongoose interacts with your embedded documents in arrays atomically, out of the box.
中间件
中间件是Mongoose 1.0推出的最激动人心的功能之一,它让我们可以不用再受嵌套回调函数的折磨了。
中间件定义在模式级别(Schema level),当方法初始化时(文档与MongoDB数据初始化后)、保存数据时(文档或嵌入文档保存后)生效。
中间件有两种类型,它们由定义的函数签名确定(即函数接受的参数)。
顺序(Serial)中间件定义如下:
View Code
1 .pre(method, function(next) {
2 // ...
3 })
当每个中间件调用下一个时,它们按顺序执行。
并行(Parallel)中间件提供更细粒度的控制,其定义如下:
View Code
1 .pre(method, function(next, done) {
2 // ...
3 })
Parallel 中间件可以立刻next(),但是只有当所有parallel中间件调用done()之后最后的参数才被执行。
错误处理
如果任一个中间件调用next或done时抛出了错误,执行流会中断,错误会被作为参数传入回调函数。
例如:
View Code
1 schema.pre('save', function(next) {
2 // 发生了一些错误
3 next(newError('有错误发生'));
4 }); // 接着...
5
6 myModel.save(function(err) {
7 // 错误可以来自某个中间件
8 });
API 文档
可以在 http://mongoosejs.com 找到相关API文档。
原文地址
http://www.csser.com/board/4f3f516e38a5ebc9780004fe
页:
[1]