【发布时间】:2020-09-30 21:19:34
【问题描述】:
我是猫鼬的新手,我有一个像这样的猫鼬模式:
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'});
请注意,用户 ID 是一个自动递增的数字字段,用于插入值 am 使用 mongoose 查询,例如:
new user(contents).save(function (err,doc){};
'contents' 是一个对象,其中包含除 userID 之外的所有字段的数据,这里我的问题是如何在插入其他字段的记录时插入 userID(自动增量编号)的值?我参考这个link来设置自动增量值......但我不知道如何在猫鼬中使用它?
【问题讨论】:
-
或者我们是否可以在添加新记录时将自动增量数字添加到 mongodb 中?有可能吗?
-
您的用户 ID 不是数字而是字符串。你有整篇文章在这里:docs.mongodb.org/manual/tutorial/…
-
为什么您需要这个自动递增字段? Mongo 在
_id字段中为您提供了一个唯一标识符。这还不够吗?