鍍金池/ 問答/數(shù)據(jù)庫  HTML/ mongoose創(chuàng)建用戶的時候如何把objectId設為userId

mongoose創(chuàng)建用戶的時候如何把objectId設為userId

const UserSchema = new Schema({
    userId:Schema.Types.ObjectId,
    username: String,
    mobile: String,
    password: String,
    //備注
    remarks: String,
    createAt: {type: Date, default: Date.now},
    updateAt: {type: Date, default: Date.now},
    //用戶類型
    userType: String
})

這是我定義的用戶schema,我希望默認寸數(shù)據(jù)的時候把userId值設為默認的objectId,該如何設?
我在查詢的時候需要根據(jù)用戶id 或者 objectId 查詢數(shù)據(jù), 又該怎么取到 objectId?

下邊是我存數(shù)據(jù)的代碼:

let password = md5(ctx.request.body.password)
            let UserModel = new User({
                username: ctx.request.body.username,
                mobile: ctx.request.body.mobile,
                password: password,
                remarks: ctx.request.body.remarks
            })
            UserModel.save(function (err) {
                if (err) console.log(err)
            })
回答
編輯回答
青瓷

1。為什么不能使用默認id
2。如果自定義id,那么默認id也會自動生成
3。要使用自定義id查詢,就用自定義的字段名稱就好,默認id當然同樣是可以繼續(xù)使用的

2017年10月17日 19:34