Schemas - 图1mongoose

Schemas - 图2mongoose

Schemas

建议先阅读快速上手,简单了解一下Mongoose的工作流程。 如果你要从4.x迁移到5.x,请阅读迁移指引.

定义一个schema

Mongoose 的一切始于 Schema。每个 schema 都会映射到一个 MongoDB collection ,并定义这个collection里的文档的构成。

  1. var mongoose = require('mongoose');
  2. var Schema = mongoose.Schema;
  3. var blogSchema = new Schema({
  4. title: String,
  5. author: String,
  6. body: String,
  7. comments: [{ body: String, date: Date }],
  8. date: { type: Date, default: Date.now },
  9. hidden: Boolean,
  10. meta: {
  11. votes: Number,
  12. favs: Number
  13. }
  14. });

在这之后你还想添加 keys 的话, 请使用 Schema#add 方法。

document 里每个属性的类型都会被转换为 在 blogSchema 里定义对应的 SchemaType。 例如 title 属性会被转换为 SchemaType String, 而 date属性会被转换为 SchemaType Date。 还可以像上面 meta 属性,更详细地指定嵌套在里面的属性类型。

允许使用的 SchemaTypes 有:

  • String
  • Number
  • Date
  • Buffer
  • Boolean
  • Mixed
  • ObjectId
  • Array

更多关于 SchemaTypes

Schema的功能不只是定义文档结构和属性类型,它可以定义——

创建一个 model

我们要把 schema 转换为一个 Model, 使用 mongoose.model(modelName, schema) 函数:

  1. var Blog = mongoose.model('Blog', blogSchema);
  2. // ready to go!

实例方法(method)

documentsModels 的实例。 Document 有很多自带的实例方法, 当然也可以自定义我们自己的方法。

  1. // define a schema
  2. var animalSchema = new Schema({ name: String, type: String });
  3. // assign a function to the "methods" object of our animalSchema
  4. animalSchema.methods.findSimilarTypes = function(cb) {
  5. return this.model('Animal').find({ type: this.type }, cb);
  6. };

现在所有 animal 实例都有 findSimilarTypes 方法:

  1. var Animal = mongoose.model('Animal', animalSchema);
  2. var dog = new Animal({ type: 'dog' });
  3. dog.findSimilarTypes(function(err, dogs) {
  4. console.log(dogs); // woof
  5. });
  • 重写 mongoose 的默认方法会造成无法预料的结果,相关链接
  • 不要在自定义方法中使用 ES6 箭头函数,会造成 this 指向错误。

静态方法(static)

添加 Model 的静态方法也十分简单,继续用 animalSchema 举例:

  1. // assign a function to the "statics" object of our animalSchema
  2. animalSchema.statics.findByName = function(name, cb) {
  3. return this.find({ name: new RegExp(name, 'i') }, cb);
  4. };
  5. var Animal = mongoose.model('Animal', animalSchema);
  6. Animal.findByName('fido', function(err, animals) {
  7. console.log(animals);
  8. });

同样不要在静态方法中使用 ES6 箭头函数

查询助手(query helper)

查询助手作用于 query 实例,方便你自定义拓展你的 链式查询

  1. animalSchema.query.byName = function(name) {
  2. return this.find({ name: new RegExp(name, 'i') });
  3. };
  4. var Animal = mongoose.model('Animal', animalSchema);
  5. Animal.find().byName('fido').exec(function(err, animals) {
  6. console.log(animals);
  7. });

索引(index)

MongoDB 支持 secondary indexes. 在 mongoose 中,我们在 Schema 中定义索引。索引分字段级别和schema级别, 复合索引 需要在 schema 级别定义。

  1. var animalSchema = new Schema({
  2. name: String,
  3. type: String,
  4. tags: { type: [String], index: true } // field level
  5. });
  6. animalSchema.index({ name: 1, type: -1 }); // schema level

应用启动时, Mongoose 会自动调用 createIndex 初始化你定义的索引。 Mongoose 顺序处理每一个 createIndex ,然后在model触发 'index' 事件。 While nice for development, it is recommended this behavior be disabled in production since index creation can cause a significant performance impact. Disable the behavior by setting the autoIndex option of your schema to false, or globally on the connection by setting the option autoIndex to false.

  1. mongoose.connect('mongodb://user:pass@localhost:port/database', { autoIndex: false });
  2. // or
  3. mongoose.createConnection('mongodb://user:pass@localhost:port/database', { autoIndex: false });
  4. // or
  5. animalSchema.set('autoIndex', false);
  6. // or
  7. new Schema({..}, { autoIndex: false });

索引构建完成或失败后,Mongoose 会触发 index 事件。

  1. // Will cause an error because mongodb has an _id index by default that
  2. // is not sparse
  3. animalSchema.index({ _id: 1 }, { sparse: true });
  4. var Animal = mongoose.model('Animal', animalSchema);
  5. Animal.on('index', function(error) {
  6. // "_id index cannot be sparse"
  7. console.log(error.message);
  8. });

相关链接 Model#ensureIndexes

虚拟值(Virtual)

Virtuals 是 document 的属性,但是不会被保存到 MongoDB。 getter 可以用于格式化和组合字段数据, setter 可以很方便地分解一个值到多个字段。

  1. // define a schema
  2. var personSchema = new Schema({
  3. name: {
  4. first: String,
  5. last: String
  6. }
  7. });
  8. // compile our model
  9. var Person = mongoose.model('Person', personSchema);
  10. // create a document
  11. var axl = new Person({
  12. name: { first: 'Axl', last: 'Rose' }
  13. });

如果你要log出全名,可以这么做:

  1. console.log(axl.name.first + ' ' + axl.name.last); // Axl Rose

但是每次都这么拼接实在太麻烦了, 推荐你使用virtual property getter, 这个方法允许你定义一个 fullName 属性,但不必保存到数据库。

  1. personSchema.virtual('fullName').get(function () {
  2. return this.name.first + ' ' + this.name.last;
  3. });

现在, mongoose 可以调用 getter 函数访问 fullName 属性:

  1. console.log(axl.fullName); // Axl Rose

如果对 document 使用 toJSON()toObject(),默认不包括虚拟值, 你需要额外向 toObject() 或者 toJSON() 传入参数 { virtuals: true }

你也可以设定虚拟值的 setter ,下例中,当你赋值到虚拟值时,它可以自动拆分到其他属性:

  1. personSchema.virtual('fullName').
  2. get(function() { return this.name.first + ' ' + this.name.last; }).
  3. set(function(v) {
  4. this.name.first = v.substr(0, v.indexOf(' '));
  5. this.name.last = v.substr(v.indexOf(' ') + 1);
  6. });
  7. axl.fullName = 'William Rose'; // Now `axl.name.first` is "William"

再次强调,虚拟值不能用于查询和字段选择,因为虚拟值不储存于 MongoDB。

别名(Alias)

Aliase 是一种特殊的虚拟值,它的 getter 和 setter 会无缝链接到另一个值。这是一个节省带宽的做法, 你可以储存一个更短的属性名到数据库,同时在调用的时候保持可读性。

  1. var personSchema = new Schema({
  2. n: {
  3. type: String,
  4. // Now accessing `name` will get you the value of `n`, and setting `n` will set the value of `name`
  5. alias: 'name'
  6. }
  7. });
  8. // Setting `name` will propagate to `n`
  9. var person = new Person({ name: 'Val' });
  10. console.log(person); // { n: 'Val' }
  11. console.log(person.toObject({ virtuals: true })); // { n: 'Val', name: 'Val' }
  12. console.log(person.name); // "Val"
  13. person.name = 'Not Val';
  14. console.log(person); // { n: 'Not Val' }

选项

Schemas 有很多可配置选项,你可以在构造时传入或者直接 set

  1. new Schema({..}, options);
  2. // or
  3. var schema = new Schema({..});
  4. schema.set(option, value);

Valid options:

option: autoIndex

应用启动时,Mongoose 自动发送 createIndex 指令, schema 里的每个 index 都会被创建。 如果你需要关闭自动创建功能或者需要在创建后进行一系列操作, 可以把 autoIndex 设为 false, 然后对 model 调用 ensureIndexes

  1. var schema = new Schema({..}, { autoIndex: false });
  2. var Clock = mongoose.model('Clock', schema);
  3. Clock.ensureIndexes(callback);

option: bufferCommands

By default, mongoose buffers commands when the connection goes down until the driver manages to reconnect. To disable buffering, set bufferCommands to false.

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

The schema bufferCommands option overrides the global bufferCommands option.

  1. mongoose.set('bufferCommands', true);
  2. // Schema option below overrides the above, if the schema option is set.
  3. var schema = new Schema({..}, { bufferCommands: false });

option: capped

Mongoose 支持 MongoDB 的 capped collections。 要从底层把 collection 设定为 capped (封顶), 可以把 collection 的最大容量设定到 capped 选项(单位bytes)。

  1. new Schema({..}, { capped: 1024 });

The capped option may also be set to an object if you want to pass additional options like max or autoIndexId. 这个情况下你需要显式传入必要值 size

  1. new Schema({..}, { capped: { size: 1024, max: 1000, autoIndexId: true } });

option: collection

Mongoose 通过 utils.toCollectionName 方法 默认生成 collection 的名称(生成 model 名称的复数形式)。 设置这个选项可以自定义名称。

  1. var dataSchema = new Schema({..}, { collection: 'data' });

option: id

Mongoose 会默认生成一个虚拟值 id,指向文档的 _id 字段。 如果你不需要 id 虚拟值,可以通过这个选项禁用此功能。

  1. // default behavior
  2. var schema = new Schema({ name: String });
  3. var Page = mongoose.model('Page', schema);
  4. var p = new Page({ name: 'mongodb.org' });
  5. console.log(p.id); // '50341373e894ad16347efe01'
  6. // disabled id
  7. var schema = new Schema({ name: String }, { id: false });
  8. var Page = mongoose.model('Page', schema);
  9. var p = new Page({ name: 'mongodb.org' });
  10. console.log(p.id); // undefined

option: _id

Mongoose 默认给你的 Schema 赋值一个 _id。 这个值的类型是 ObjectId,这与MongoDB的默认表现一致。 如果你不需要 _id,可以通过这个选项禁用此功能。

此选项只能用于 subdocument。 Mongoose 不能保存没有id的文档,如果你硬是要这么做,会报错的哦。

  1. // default behavior
  2. var schema = new Schema({ name: String });
  3. var Page = mongoose.model('Page', schema);
  4. var p = new Page({ name: 'mongodb.org' });
  5. console.log(p); // { _id: '50341373e894ad16347efe01', name: 'mongodb.org' }
  6. // disabled _id
  7. var childSchema = new Schema({ name: String }, { _id: false });
  8. var parentSchema = new Schema({ children: [childSchema] });
  9. var Model = mongoose.model('Model', parentSchema);
  10. Model.create({ children: [{ name: 'Luke' }] }, function(error, doc) {
  11. // doc.children[0]._id will be undefined
  12. });

option: minimize

Mongoose 默认不保存空对象。

  1. var schema = new Schema({ name: String, inventory: {} });
  2. var Character = mongoose.model('Character', schema);
  3. // will store `inventory` field if it is not empty
  4. var frodo = new Character({ name: 'Frodo', inventory: { ringOfPower: 1 }});
  5. Character.findOne({ name: 'Frodo' }, function(err, character) {
  6. console.log(character); // { name: 'Frodo', inventory: { ringOfPower: 1 }}
  7. });
  8. // will not store `inventory` field if it is empty
  9. var sam = new Character({ name: 'Sam', inventory: {}});
  10. Character.findOne({ name: 'Sam' }, function(err, character) {
  11. console.log(character); // { name: 'Sam' }
  12. });

如果把 minimize 选项设为 false,Mongoose 将保存空对象。

  1. var schema = new Schema({ name: String, inventory: {} }, { minimize: false });
  2. var Character = mongoose.model('Character', schema);
  3. // will store `inventory` if empty
  4. var sam = new Character({ name: 'Sam', inventory: {}});
  5. Character.findOne({ name: 'Sam' }, function(err, character) {
  6. console.log(character); // { name: 'Sam', inventory: {}}
  7. });

option: read

Allows setting query#read options at the schema level, providing us a way to apply default ReadPreferences to all queries derived from a model.

  1. var schema = new Schema({..}, { read: 'primary' }); // also aliased as 'p'
  2. var schema = new Schema({..}, { read: 'primaryPreferred' }); // aliased as 'pp'
  3. var schema = new Schema({..}, { read: 'secondary' }); // aliased as 's'
  4. var schema = new Schema({..}, { read: 'secondaryPreferred' }); // aliased as 'sp'
  5. var schema = new Schema({..}, { read: 'nearest' }); // aliased as 'n'

The alias of each pref is also permitted so instead of having to type out 'secondaryPreferred' and getting the spelling wrong, we can simply pass 'sp'.

The read option also allows us to specify tag sets. These tell the driver from which members of the replica-set it should attempt to read. Read more about tag sets here and here.

NOTE: you may also specify the driver read pref strategy option when connecting:

  1. // pings the replset members periodically to track network latency
  2. var options = { replset: { strategy: 'ping' }};
  3. mongoose.connect(uri, options);
  4. var schema = new Schema({..}, { read: ['nearest', { disk: 'ssd' }] });
  5. mongoose.model('JellyBean', schema);

option: shardKey

分片相关 The shardKey option is used when we have a sharded MongoDB architecture. Each sharded collection is given a shard key which must be present in all insert/update operations. We just need to set this schema option to the same shard key and we’ll be all set.

  1. new Schema({ .. }, { shardKey: { tag: 1, name: 1 }})

Note that Mongoose does not send the shardcollection command for you. You must configure your shards yourself.

option: strict

Strict 选项默认为 true,这意味着你不能 save schema 里没有声明的属性。

  1. var thingSchema = new Schema({..})
  2. var Thing = mongoose.model('Thing', thingSchema);
  3. var thing = new Thing({ iAmNotInTheSchema: true });
  4. thing.save(); // iAmNotInTheSchema is not saved to the db
  5. // set to false..
  6. var thingSchema = new Schema({..}, { strict: false });
  7. var thing = new Thing({ iAmNotInTheSchema: true });
  8. thing.save(); // iAmNotInTheSchema is now saved to the db!!

doc.set() 也受该选项影响:

  1. var thingSchema = new Schema({..})
  2. var Thing = mongoose.model('Thing', thingSchema);
  3. var thing = new Thing;
  4. thing.set('iAmNotInTheSchema', true);
  5. thing.save(); // iAmNotInTheSchema is not saved to the db

这个值可以在 model 级别重写,在第二个参数值传入:

  1. var Thing = mongoose.model('Thing');
  2. var thing = new Thing(doc, true); // enables strict mode
  3. var thing = new Thing(doc, false); // disables strict mode

The strict option may also be set to "throw" which will cause errors to be produced instead of dropping the bad data.

NOTE: Any key/val set on the instance that does not exist in your schema is always ignored, regardless of schema option.

  1. var thingSchema = new Schema({..})
  2. var Thing = mongoose.model('Thing', thingSchema);
  3. var thing = new Thing;
  4. thing.iAmNotInTheSchema = true;
  5. thing.save(); // iAmNotInTheSchema is never saved to the db

option: strictQuery

For backwards compatibility, the strict option does not apply to the filter parameter for queries.

  1. const mySchema = new Schema({ field: Number }, { strict: true });
  2. const MyModel = mongoose.model('Test', mySchema);
  3. // Mongoose will **not** filter out `notInSchema: 1`, despite `strict: true`
  4. MyModel.find({ notInSchema: 1 });

The strict option does apply to updates.

  1. // Mongoose will strip out `notInSchema` from the update if `strict` is
  2. // not `false`
  3. MyModel.updateMany({}, { $set: { notInSchema: 1 } });

Mongoose has a separate strictQuery option to toggle strict mode for the filter parameter to queries.

  1. const mySchema = new Schema({ field: Number }, {
  2. strict: true,
  3. strictQuery: true // Turn on strict mode for query filters
  4. });
  5. const MyModel = mongoose.model('Test', mySchema);
  6. // Mongoose will strip out `notInSchema: 1` because `strictQuery` is `true`
  7. MyModel.find({ notInSchema: 1 });

option: toJSON

Exactly the same as the toObject option but only applies when the documents toJSON method is called.

  1. var schema = new Schema({ name: String });
  2. schema.path('name').get(function (v) {
  3. return v + ' is my name';
  4. });
  5. schema.set('toJSON', { getters: true, virtuals: false });
  6. var M = mongoose.model('Person', schema);
  7. var m = new M({ name: 'Max Headroom' });
  8. console.log(m.toObject()); // { _id: 504e0cd7dd992d9be2f20b6f, name: 'Max Headroom' }
  9. console.log(m.toJSON()); // { _id: 504e0cd7dd992d9be2f20b6f, name: 'Max Headroom is my name' }
  10. // since we know toJSON is called whenever a js object is stringified:
  11. console.log(JSON.stringify(m)); // { "_id": "504e0cd7dd992d9be2f20b6f", "name": "Max Headroom is my name" }

To see all available toJSON/toObject options, read this.

option: toObject

Documents 的 toObject 方法可以把文档转换成一个 plain javascript object (也就是去掉里面的方法)。 这是一个可以接收多个参数的方法,我们可以在 schemas 定义这些参数。

例如要打印出虚拟值,可以向 toObject 传入 { getters: true }

  1. var schema = new Schema({ name: String });
  2. schema.path('name').get(function (v) {
  3. return v + ' is my name';
  4. });
  5. schema.set('toObject', { getters: true });
  6. var M = mongoose.model('Person', schema);
  7. var m = new M({ name: 'Max Headroom' });
  8. console.log(m); // { _id: 504e0cd7dd992d9be2f20b6f, name: 'Max Headroom is my name' }

完整的 toObject 选项请看这里

option: typeKey

By default, if you have an object with key 'type' in your schema, mongoose will interpret it as a type declaration.

  1. // Mongoose interprets this as 'loc is a String'
  2. var schema = new Schema({ loc: { type: String, coordinates: [Number] } });

However, for applications like geoJSON, the 'type' property is important. If you want to control which key mongoose uses to find type declarations, set the 'typeKey' schema option.

  1. var schema = new Schema({
  2. // Mongoose interpets this as 'loc is an object with 2 keys, type and coordinates'
  3. loc: { type: String, coordinates: [Number] },
  4. // Mongoose interprets this as 'name is a String'
  5. name: { $type: String }
  6. }, { typeKey: '$type' }); // A '$type' key means this object is a type declaration

option: validateBeforeSave

By default, documents are automatically validated before they are saved to the database. This is to prevent saving an invalid document. If you want to handle validation manually, and be able to save objects which don't pass validation, you can set validateBeforeSave to false.

  1. var schema = new Schema({ name: String });
  2. schema.set('validateBeforeSave', false);
  3. schema.path('name').validate(function (value) {
  4. return v != null;
  5. });
  6. var M = mongoose.model('Person', schema);
  7. var m = new M({ name: null });
  8. m.validate(function(err) {
  9. console.log(err); // Will tell you that null is not allowed.
  10. });
  11. m.save(); // Succeeds despite being invalid

option: versionKey

versionKey 是 Mongoose 在文件创建时自动设定的。 这个值包含文件的内部修订号versionKey 是一个字符串,代表版本号的属性名, 默认值为 __v。如果这个值与你的计划冲突,你可以设定为其他名称:

  1. var schema = new Schema({ name: 'string' });
  2. var Thing = mongoose.model('Thing', schema);
  3. var thing = new Thing({ name: 'mongoose v3' });
  4. thing.save(); // { __v: 0, name: 'mongoose v3' }
  5. // customized versionKey
  6. new Schema({..}, { versionKey: '_somethingElse' })
  7. var Thing = mongoose.model('Thing', schema);
  8. var thing = new Thing({ name: 'mongoose v3' });
  9. thing.save(); // { _somethingElse: 0, name: 'mongoose v3' }

你也可以赋值为 false 禁用 versionKey你不应该随便禁用这个功能,除非你清楚知道这有什么影响

  1. new Schema({..}, { versionKey: false });
  2. var Thing = mongoose.model('Thing', schema);
  3. var thing = new Thing({ name: 'no versioning please' });
  4. thing.save(); // { name: 'no versioning please' }

option: collation

为 查询(query)和 聚合(aggregation)设置 collation。 这里有一份友好的 collation 指南

  1. var schema = new Schema({
  2. name: String
  3. }, { collation: { locale: 'en_US', strength: 1 } });
  4. var MyModel = db.model('MyModel', schema);
  5. MyModel.create([{ name: 'val' }, { name: 'Val' }]).
  6. then(function() {
  7. return MyModel.find({ name: 'val' });
  8. }).
  9. then(function(docs) {
  10. // `docs` will contain both docs, because `strength: 1` means
  11. // MongoDB will ignore case when matching.
  12. });

option: skipVersioning

skipVersioning allows excluding paths from versioning (i.e., the internal revision will not be incremented even if these paths are updated). DO NOT do this unless you know what you're doing. For subdocuments, include this on the parent document using the fully qualified path.

  1. new Schema({..}, { skipVersioning: { dontVersionMe: true } });
  2. thing.dontVersionMe.push('hey');
  3. thing.save(); // version is not incremented

option: timestamps

如果设置了 timestamps 选项, mongoose 会在你的 schema 自动添加 createdAtupdatedAt 字段, 其类型为 Date

这两个字段的默认名是 createdAtupdatedAt, 你可以通过设定 timestamps.createdAttimestamps.updatedAt 自定义字段名称。

  1. var thingSchema = new Schema({..}, { timestamps: { createdAt: 'created_at' } });
  2. var Thing = mongoose.model('Thing', thingSchema);
  3. var thing = new Thing();
  4. thing.save(); // `created_at` & `updatedAt` will be included

option: useNestedStrict

在 mongoose4 中,update()findOneAndUpdate()只检查顶层 schema 的严格模式设定。

  1. var childSchema = new Schema({}, { strict: false });
  2. var parentSchema = new Schema({ child: childSchema }, { strict: 'throw' });
  3. var Parent = mongoose.model('Parent', parentSchema);
  4. Parent.update({}, { 'child.name': 'Luke Skywalker' }, function(error) {
  5. // 报错!原因是父Schema设定为`strict: throw`,但是因为只检查顶层,导致
  6. // 子Schema的 `strict: false` 遭到无情忽视
  7. });
  8. var update = { 'child.name': 'Luke Skywalker' };
  9. var opts = { strict: false };
  10. Parent.update({}, update, opts, function(error) {
  11. // 这样可以,因为重写了父Schema的 strict 选项
  12. });

如果你把 useNestedStrict 设为 true,mongoose 就不会忽略嵌套的 strict 设定。

  1. var childSchema = new Schema({}, { strict: false });
  2. var parentSchema = new Schema({ child: childSchema },
  3. { strict: 'throw', useNestedStrict: true });
  4. var Parent = mongoose.model('Parent', parentSchema);
  5. Parent.update({}, { 'child.name': 'Luke Skywalker' }, function(error) {
  6. // Works!
  7. });

Pluggable

Schemas 是 pluggable(可扩展的), 我们可以打包插件分享到社区,或者复用于自己的项目。

下一步

这章我们介绍了 Schemas,下一个章节将会介绍 SchemaTypes