这是一个示例,如何在Mongoose中实现自动增量字段:
var CounterSchema = Schema({ _id: {type: String, required: true}, seq: { type: Number, default: 0 }});var counter = mongoose.model('counter', CounterSchema);var entitySchema = mongoose.Schema({ testvalue: {type: String}});entitySchema.pre('save', function(next) { var doc = this; counter.findByIdAndUpdate({_id: 'entityId'}, {$inc: { seq: 1} }, function(error, counter) { if(error) return next(error); doc.testvalue = counter.seq; next(); });});


