您的位置:首页 > 编程语言 > Go语言

mongoose 文档(一) schemas

2016-02-03 01:54 483 查看

1、定义shcema

在mongoose里一切都由schema开始。每一个schema对应一个mongoDB collection 并且在那个collection里面定义了documents的模型。

var mongoose = require('mongoose');
var Schema = mongoose.Schema;

var blogSchema = new Schema({
title:  String,
author: String,
body:   String,
comments: [{ body: String, date: Date }],
date: { type: Date, default: Date.now },
hidden: Boolean,
meta: {
votes: Number,
favs:  Number
}
});


之后如果你想增加额外的键,使用 Schema#add方法

在blogSchema中的每一个key定义了在document中的一个属性,将转换为它相关的Schema类型。例如,我们已经定义了的title将转换为String Schema类型并且date将转换为Date Schema 类型。键也可以指定为嵌套对象包含更多的 key/type 定义(例如上面的'meta'属性)。

合法的Schema 类型是

String

Number

Date

Buffer

Boolean

Mixed

ObjectId

Array

查看更多

Shema不仅定义了document的结构和构造了属性,还定义了document实例方法、静态Model方法复合索引和被称作middleware的document生命周期钩子。

2、创建一个Model

使用schema定义,需要将blogSchema转换为能够工作的Model。为此,我们把它传给

mongoose.model(modelName, schema)

var Blog = mongoose.model('Blog', blogSchema);
// ready to go!


3、实例方法

Model的实例是document。document有很多内置的实例方法。我们也可以定义自己的document实例方法。

// define a schema
var animalSchema = new Schema({ name: String, type: String });

// assign a function to the "methods" object of our animalSchema
animalSchema.methods.findSimilarTypes = function (cb) {
return this.model('Animal').find({ type: this.type }, cb);
}


现在我们所有的animal实例有findSiilarTypes方法 可用。

var Animal = mongoose.model('Animal', animalSchema);
var dog = new Animal({ type: 'dog' });

dog.findSimilarTypes(function (err, dogs) {
console.log(dogs); // woof
});


重新默认的mongoose document方法可以会导致不可预知的结果。

4、静态

在model中添加静态方法很简单。继续animalSchema。

// assign a function to the "statics" object of our animalSchema
animalSchema.statics.findByName = function (name, cb) {
return this.find({ name: new RegExp(name, 'i') }, cb);
}

var Animal = mongoose.model('Animal', animalSchema);
Animal.findByName('fido', function (err, animals) {
console.log(animals);
});


5、索引

MongoDB支持二级索引。在mongoose, at the path level或schema层次定义Schema的索引。当创建复合索引时,在shema定义索引是必要的。

var animalSchema = new Schema({
name: String,
type: String,
tags: { type: [String], index: true } // field level
});

animalSchema.index({ name: 1, type: -1 }); // schema level


当程序启动时,Mongoose为每个在schema定义的索引自动地调用ensureIndex 。Mongoose会连续为每个索引调用ensureIndex,当所有ensureIndex调用成功或发生错误在model发出index事件。建议在生产中禁止这种行为因为索引创建能够导致显著的性能影响。通过给schema设置autoIndex选项为false来禁用行为,或者在connection全局设置选项config.autoIndex为false。

animalSchema.set('autoIndex', false);
// or
new Schema({..}, { autoIndex: false });


Model#ensureIndexes 方法

6、Virtuals

virtual 是你能 get 和 set 但不能保存到 MongoDB 的 document 属性。getter用于格式化或符合的field,而setter用于de-composing一个单值到多值存储。

// define a schema
var personSchema = new Schema({
name: {
first: String,
last: String
}
});

// compile our model
var Person = mongoose.model('Person', personSchema);

// create a document
var bad = new Person({
name: { first: 'Walter', last: 'White' }
});


假设我们想要记录bad的全名,我们这样做:

console.log(bad.name.first + ' ' + bad.name.last); // Walter White


或者我们可以在 personSchema 定义一个 virtual属性getter, 因此我们不需每次写这些字符串连接

personSchema.virtual('name.full').get(function () {
return this.name.first + ' ' + this.name.last;
});


现在,当我们使用虚拟属性name.full,getter函数会被执行并且返回值

console.log('%s is insane', bad.name.full); // Walter White is insane


主要如果结果记录转换为object或者JSON,默认不包括virtual。

通过设置 this.name.full 能设置this.name.first和 this.name.last 是令人高兴的。例如,我们想要分别改变 bad 的 name.first 和 name.last 为 'Breaking' 和 'bad',可以这样:

bad.name.full = 'Breaking Bad';


Mongoose也通过它的 virtual property setters让你这么做

personSchema.virtual('name.full').set(function (name) {
var split = name.split(' ');
this.name.first = split[0];
this.name.last = split[1];
});

...

mad.name.full = 'Breaking Bad';
console.log(mad.name.first); // Breaking
console.log(mad.name.last);  // Bad


虚拟属性setter在其他验证前应用。因此上面的例子仍然能工作。即使需要 name.first 和name.last fields。

作为查询的一部分和对于field选择,只有非虚拟属性有效。

7、option

new Schema({..}, options);

// or

var schema = new Schema({..});
schema.set(option, value);


有效的选项

autoIndex

capped

collection

emitIndexErrors

id

_id

minimize

read

safe

shardKey

strict

toJSON

toObject

typeKey

validateBeforeSave

versionKey

skipVersioning

timestamps
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: