- 英文原版
- 关于中文文档
- 快速上手
- 模式(Schemas)
- 模式类型(SchemaTypes)
- 连接(Connections)
- 模型(Models)
- 文档 (Documents)
- 子文档(Subdocuments)
- 查询 (queries)
- 验证 (validation)
- 中间件 (middleware)
- 填充 (Populate)
- 鉴别器 (Discriminators)
- 插件
- AWS Lambda
- API 文档
- Schema
- Connection
- Document
- Model
- Query
- Aggregate
- SchemaType
- VirtualType
- Error
- Version Compatibility
- FAQ
插件
Schema 是可拓展的,你可以用打包好的功能拓展你的 Schema。这是一个很实用的特性。
试想下数据库里有很多个 collection,我们需要对它们都添加 记录“最后修改”的功能。使用插件,我们很容易做到。 创建一次插件,然后应用到每个 Schema 就好了。
// lastMod.jsmodule.exports = exports = function lastModifiedPlugin (schema, options) {schema.add({ lastMod: Date });schema.pre('save', function (next) {this.lastMod = new Date();next();});if (options && options.index) {schema.path('lastMod').index(options.index);}}// game-schema.jsvar lastMod = require('./lastMod');var Game = new Schema({ ... });Game.plugin(lastMod, { index: true });// player-schema.jsvar lastMod = require('./lastMod');var Player = new Schema({ ... });Player.plugin(lastMod);
这样就已经在 Game 和 Player 添加了记录最后修改功能, 同时对 game 的 lastMod 添加索引。这寥寥几行代码,看起来不错。
全局插件
想对所有 schema 注册插件?可以使用 mongoose 单例提供的 .plugin() 函数,请看例子:
var mongoose = require('mongoose');mongoose.plugin(require('./lastMod'));var gameSchema = new Schema({ ... });var playerSchema = new Schema({ ... });// `lastModifiedPlugin` gets attached to both schemasvar Game = mongoose.model('Game', gameSchema);var Player = mongoose.model('Player', playerSchema);
社区!
你还可以逛逛 Mongoose 社区!看看有什么好用的插件,当然你也可以把自己的插件分享到社区。 你只要把插件发布到 npm 并打上 mongoose 的 tag, 就能在我们的插件专页搜索到啦。
mongoose