简体   繁体   English

如何在猫鼬中创建自动增量字段

[英]how can I create a auto-increment field in mongoose

can any one tell me how can I create a auto-increment field in mongoose without using any library or plugin.谁能告诉我如何在不使用任何库或插件的情况下在 mongoose 中创建自动增量字段。 Any simple and easy way..任何简单易行的方法..

there is a very simple and easy way to implement auto increment in mongoose by using mongoose pre save function.通过使用mongoose 预保存功能,有一种非常简单易行的方法可以在 mongoose 中实现自动增量。 Here are my codes for user collection, try the same trick in your mongoose model(schema) to implement auto increment.这是我的用户收集代码,在你的猫鼬模型(模式)中尝试相同的技巧来实现自动增量。 Paste this code in your collection schema file and replace some variables ( userSchema, user ) according to your collection schema .将此代码粘贴到您的集合架构文件中,并根据您的集合架构替换一些变量userSchema, user )。

userSchema.pre('save', function(next) {
    var currentDate = new Date();
    this.updated_at = currentDate;

    if (!this.created_at)
        this.created_at = currentDate;

    var sno = 1;
    var user = this;
    User.find({}, function(err, users) {
    if (err) throw err;
        sno = users.length + 1;
        user.sno = sno;
        next();
    });
});

If you are using soft delete functionality above answer may work but to solve it, there are several ways.如果您使用上面的软删除功能,答案可能会起作用,但要解决它,有几种方法。 1: Before inserting a new record fetch the last document(row) and put id = 1 + (id of the last record) . 1:在插入新记录之前获取最后一个文档(行)并放置id = 1 + (id of the last record) 2: You can use plugin mongoose-auto-increment . 2:您可以使用插件mongoose-auto-increment 3: Last but not least option save the last incremented value in a separate collection(collection name: autoIncrement columns will be collection and sno), before creating a record fetch the sno value of that collection increment it, create your record and update the sno value again in autoIncrement collection. 3:最后但并非最不重要的选项将最后增加的值保存在单独的集合中(集合名称: autoIncrement列将是集合和 sno),在创建记录之前获取该集合的 sno 值增加它,创建您的记录并更新 sno autoIncrement 集合中的再次值。

Bonus:奖金:

1: You can create autoIncrement in redis. 1:可以在redis中创建autoIncrement。

2: The first point of this answer will not able to solve several problems, ie: Your app will do kind of behavior on deletion, if user delete a row from start or middle of your collection, it will remove that id(that sno. form table), but in case the user deletes the last record. 2:这个答案的第一点将无法解决几个问题,即:您的应用程序将在删除时执行某种行为,如果用户从集合的开头或中间删除一行,它将删除该 id(that sno.表格),但万一用户删除了最后一条记录。 It will put that sno.它会放那个sno。 in next record.在下一个记录中。

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();
    });
});

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

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