Node mongoose 結尾總結

liangtongzhuo發表於2017-12-14
    _someId: Schema.Types.ObjectId,
    name : { type:String },//屬性name,型別為String
    age  : { type:Number, default:0 },//屬性age,型別為Number,預設為0
    time : { type:Date, default:Date.now },
    email: { type:String,default:''}
    age : {type: Number, min: 13, max:19}
    day : {type: String, enum: ['monday', 'tuesday', 'wednesday', 'thursday','friday']}

複製程式碼

例項方法

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

animalSchema.methods.findSimilarTypes = function(cb) {
  return this.model('Animal').find({ type: this.type }, cb);
};
var Animal = mongoose.model('Animal', animalSchema);
var dog = new Animal({ type: 'dog' });

dog.findSimilarTypes(function(err, dogs) {
  console.log(dogs); // woof
});
複製程式碼

靜態方法

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);
});
複製程式碼

索引: http://blog.csdn.net/loveyunwt/article/details/8067480

相關文章