如何在我的猫鼬集合中插入自动递增编号 [英] how to insert autoincrement number with my mongoose collection

查看:70
本文介绍了如何在我的猫鼬集合中插入自动递增编号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是猫鼬的新手,我有这样的猫鼬架构:

I am newbie to mongoose, I have a mongoose schema like this:

var user = mongoose.Schema({
    userID: {
        type: String,
        required:true
    },
    seq: {
        type: Number,
        default: 0
    },
    firstname: {
        type: String
    },
    lastname: {
        type: String
    },
    dob: {
        type: String
    },
    email: {
        type: String,
        required: true
    },
    username: {
        type: String,
        required: true
    },
    displayname: {
        type: String
    },
    password: {
        type: String,
        required: true
    },
    mobile: {
        type: String
    },
    profilePic: {
        type: String
    },
    city: {
        type: String
    },
    gender: {
        type: String
    },
    profileType: {
        type: String,
        required: true
    },
    profileId: {
        type: String
    },
    isActive: {
        type: Number
    },
    ageVerified: {
        type: String
    },
    ipAddress: {
        type: String
    },
    key: {
        type: String
    },
    osType: {
        type: String
    },
    osVersion: {
        type: String
    },
    deviceName: {
        type: String
    },
    joinedDate: {
        type: String
    },
    connectedAccounts: [{
        profileType: {
            type: String
        },
        profileId: {
            type: String
        },
        email: {
            type: String
        }
    }]
}, {collection: 'user'});

请注意,userID是一个自动递增数字字段,用于使用mongoose查询插入值,例如:

Please note the userID is an auto increment number field, for inserting value am using the mongoose query like:

new user(contents).save(function (err,doc){};

'contents'是一个对象,其中包含除userID以外的所有字段的数据,这是我的问题是在为其他字段插入记录时如何为userID(autoincrement number)插入值?我引用此链接来设置自动增量价值...但是我不知道如何在猫鼬中使用它?

'contents' is a object, which contain data for all the field except userID, here my question is how to insert value for the userID(autoincrement number) while inserting records for other fields? And I refer this linkto set the auto increment value... But I don't know how to use this in mongoose?

推荐答案

在MongoDB教程之后,

Following the MongoDB tutorial, Create an Auto-Incrementing Sequence Field, you need to first create a separate counters collection to track the last number sequence used. The _id field contains the sequence name i.e. the userID field in the user collection and the seq field contains the last value of the sequence.

首先,将userID的初始值插入计数器集合:

To start with, insert into the counters collection the initial value for the userID:

db.counter.insert(
   {
      "_id": "userID",
      "seq": 0
   }
)

填充了counters集合,并在Mongoose中生成了其架构:

Having populated the counters collection, generate its schema in Mongoose:

var counterSchema = mongoose.Schema({
    "_id": { "type": String, "required": true },
    "seq": { "type": Number, "default": 0 }
});

var counter = mongoose.model('counter', counterSchema);

然后重新定义用户架构,以便在保存用户模型时,它首先调用计数器模型的findByIdAndUpdate()方法以原子方式递增seq值并返回此新值,然后将该新值用作下一个userID值:

Then redefine your user schema so that when you save a user model it first calls the counter model's findByIdAndUpdate() method to atomically increment the seq value and return this new value which can then be used as the next userID value:

var userSchema = mongoose.Schema({
    "userID": { "type": String, "required": true },
    "firstname": { "type": String },
    "lastname": { "type": String },
    // other properties ...
    }, { "collection": "user" }
);

userSchema.pre("save", function (next) {
    var doc = this;
    counter.findByIdAndUpdate(
        { "_id": "userID" }, 
        { "$inc": { "seq": 1 } }
    , function(error, counter)   {
        if(error) return next(error);
        doc.userID = counter.seq.toString();
        next();
    });
});

这篇关于如何在我的猫鼬集合中插入自动递增编号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆