我认为我遇到了这个问题https://github.com/Automattic/mongoose/issues/1844。
我可以看到会发生这种情况 - 一个请求进来,测试正在更新,同时另一个请求到来,导致测试的另一个更新。
我的架构看起来像这样
const User = new mongoose.Schema({
_id: { type: String, default: uuid.v1 },
firstName: {
type: String,
required: true
},
lastName: {
type: String,
required: true
},
tests: [Test],
});
const Test = new mongoose.Schema(
{
_id: { type: String, default: uuid.v1 },
responses: [Response]
},
{
timestamps: true
}
);
const Response = new mongoose.Schema({
_id: { type: String, default: uuid.v1 },
answer: {
type: String,
enum: [
"StronglyAgree",
"Agree",
"SomewhatAgree",
"Neutral",
"SomewhatDisagree",
"Disagree",
"StronglyDisagree"
]
},
question: { type: String, ref: "Question" }
});
const Question = new mongoose.Schema({
_id: { type: String, default: uuid.v1 },
description: String,
});
我有一个使用猫鼬模特的课程UserModel
。
这样做
async createTest(userId) {
try {
const test = await this.testModel.create();
try {
const user = await this.model.findOne({ userId });
if (user) {
user.tests.push(test);
return await user.save();
} else {
throw new Error("Non existent UserId");
}
} catch (e) {
throw e;
}
} catch (e) {
throw e;
}
}
这就是创造的样子。
async create() {
if (!this._model) {
await this._getModel();
}
try {
const questions = await this.questionModel.getAllQuestions();
const test = new this.model();
questions.forEach(question => {
const response = this.responseModel.create(question.id);
test.responses.push(response);
});
await this.model.populate(test, {
path: "responses.question",
model: "Question"
});
return test;
} catch (e) {
throw e;
}
}
我不确定如何重写这个以避免版本控制问题(而且我不想跳过版本控制)。该模式对我来说也很有意义,因为我不想对问题进行重复描述(我将来可能需要更改说明)。
我该怎么做?
答案 0 :(得分:0)
唯一的方法是通过添加
来禁用版本控制{ " versionKey":假 }
在架构的末尾。