推送到猫鼬子文档

时间:2020-07-05 00:53:20

标签: javascript node.js mongoose

您好,我正在创建一系列分组,描述某些用户在帮助客户的背景下所扮演的角色。 Prospect模型中的对象称为案例工作者。在案例工作者中,是一系列数组,用于完成不同类型的角色。方程式是允许用户将其信息作为名为CaseWorker的子文档进行推送。基本上创建一个具有6个数组的对象,用户可以将其推入。香港专业教育学院尝试了一些事情,并解决了子文档。任何帮助都会很棒。

这是我的代码:

id
1
2
3
4
8
7
6
5
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);

1 个答案:

答案 0 :(得分:0)

在更新文档时,您将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,
      },
    },