您好,我正在创建一系列分组来描述某些用户在帮助客户的背景下所扮演的角色。Prospect 模型中的对象称为个案工作者。在 caseworkers 中是一系列数组,用于完成不同类型的角色。等式是允许用户将他的信息作为称为 CaseWorker 的子文档推送。基本上创建一个用户可以推送到的具有 6 个数组的对象。我尝试了一些事情并选择了子文档。任何帮助都是极好的。这是我的代码:const mongoose = require("mongoose");const CaseWorker = require("./CaseWorker");const ProspectSchema = mongoose.Schema({ caseWorkers: { originators: [CaseWorker.schema], loanProcessors: [CaseWorker.schema], documentProcessors: [CaseWorker.schema], upsells: [CaseWorker.schema], primaryReso: [CaseWorker.schema], taxPreparers: [CaseWorker.schema], secondaryReso: [CaseWorker.schema], }module.exports = mongoose.model("prospect", ProspectSchema);const mongoose = require("mongoose");const Schema = mongoose.Schema;const CaseWorkerSchema = new Schema({ name: { type: String, required: true, }, email: { type: String, required: true, unique: true, }, role: { type: String }, resoCred1: { type: String }, resoCred2: { type: String }, reminders: [ { _id: { type: mongoose.Schema.Types.ObjectId, ref: "User" }, userReminded: { type: mongoose.Schema.Types.ObjectId }, reminderDate: { type: Date }, reminderDueDate: { type: Date }, status: { type: String }, daysTilDue: { type: Number }, id: { type: String }, text: { type: String }, clientId: { type: mongoose.Schema.Types.ObjectId, ref: "Prospect" }, }, ],});module.exports = mongoose.model("caseWorker", CaseWorkerSchema);router.put("/:_id/caseWorkers/loanProcessors", auth, async (req, res) => { const prospect = await Prospect.findByIdAndUpdate(req.params._id, { "$push": { "loanProcessors": { "caseWorker": { "name": req.body.name, "email": req.body.email, "role": req.body.role, "resoCred1": req.body.resoCred1, "resoCred2": req.body.resoCred2, }, }, }, }); res.json(prospect); console.log(prospect);});
1 回答
杨魅力
TA贡献1811条经验 获得超6个赞
在您更新您放置的文档时的方法中,caseWorker但loanProcessors它在模式中以相反的方式声明。
要更新嵌套对象,您必须使用点符号来引用该字段。
不要忘记将表示字段的对象键放在这样的字符串"caseWorkers.loanProcessors"中,因为caseWorkers.loanProcessors在 javascript 中是无效的对象键
"$push": {
"caseWorkers.loanProcessors": {
"name": req.body.name,
"email": req.body.email,
"role": req.body.role,
"resoCred1": req.body.resoCred1,
"resoCred2": req.body.resoCred2,
},
},
添加回答
举报
0/150
提交
取消