模式的扩展
预定义模式修饰符
var UserSchema = new mongoose.Schema({
nickname:{
type:String,
default:'zhong user' //预定义名字
},
regTime:{
type:Date,
default:Date.now //预定义时间
}
});
自定义setter修饰符 (数据保存之前)
var User = mongoose.model('User', {
nickname: {
type: String,
trim: true //修饰符 去空格
},
blog:{
type:String,
set:function(url){ //自定义 方法
if(!url) return url;
if(0 !== url.indexOf("http://") && 0 !== url.indexOf("https://")){
url = "http://" + url;
return url;
}
}
}
});
自定义getter修饰符 ( 取出数据之后 )
var User = mongoose.model('User', {
blog:{
type:String,
get:function(url){
if(!url) return url;
if(0 !== url.indexOf("http://") && 0 !== url.indexOf("https://") ){
url = "http://" + url;
return url;
}
}
}
});
模型的方法
静态方法
var BookSchema = new mongoose.Schema({ //模式
name: String,
isbn: Number
});
//添加 静态方法
BookSchema.statics.findByISBN = function(isbn, cb) {
this.findOne({ isbn: isbn }, function(err, doc) {
cb(err, doc);
});
}
var Book = mongoose.model('Book',BookSchema); //模型
var book = new Book({
name:'NEAN IS FIDE',
isbn:1314799
});
book.save(function(err){
if(err){
return console.log('sava is failed', err);
}
Book.findByISBN(1314799,function(err, doc){
console.log('isok', err , JSON.stringify(doc))
});
book.print();
});
实例方法
var BookSchema = new mongoose.Schema({ //模式
name: String,
isbn: Number
});
//实例方法
BookSchema.methods.print = function(){
console.log(this.name);
console.log(this.isbn);
}
var Book = mongoose.model('Book',BookSchema); //模型
var book = new Book({
name:'NEAN IS FIDE',
isbn:1314799
});
book.save(function(err){
if(err){
return console.log('sava is failed', err);
}
Book.findByISBN(1314799,function(err, doc){
console.log('isok', err , JSON.stringify(doc))
});
book.print();
});
数据的校验
自定义,预定义验证器
var OrderScheme = new mongoose.Schema({
count: {
type: Number,
required: true, //预定义验证器 必须字段
max:1000, //预定义验证器 最大值
min:10 //预定义验证器 最小值
},
status:{
type:String,
enum:['created', 'sucess', 'failed']
},
decs:{
type:String,
match: /book/g, //预定义验证器 正则表达式
validate:function(desc){ //自定义验证器
return desc.length >= 10;
}
}
});
中间件
var ResellerSchema = new mongoose.Schema({
address:String
});
//保存完成之后 后置处理中间件
ResellerSchema.post('save',function(next){
console.log('this is save');
next();
});
//保存之前
ResellerSchema.pre('save', true, function(next, done){
console.log('zhiqian');
next();
done();
});
var Reseller = mongoose.model('Reseller', ResellerSchema);
var reseller = new Reseller({
address:'123456'
});
reseller.save();
虚拟属性
var PersonSchema = new mongoose.Schema({
firstName:String,
lastName:String
});
//虚拟属性 fullName
PersonSchema.virtual('fullName').get(function(){
return this.firstName+' '+ this.lastName;
});
//将虚拟属性添加 到JSON.stringify() 方法里面
PersonSchema.set('toJSON',{ getters:true, virtual:true });
var Person = mongoose.model('Person', PersonSchema);
var person = new Person({
firstName:'zhong',
lastName:'sheng'
});
DBref
var User = mongoose.model('User', {
username: String
});
var News = mongoose.model('News', {
title: String,
author: {
type: mongoose.Schema.ObjectId,
ref: 'User' //关联User 集合
}
});
var user = new User({ username: 'zhong' });
var news = new News({
title: 'congratulation',
author: user
});
user.save(function(err) {
if (err) {
return console.log('so', err)
}
news.save(function(err) {
if (err) {
return console.log('so no', err)
}
// populate 指定填充字段
News.findOne().populate('author').exec(function(err,doc){
console.log(JSON.stringify(doc));
});
});
})