[譯]Mongoose指南 - Schema


定義schema

用mongoose的第一件事情就應該是定義schema. schema是什么呢? 它類似於關系數據庫的表結構.

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);

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

實例化方法

model的實例是document. document有許多內置的實例方法. 我們可以為document定義自己的實例方法

var animalSchema = new Schema({name: String, type: String});

//定義實例方法
animalSchema.methods.findSimilarType = function(cb){
  return this.model('Animal').find({type: this.type}, cb);
}

現在animal實例有findSimilarTypes方法了

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

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

Model靜態方法

還可以給Model添加靜態方法

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索引

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

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

Virtual

virtual是document的屬性 你可以get,set他們但是不持續化到MongoDB. virtual屬性get非常有用可以格式化或者合並字段,  set可以分解一個字段到多個字段並持續化到數據庫

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的全名 你需要這樣做

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

或者我們可以在personSchema中定義virtual getter. 這樣我們就不需要在每個要用fullname的地方拼接字符串了

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

現在我么可以使用 name.full虛屬性了

console.log(bad.name.full);

我們還可以通過設置this.name.full來設置this.name.first和this.name.last

bad.name.full = "Breaking Bad";

 

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有一些配置選項, 可以如下面一樣設置

new Schema({}, options);

//or
var xxSchema = new Schema({});
xxSchema.set(option, value);

option:autoIndex

應用啟動的時候Mongoose會自動為每一個schema發送一個ensureIndex命令。  如果你想禁止自動創建index要自己手動來創建的話 你可以設置autoIndex為false

var xxSchema = new Schema({}, { autoIndex: false });

var Clock = mongoose.model('Clock', xxSchema);
Clock.ensureIndexs(callback);

option:bufferCommands

todo

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

option:capped

todo 

....


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM