保存到mongDB 3级嵌套对象数组

时间:2019-07-04 15:07:11

标签: node.js express mongoose

我正在尝试使用以下模型样本将数据保存到Teams数据库中。这是使用Mongoose的MongoDB的第三层嵌套对象数组。


    const teamSchema = new Schema({
        name: {
            type: String,
            required: true
        },
        who: {
            type: String,
            required: true
        },
        textManifesto: {
            type: String,
            required: false
        },
        videoManifesto: {
            type: String,
            required: false
        },
        competencies: {
            competency: [{
                type: Schema.Types.ObjectId,
                ref: 'Competency',
                required: true
            }]
        },
        members: [{
            member: {
                type: Schema.Types.ObjectId,
                ref: 'Applicant',
                required: true
            },
            position: {
                type: String,
                required: true
            }, 
            memberCompetencies: {
                competency: [{
                    type: Schema.Types.ObjectId,
                    ref: 'Competency',
                    required: true
                }]
            },
            evaluatedCompetencies: {
                competency: [{
                    type: Schema.Types.ObjectId,
                    ref: 'Competency',
                    required: false
                }]
            }
        }],
    },

我基本上使用push方法将数组元素推送到变量中,然后尝试保存到数据库中,但对于Members.memberCompetencies,数据库为空。它只是显示并为空数组。



    //Map Team Competencies
        for (let competency of teamCompetencies) //array is your array variable, i suppose
            newTeamCompetencies.push({ competency: competency, _id: mongoose.Types.ObjectId() });

            //console.log(newTeamCompetencies);

            //Map My Competencies
        for (let competency of myCompetencies) //array is your array variable, i suppose
            newMyCompetencies.push({ _id: mongoose.Types.ObjectId(), competency: competency });

            // console.log(newMyCompetencies);


        team = await new Team({
            name: teamName,
            textManifesto: textManifesto,
            who: who,
        });
        //save collective expected competencies of team
        team.competencies = newMyCompetencies;
        //save member details
        team.members = ({
            member: res.locals.applicant._id,
            position: 'Leader',
            memberCompetencies: newMyCompetencies,
        })


        team = await team.save();

        console.log(team.members);

预期结果

    [{"memberCompetencies":{ '0':
          { _id: 5d1e128b2a9f1c74907e5ba9,
            competency: '5d1dd97206660707754eefb3' },
         '1':
          { _id: 5d1e128b2a9f1c74907e5baa,
            competency: '5d1dd9d506660707754eefb4' } },,"evaluatedCompetencies":{"competency":[]},"_id":"5d1e1393f640587531b0fd48","member":"5d19999df6f9c678e891af14","position":"Leader"}]

实际结果


    [{"memberCompetencies":{"competency":[]},"evaluatedCompetencies":{"competency":[]},"_id":"5d1e1393f640587531b0fd48","member":"5d19999df6f9c678e891af14","position":"Leader"}]

1 个答案:

答案 0 :(得分:0)

发现了问题。该模型是错误的。模型的“成员”部分应为


    members: {
                member: {
                    type: Schema.Types.ObjectId,
                    ref: 'Applicant',
                    required: true
                },
                position: {
                    type: String,
                    required: true
                }, 
                memberCompetencies: {
                    competency: [{
                        type: Schema.Types.ObjectId,
                        ref: 'Competency',
                        required: true
                    }]
                },
                evaluatedCompetencies: {
                    competency: [{
                        type: Schema.Types.ObjectId,
                        ref: 'Competency',
                        required: false
                    }]
                }
            },

应该是


    members: {...} 
    NOT 
    members: [{...}]