Mongoose指南

来源:互联网 发布:佳能打印机清零软件 编辑:程序博客网 时间:2024/06/06 15:15

原文链接:http://www.cnblogs.com/irocker/p/mongoose-schema.html#top

定义schema

用mongoose的第一件事情就应该是定义schema. schema是什么呢? 它类似于关系数据库的表结构.

1
2
3
4
5
6
7
8
9
10
var mongoose = require('mongoose');
var schema = mongoose.Schema;
 
var blogSchema = new Schema({
    titile: String,
    body: String,
    comments: [{body: String, date: Date}],
    date: {type: Date, default: Date.now},
    hidden:Boolen
});

创建model

格式是mongoose.model(modelName, schema);

1
var BlogModel = mongoose.model('Blog', blogSchema);

实例化方法

model的实例是document. document有许多内置的实例方法. 我们可以为document定义自己的实例方法

1
2
3
4
5
6
var animalSchema = new Schema({name: String, type: String});
 
//定义实例方法
animalSchema.methods.findSimilarType = function(cb){
  return this.model('Animal').find({type: this.type}, cb);
}

现在animal实例有findSimilarTypes方法了

1
2
3
4
5
6
var Animal = mongoose.model('Animal', animalSchema);
var dog = new Animal({type: 'dog'});
 
dog.findSimilarTypes(function(err, dogs){
  console.log(dogs);
});

Model静态方法

还可以给Model添加静态方法

1
2
3
4
5
6
7
8
9
10
animalSchema.statics.findByName = function(name, cb){
   this.find({name: new RegExp(name, 'i')}, cb);
}
 
 
var Animal = mongoose.model('Animal', animalSchema);
 
Animal.findByName('fido'function(err, animals){
  console.log(animals);
});

索引

索引分为field级别和schema级别. 如果使用复合索引那么必须使用schema索引

1
2
3
4
5
6
7
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确保生成索引. 开发环境用这个很好, 但是建议在生产环境不要使用这个.使用下面的方法禁用ensureIndex

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

Virtual

virtual是document的属性 你可以get,set他们但是不持续化到MongoDB. virtual属性get非常有用可以格式化或者合并字段,  set可以分解一个字段到多个字段并持续化到数据库

1
2
3
4
5
6
7
8
9
10
11
12
13
var personSchema = new Schema({
    name: {
        first: String,
        last: String
    }
});
 
 
var Person = mongoose.model('Person', personSchema);
 
var bad = new Person({
   name: {first: 'Walter', last: 'White'}
});

如果你想获取bad的全名 你需要这样做

1
console.log(bad.name.first + ' ' + bad.name.last);

或者我们可以在personSchema中定义virtual getter. 这样我们就不需要在每个要用fullname的地方拼接字符串了

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

现在我么可以使用 name.full虚属性了

1
console.log(bad.name.full);

我们还可以通过设置this.name.full来设置this.name.first和this.name.last

1
bad.name.full = "Breaking Bad";

 

1
2
3
4
5
6
7
8
9
10
11
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

  

 Options

Schema有一些配置选项, 可以如下面一样设置

1
2
3
4
5
new Schema({}, options);
 
//or
var xxSchema = new Schema({});
xxSchema.set(option, value);

option:autoIndex

应用启动的时候Mongoose会自动为每一个schema发送一个ensureIndex命令。  如果你想禁止自动创建index要自己手动来创建的话 你可以设置autoIndex为false

1
2
3
4
var xxSchema = new Schema({}, { autoIndex: false });
 
var Clock = mongoose.model('Clock', xxSchema);
Clock.ensureIndexs(callback);

option:bufferCommands

todo

1
var schema = new Schema({}, { bufferCommands: false });

option:capped

todo 

....


阅读全文
'); })();
0 0
原创粉丝点击
热门IT博客
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 调味油 葱花油 豉油 香料油 虎图 虎, 虎穴 四人虎影 虎视眈眈意思 四虎国产 虎视眈眈 你是不是虎啥意思 虎拼音 虎的拼音 虎繁体 虎是什么意思 金鸡虎补丸 虎标万金油价格 香港虎标万金油 属虎的带貔貅好吗 虎斑木地板 虎斑虾 虎塘沟 虎阳生力片价格 虎白女51p图鉴 虎白女51p 虎白女p51 自虎馒头51p 虎丘 虎丘风景区 虎丘旅游 虎丘塔 虎丘剑池 虎丘门票 虎丘婚纱城 虎丘婚纱 虎丘山 虎丘区 虎丘塔简介 苏州虎丘区