简体   繁体   English

如何在mongoose模式中定义触发器

[英]how to define a trigger in a mongoose schema

is there any kind of trigger at mongoose model level which provide the capability to set the value of the open field = false when the number of the members collection reaches 100? 在mongoose模型级别是否有任何类型的触发器,当成员集合的数量达到100时,它提供设置open field = false的能力?

var mongoose  = require('mongoose');
var Schema    = mongoose.Schema;

var listSchema = new Schema({
  name: {
    type: String,
    required: true,
    trim: true
  },
  desc: {
    type: String
  },
  open: {
    type: Boolean,
    default: true
  },
  members: [{
    userid: {
      type: Schema.Types.ObjectId, ref: 'User'
    },
    prId: {
      type: Schema.Types.ObjectId, ref: 'PR'
    },
    checkedIn: {
      type: Boolean
    }
  }]
});

module.exports = mongoose.model('List', listSchema);

Triggers are not available in mongo. mongo中没有触发器。 It's hard to say why you want to change documents when collection reaches some limit by maybe capped collection is what you really want? 很难说为什么你想要在收藏达到一定限度时更改文件,可能有限制收藏是你真正想要的?

new Schema({..}, { capped: { size: 1024, max: 100 } });

size is maximum collection size in bytes and max is maximum number of documents that can be inserted in collection. size是最大集合大小(以字节为单位),max是可以在集合中插入的最大文档数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM