http://mongoosejs.com/docs/guide.html#virtualshtml
每一个Schema映射一个数据库中的集合,限定了文档字段内容数据库
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 } });
String Number Date Buffer Boolean Mixed ObjectId Array
经过Schema建立模型(mongoose.model(modelName, schema))app
var Blog = mongoose.model('Blog', blogSchema);
经过Schema实例方法methods能够为模型(documents)添加实例方法mongoose
// 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); };
经过Schema实例方法statics能够为模型(documents)添加实例方法ide
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://mongoosejs.com/docs/guide.html#autoIndexui
autoIndex bufferCommands capped // 集合的封顶大小byte collection // 设置集合名称 emitIndexErrors // id // 是否容许经过实体.id查询id _id // 是否自动建立_id字段 minimize // 是否清除空字段,默认清除 read // 设置schema level shardKey // 严格模式,传入model的字段不符则不能存入数据库 strict // toJSON // toObject // typeKey // validateBeforeSave // 保存字段验证 versionKey // 设置versionKey,默认_v,可设为false collation // skipVersioning // timestamps // 设置了timestamps,会加入createdAt and updatedAt字段