【发布时间】:2019-11-01 20:49:15
【问题描述】:
This brilliant answer by Soviut 解释了使用电子邮件验证来验证和激活新用户所需的逻辑步骤。
但是,使用 node.js 和 mongoose 我不确定保存文档以及引用第一个文档 ID 的第二个文档需要哪些步骤。
- 有没有办法同时保存两个文档?
- 是否需要保存第一个文档,然后使用
findOne找到它并将其 ID 复制到第二个文档中? - 对于此类问题是否有最佳实践?
这里有 2 个示例架构,可以使这一点更清楚。
新用户:
const UserSchema = new mongoose.Schema({
email: {
type: String,
required: true
},
yourName: {
type: String,
required: true
},
businessName: {
type: String,
required: true
},
password: {
type: String,
required: true
},
active: {
type: Boolean,
default: false
}
});
用于验证的临时哈希:
const NewUserHash = new mongoose.Schema({
randomHash: {
type: String
},
// what is the best practice for getting the ID from a UserSchema document and saving it here?
referenceToUser: {
type: String
},
createdAt: {
type: Date,
default: Date.now,
expires: 7200
}
});
【问题讨论】: