Node.js 如何在我的猫鼬收藏中插入自动递增编号
我是猫鼬的新手,我有这样一个猫鼬模式:Node.js 如何在我的猫鼬收藏中插入自动递增编号,node.js,mongodb,mongoose,Node.js,Mongodb,Mongoose,我是猫鼬的新手,我有这样一个猫鼬模式: var user = mongoose.Schema({ userID: { type: String, required:true }, seq: { type: Number, default: 0 }, firstname: { type: String }, lastname: { type: St
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'});
UserSchema.pre("save", function (next) {
if (this.isNew) {
this.constructor.find({}).then((users) => {
this.autoIncrementProp = users.length + 1;
next();
});
}
});
请注意,userID是一个自动递增的数字字段,用于使用mongoose查询插入值am,如:
new user(contents).save(function (err,doc){};
“contents”是一个对象,它包含除userID之外的所有字段的数据,这里我的问题是如何在插入其他字段的记录时插入userID(自动递增编号)的值?我引用它来设置自动增量值。。。但是我不知道如何在mongoose中使用它?在MongoDB教程之后,,您需要首先创建一个单独的
计数器
集合来跟踪使用的最后一个数字序列。\u id
字段包含序列名称,即用户集合中的userID
字段,而seq
字段包含序列的最后一个值
首先,在计数器集合中插入用户ID的初始值
:
db.counter.insert(
{
"_id": "userID",
"seq": 0
}
)
填充计数器集合后,在Mongoose中生成其架构:
var counterSchema = mongoose.Schema({
"_id": { "type": String, "required": true },
"seq": { "type": Number, "default": 0 }
});
var counter = mongoose.model('counter', counterSchema);
然后重新定义用户模式,以便在保存用户模型时,它首先调用计数器模型的findbyidanddupdate()
方法,以原子方式递增seq值并返回此新值,然后将其用作下一个userID
值:
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();
});
});
考虑到schema.findByIdAndUpdate的逻辑是“返回当前值,然后增加它”,您还可以使用@chridam的解决方案进行以下编辑:
var counter = mongoose.model('counter', counterSchema);
userSchema.pre("save", function (next) {
var doc = this;
counter.findByIdAndUpdate(
{ "_id": "userID" },
{ "$inc": { "seq": 1 } }
, function(error, c /*counter*/) {
if(error)
return next(error);
else if(!c) {
c = new counter({ _id: "userID" }, { $inc: { seq: 1 } };
c.save(function() {
doc.userID = (c.seq - 1) + '';
next();
});
} else {
doc.userID = counter.seq.toString();
next();
}
});
});
请注意,此解决方案使您的代码从头开始工作,而不必强制您初始化数据库
诀窍在于循环的第一轮<代码>计数器未定义,因此需要对其进行初始化,但如果将其初始化为0,则下一个勾号代码将再次尝试分配userID=0
这是因为一方面存在schema.findByIdAndUpdate的逻辑,该逻辑首先读取值,然后将其递增,另一方面,您需要一个先分配值的逻辑
您可以通过使算法跳过第一步并如上所述设置变量值来连接这两个逻辑
注:counterSchema
与chridam的相同:
counterSchema = mongoose.Schema({
"_id": { "type": String, "required": true },
"seq": { "type": Number, "default": 0 }
});
如果需要基于收藏长度的自动增量道具,可以执行以下操作:
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'});
UserSchema.pre("save", function (next) {
if (this.isNew) {
this.constructor.find({}).then((users) => {
this.autoIncrementProp = users.length + 1;
next();
});
}
});
isNew是一个保留的模式名(布尔标志,指定文档是否为新文档)。或者我们是否可以在添加新记录时向mongodb中添加自动递增编号?可能吗?您的用户ID不是一个数字,而是一个字符串。这里有整篇文章:为什么需要这个自动递增字段?Mongo在
\u id
字段中为您提供唯一标识符。这还不够吗?谢谢你的帖子。。!执行时,它在“doc.userID=counter.seq.toString();”@Karthik在试图保存用户
模型之前,您是否首先在计数器
集合中插入了用户ID
的初始值?您提到了吗,我必须在中插入一行中的“计数器”集合mongodb@Karthik您必须首先创建集合,我甚至明确提到,在我的回答中,首先在counters集合中插入userID的初始值:我创建了一个初始值为{“\u id”:“500”,seq:0}的计数器集合。它仍然抛出相同的错误”TypeError:无法读取doc.userID=counter.seq.toString()处null“的属性'seq';“这很好,我一直在寻找类似的东西,但我在想,如果一个用户被删除,用户ID是否会重新调整以仍然遵循其正常的编号顺序。例如,如果我们有123,2被删除,3将变为2。不幸的是,计数器并不能填补空白。如果您描述的这种行为经常发生,系统em往往有几个间隙。