繁体   English   中英

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

[英]How to make one to many relation in Mongoose by pushing array?

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

我认为推数组会很好,但怎么做呢? 提前致谢。

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

/*@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)

首先,您的团队架构中的项目需要位于这样的数组中:

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. 将括号添加到架构
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)
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM