如何通过推数组在猫鼬中建立一对多的关系?

时间:2020-09-05 05:15:16

标签: node.js express mongoose

我有两个集合,即Project和Developer。 我想将多个项目分配给开发人员。 我可以将一个项目分配给开发人员,但如何分配多个项目?这对我来说很混乱。

我认为推送数组会很好,但是怎么做呢? 预先感谢。

用于将项目分配给开发人员的控制器

/*@Submit project to developer */
exports.putDevAssign = (req, res, next) => {
    const {projectId, teamId} = req.body

    Team.findByIdAndUpdate(teamId, { project : projectId }, { new: true }).populate('project')
    .then(result => {
        const member = result
        res.render('team/project-list', {
            result,
            member
        })
    })
    .catch(err => console.log(err))
}

开发者模型

const mongoose = require('mongoose')
const bcrypt = require('bcryptjs')

const Schema = mongoose.Schema

const teamSchema = new Schema({
    position: {
        type: String,
        required: true
    },
    project: {
        type: Schema.Types.ObjectId,
        ref:'Admin'
    },
    email: {
        type: String,
        required: true
    },
    password: {
        type: String,
        required:true
    }
})

teamSchema.pre('save', function(next) {
    const team = this

    bcrypt.hash(team.password, 10, function (error, encrypted) {
        team.password = encrypted
        next()
    })
}) //hook

module.exports = mongoose.model('Team', teamSchema)

和项目架构

const mongoose = require('mongoose')

const Schema = mongoose.Schema

const adminSchema = new Schema({
    pName: {
        type: String,
        required: true
    },
    category: {
        type: String,
        required: true
    },
    totalPrice: {
        type: Number,
        required: true
    },
    advance: {
        type: Number,
        required: true
    },
    logo: {
        type: String,
    },
    startDate: {
        type: String,
        required: true
    },
    endDate: {
        type: String,
        required: true
    },
    cName: {
        type: String,
        required: true
    },
    address: {
        type: String,
        required: true
    },
    email: {
        type: String,
        required: true
    },
    number: {
        type: Number,
        required: true
    },
    logo: {
        type: String,
        required: true
    },
    info :{
        type: String
    },
    /* ,
    docs: {
        type: Array
    } */


}, {
    timestamps:true
})

module.exports = mongoose.model('Admin',adminSchema)

2 个答案:

答案 0 :(得分:0)

首先,您的团队模式中的项目必须位于这样的Array中:

const mongoose = require('mongoose')
const bcrypt = require('bcryptjs')

const Schema = mongoose.Schema

const teamSchema = new Schema({
    position: {
        type: String,
        required: true
    },
    project: [{
        type: Schema.Types.ObjectId,
        ref:'project'
    }],
    email: {
        type: String,
        required: true
    },
    password: {
        type: String,
        required:true
    }
})

teamSchema.pre('save', function(next) {
    const team = this

    bcrypt.hash(team.password, 10, function (error, encrypted) {
        team.password = encrypted
        next()
    })
}) //hook

module.exports = mongoose.model('Team', teamSchema)

然后,创建另一个名为Projects Hema的架构,在其中存储这样的项目:

const mongoose = require('mongoose')
const bcrypt = require('bcryptjs')

const Schema = mongoose.Schema

const projectSchema = new Schema({
    title: {
        type: String,
        required: true
    },
      description: {
        type: String,
        required: true
    }

})


module.exports = mongoose.model('Project', projectSchema)

最后输入您的密码

exports.putDevAssign = (req, res, next) => {
    const {projectId, teamId} = req.body

    Team.findByIdAndUpdate(teamId, { project : projectId }, { new: true }).populate('project')
    .then(result => {
        res.render('team/project-list', {
            result,
            member
        })
    })
    .catch(err => console.log(err))
}

答案 1 :(得分:0)

浏览并从Internet获得帮助后,我修复了此问题。可以做到

  1. 在架构中添加括号
const mongoose = require('mongoose')
const bcrypt = require('bcryptjs')

const Schema = mongoose.Schema

const teamSchema = new Schema({
    position: {
        type: String,
        required: true
    },
    project: [{
        type: Schema.Types.ObjectId,
        ref:'project'
    }],
    email: {
        type: String,
        required: true
    },
    password: {
        type: String,
        required:true
    }
})

teamSchema.pre('save', function(next) {
    const team = this

    bcrypt.hash(team.password, 10, function (error, encrypted) {
        team.password = encrypted
        next()
    })
}) //hook

module.exports = mongoose.model('Team', teamSchema)
  1. 然后使用$ push功能并检查分配给开发人员代码的唯一项目,看起来像这样
/*@Submit assign project to developer */
exports.putDevAssign = (req, res, next) => {
    const {projectId, teamId} = req.body

    Team.findById(teamId)
    .then(result => {
        if(result.project.length == 0 ) {
            Team.findByIdAndUpdate(teamId, {$push: { project : projectId } }, { new: true }).populate('project')
                    .then(result => {
                        res.redirect('/Assigned-Developer/' + teamId)
                    })
                    .catch(err => console.log(err))
        } else {
            for(let i = 0; i < result.project.length; i++) {
                if( projectId == result.project[i] ) {  
                    req.flash('message', 'Already Assigned')
                    return res.redirect('/Assigned-Developer/' + teamId)  // project already in list
                }
            }
            // if code reached here, project not in current project list, must add it
            Team.findByIdAndUpdate(teamId, {$push: { project : projectId } }, { new: true }).populate('project')
            .then(result => {
                return res.redirect('/Assigned-Developer/' + teamId)
            })
            .catch(err => console.log(err))
            }
        })
    .catch(err => console.log)
}